62 id = db.dhcp_hosts.c.rowid, |
62 id = db.dhcp_hosts.c.rowid, |
63 #_mac = db.dhcp_hosts.c.mac, |
63 #_mac = db.dhcp_hosts.c.mac, |
64 #_name = db.dhcp_hosts.c.name, |
64 #_name = db.dhcp_hosts.c.name, |
65 )) |
65 )) |
66 |
66 |
67 HOST_ATTRS = { |
67 class BaseHandler (web.Handler) : |
68 'id': Host.id, |
68 HOST_ATTRS = { |
69 'ip': Host.ip, |
69 'id': Host.id, |
70 'mac': Host.mac, |
70 'ip': Host.ip, |
71 'name': Host.name, |
71 'mac': Host.mac, |
72 'seen': Host.last_seen, |
72 'name': Host.name, |
73 } |
73 'seen': Host.last_seen, |
74 HOST_SORT = Host.last_seen.desc() |
74 } |
75 |
75 |
76 def render_hosts (hosts, title=None) : |
76 HOST_SORT = Host.last_seen.desc() |
77 COLS = ( |
77 |
78 #title sort |
78 def query (self) : |
79 ('#', None), |
|
80 ('IP', 'ip'), |
|
81 ('MAC', 'mac'), |
|
82 ('Hostname', 'name'), |
|
83 ('Seen', 'seen'), |
|
84 ) |
|
85 |
|
86 return html.table( |
|
87 html.caption(title) if title else None, |
|
88 html.thead( |
|
89 html.tr( |
|
90 html.th( |
|
91 html.a(href='?sort={name}'.format(name=sort))(title) if sort else (title) |
|
92 ) for title, sort in COLS |
|
93 ) |
|
94 ), |
|
95 html.tbody( |
|
96 html.tr(class_=('alternate' if i % 2 else None), id=host.id)( |
|
97 html.td(class_='id')( |
|
98 html.a(href='/hosts/{host.id}'.format(host=host))( |
|
99 '#' #host['rowid']) |
|
100 ) |
|
101 ), |
|
102 html.td(class_='ip')( |
|
103 html.a(href='/hosts/ip/{host.ip}'.format(host=host))(host.ip) |
|
104 ), |
|
105 html.td(class_='mac')( |
|
106 html.a(href='/hosts/mac/{host.mac}'.format(host=host))( |
|
107 host.render_mac() |
|
108 ) |
|
109 ), |
|
110 html.td(host.render_name()), |
|
111 html.td(host.when()), |
|
112 ) for i, host in enumerate(hosts) |
|
113 ) |
|
114 ) |
|
115 |
|
116 def render_host (host, hosts) : |
|
117 title = 'DHCP Host: {host}'.format(host=host) |
|
118 |
|
119 attrs = ( |
|
120 ('IP', host.ip), |
|
121 ('MAC', host.mac), |
|
122 ('Hostname', host.name), |
|
123 ('DNS', host.dns()), |
|
124 ) |
|
125 |
|
126 return ( |
|
127 html.h2('Host'), |
|
128 html.dl( |
|
129 (html.dt(title), html.dd(value)) for title, value in attrs |
|
130 ), |
|
131 |
|
132 html.h2('Related'), |
|
133 render_hosts(hosts), |
|
134 |
|
135 html.a(href='/hosts')(html('«'), 'Back'), |
|
136 ) |
|
137 |
|
138 class Handler (web.Handler) : |
|
139 |
|
140 def title (self) : |
|
141 pass |
|
142 |
|
143 def index (self) : |
|
144 return render_hosts(self.hosts) |
|
145 |
|
146 def detail (self) : |
|
147 return render_host(self.host, self.hosts) |
|
148 |
|
149 def list (self) : |
|
150 return render_host(self.host, self.hosts) |
|
151 |
|
152 def process (self, id=None, attr=None, value=None) : |
|
153 hosts = self.db.query(Host) |
79 hosts = self.db.query(Host) |
154 |
80 |
155 # sort ? |
81 # sort ? |
156 sort = self.request.args.get('sort') |
82 sort = self.request.args.get('sort') |
157 |
83 |
158 if sort : |
84 if sort : |
159 sort = HOST_ATTRS[sort] |
85 sort = self.HOST_ATTRS[sort] |
160 else : |
86 else : |
161 sort = HOST_SORT |
87 sort = self.HOST_SORT |
162 |
88 |
163 log.debug("sort: %s", sort) |
89 log.debug("sort: %s", sort) |
164 |
90 |
165 hosts = hosts.order_by(sort) |
91 hosts = hosts.order_by(sort) |
|
92 |
|
93 # k |
|
94 return hosts |
|
95 |
|
96 def render_hosts (self, hosts, title=None) : |
|
97 COLS = ( |
|
98 #title sort |
|
99 ('#', None), |
|
100 ('IP', 'ip'), |
|
101 ('MAC', 'mac'), |
|
102 ('Hostname', 'name'), |
|
103 ('Seen', 'seen'), |
|
104 ) |
|
105 |
|
106 return html.table( |
|
107 html.caption(title) if title else None, |
|
108 html.thead( |
|
109 html.tr( |
|
110 html.th( |
|
111 html.a(href='?sort={name}'.format(name=sort))(title) if sort else (title) |
|
112 ) for title, sort in COLS |
|
113 ) |
|
114 ), |
|
115 html.tbody( |
|
116 html.tr(class_=('alternate' if i % 2 else None), id=host.id)( |
|
117 html.td(class_='id')( |
|
118 html.a(href='/hosts/{host.id}'.format(host=host))( |
|
119 '#' #host['rowid']) |
|
120 ) |
|
121 ), |
|
122 html.td(class_='ip')( |
|
123 html.a(href='/hosts/ip/{host.ip}'.format(host=host))(host.ip) |
|
124 ), |
|
125 html.td(class_='mac')( |
|
126 html.a(href='/hosts/mac/{host.mac}'.format(host=host))( |
|
127 host.render_mac() |
|
128 ) |
|
129 ), |
|
130 html.td(host.render_name()), |
|
131 html.td(host.when()), |
|
132 ) for i, host in enumerate(hosts) |
|
133 ) |
|
134 ) |
|
135 |
|
136 def render_host (self, host, hosts) : |
|
137 attrs = ( |
|
138 ('IP', host.ip), |
|
139 ('MAC', host.mac), |
|
140 ('Hostname', host.name), |
|
141 ('DNS', host.dns()), |
|
142 ) |
|
143 |
|
144 return ( |
|
145 html.h2('Host'), |
|
146 html.dl( |
|
147 (html.dt(title), html.dd(value)) for title, value in attrs |
|
148 ), |
|
149 |
|
150 html.h2('Related'), |
|
151 self.render_hosts(hosts), |
|
152 |
|
153 html.a(href='/hosts')(html('«'), 'Back'), |
|
154 ) |
|
155 |
|
156 class IndexHandler (BaseHandler) : |
|
157 def process (self) : |
|
158 self.hosts = self.query() |
|
159 |
|
160 def title (self) : |
|
161 return "DHCP Hosts" |
|
162 |
|
163 def render (self) : |
|
164 return self.render_hosts(self.hosts) |
|
165 |
|
166 class ItemHandler (BaseHandler) : |
|
167 def process (self, id) : |
|
168 self.hosts = self.query() |
|
169 self.host = self.hosts.get(id) |
166 |
170 |
167 # lookup host |
171 if not self.host : |
168 if id : |
172 raise web.NotFound("No such host: {id}".format(id=id)) |
169 self.host = hosts.get(id) |
173 |
170 |
174 self.hosts = self.hosts.filter((Host.ip == self.host.ip) | (Host.mac == self.host.mac)) |
171 if not self.host : |
175 |
172 raise web.NotFound("No such host: {id}".format(id=id)) |
176 def title (self) : |
173 |
177 return u"DHCP Host: {self.host}".format(self=self) |
174 self.hosts = hosts.filter((Host.ip == self.host.ip) | (Host.mac == self.host.mac)) |
178 |
175 self.render = self.detail |
179 def render (self) : |
176 self.title = "DHCP Host: {host}".format(host=unicode(self.host)) |
180 return self.render_host(self.host, self.hosts) |
|
181 |
|
182 class ListHandler (BaseHandler) : |
|
183 def process (self, attr, value) : |
|
184 # fake host |
|
185 _host = { 'ip': None, 'mac': None, 'name': None } |
|
186 |
|
187 if attr not in self.HOST_ATTRS : |
|
188 raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr)) |
|
189 |
|
190 _host[attr] = value |
|
191 |
|
192 self.host = Host(**_host) |
|
193 self.expression = "{attr}: {value}".format(attr=attr, value=value) |
|
194 |
|
195 # query |
|
196 attr = self.HOST_ATTRS[attr] |
|
197 log.debug("%s == %s", attr, value) |
177 |
198 |
178 # lookup hosts |
199 self.hosts = self.query().filter(attr == value) |
179 elif attr and value : |
200 |
180 # fake host |
201 def title (self) : |
181 host = { 'ip': None, 'mac': None, 'name': None } |
202 return "DHCP Hosts: {self.expression}".format(self=self) |
182 |
203 |
183 if attr not in HOST_ATTRS : |
204 def render (self) : |
184 raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr)) |
205 return self.render_host(self.host, self.hosts) |
185 |
206 |
186 host[attr] = value |
|
187 |
|
188 self.host = Host(**host) |
|
189 |
|
190 # query |
|
191 attr = HOST_ATTRS[attr] |
|
192 log.debug("%s == %s", attr, value) |
|
193 |
|
194 self.hosts = hosts.filter(attr == value) |
|
195 self.render = self.list |
|
196 self.title = "DHCP Hosts: {value}".format(value=value) |
|
197 |
|
198 # list |
|
199 else : |
|
200 self.hosts = hosts |
|
201 self.render = self.index |
|
202 self.title = "DHCP Hosts" |
|
203 |
|