--- a/pvl/verkko/hosts.py Wed Oct 10 23:16:25 2012 +0300
+++ b/pvl/verkko/hosts.py Wed Oct 10 23:29:38 2012 +0300
@@ -64,140 +64,143 @@
#_name = db.dhcp_hosts.c.name,
))
-HOST_ATTRS = {
- 'id': Host.id,
- 'ip': Host.ip,
- 'mac': Host.mac,
- 'name': Host.name,
- 'seen': Host.last_seen,
-}
-HOST_SORT = Host.last_seen.desc()
-
-def render_hosts (hosts, title=None) :
- COLS = (
- #title sort
- ('#', None),
- ('IP', 'ip'),
- ('MAC', 'mac'),
- ('Hostname', 'name'),
- ('Seen', 'seen'),
- )
+class BaseHandler (web.Handler) :
+ HOST_ATTRS = {
+ 'id': Host.id,
+ 'ip': Host.ip,
+ 'mac': Host.mac,
+ 'name': Host.name,
+ 'seen': Host.last_seen,
+ }
- return html.table(
- html.caption(title) if title else None,
- html.thead(
- html.tr(
- html.th(
- html.a(href='?sort={name}'.format(name=sort))(title) if sort else (title)
- ) for title, sort in COLS
- )
- ),
- html.tbody(
- html.tr(class_=('alternate' if i % 2 else None), id=host.id)(
- html.td(class_='id')(
- html.a(href='/hosts/{host.id}'.format(host=host))(
- '#' #host['rowid'])
- )
- ),
- html.td(class_='ip')(
- html.a(href='/hosts/ip/{host.ip}'.format(host=host))(host.ip)
- ),
- html.td(class_='mac')(
- html.a(href='/hosts/mac/{host.mac}'.format(host=host))(
- host.render_mac()
- )
- ),
- html.td(host.render_name()),
- html.td(host.when()),
- ) for i, host in enumerate(hosts)
- )
- )
+ HOST_SORT = Host.last_seen.desc()
-def render_host (host, hosts) :
- title = 'DHCP Host: {host}'.format(host=host)
-
- attrs = (
- ('IP', host.ip),
- ('MAC', host.mac),
- ('Hostname', host.name),
- ('DNS', host.dns()),
- )
-
- return (
- html.h2('Host'),
- html.dl(
- (html.dt(title), html.dd(value)) for title, value in attrs
- ),
-
- html.h2('Related'),
- render_hosts(hosts),
-
- html.a(href='/hosts')(html('«'), 'Back'),
- )
-
-class Handler (web.Handler) :
-
- def title (self) :
- pass
-
- def index (self) :
- return render_hosts(self.hosts)
-
- def detail (self) :
- return render_host(self.host, self.hosts)
-
- def list (self) :
- return render_host(self.host, self.hosts)
-
- def process (self, id=None, attr=None, value=None) :
+ def query (self) :
hosts = self.db.query(Host)
# sort ?
sort = self.request.args.get('sort')
if sort :
- sort = HOST_ATTRS[sort]
+ sort = self.HOST_ATTRS[sort]
else :
- sort = HOST_SORT
+ sort = self.HOST_SORT
log.debug("sort: %s", sort)
hosts = hosts.order_by(sort)
-
- # lookup host
- if id :
- self.host = hosts.get(id)
-
- if not self.host :
- raise web.NotFound("No such host: {id}".format(id=id))
-
- self.hosts = hosts.filter((Host.ip == self.host.ip) | (Host.mac == self.host.mac))
- self.render = self.detail
- self.title = "DHCP Host: {host}".format(host=unicode(self.host))
-
- # lookup hosts
- elif attr and value :
- # fake host
- host = { 'ip': None, 'mac': None, 'name': None }
- if attr not in HOST_ATTRS :
- raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr))
-
- host[attr] = value
-
- self.host = Host(**host)
+ # k
+ return hosts
+
+ def render_hosts (self, hosts, title=None) :
+ COLS = (
+ #title sort
+ ('#', None),
+ ('IP', 'ip'),
+ ('MAC', 'mac'),
+ ('Hostname', 'name'),
+ ('Seen', 'seen'),
+ )
- # query
- attr = HOST_ATTRS[attr]
- log.debug("%s == %s", attr, value)
+ return html.table(
+ html.caption(title) if title else None,
+ html.thead(
+ html.tr(
+ html.th(
+ html.a(href='?sort={name}'.format(name=sort))(title) if sort else (title)
+ ) for title, sort in COLS
+ )
+ ),
+ html.tbody(
+ html.tr(class_=('alternate' if i % 2 else None), id=host.id)(
+ html.td(class_='id')(
+ html.a(href='/hosts/{host.id}'.format(host=host))(
+ '#' #host['rowid'])
+ )
+ ),
+ html.td(class_='ip')(
+ html.a(href='/hosts/ip/{host.ip}'.format(host=host))(host.ip)
+ ),
+ html.td(class_='mac')(
+ html.a(href='/hosts/mac/{host.mac}'.format(host=host))(
+ host.render_mac()
+ )
+ ),
+ html.td(host.render_name()),
+ html.td(host.when()),
+ ) for i, host in enumerate(hosts)
+ )
+ )
- self.hosts = hosts.filter(attr == value)
- self.render = self.list
- self.title = "DHCP Hosts: {value}".format(value=value)
+ def render_host (self, host, hosts) :
+ attrs = (
+ ('IP', host.ip),
+ ('MAC', host.mac),
+ ('Hostname', host.name),
+ ('DNS', host.dns()),
+ )
- # list
- else :
- self.hosts = hosts
- self.render = self.index
- self.title = "DHCP Hosts"
-
+ return (
+ html.h2('Host'),
+ html.dl(
+ (html.dt(title), html.dd(value)) for title, value in attrs
+ ),
+
+ html.h2('Related'),
+ self.render_hosts(hosts),
+
+ html.a(href='/hosts')(html('«'), 'Back'),
+ )
+
+class IndexHandler (BaseHandler) :
+ def process (self) :
+ self.hosts = self.query()
+
+ def title (self) :
+ return "DHCP Hosts"
+
+ def render (self) :
+ return self.render_hosts(self.hosts)
+
+class ItemHandler (BaseHandler) :
+ def process (self, id) :
+ self.hosts = self.query()
+ self.host = self.hosts.get(id)
+
+ if not self.host :
+ raise web.NotFound("No such host: {id}".format(id=id))
+
+ self.hosts = self.hosts.filter((Host.ip == self.host.ip) | (Host.mac == self.host.mac))
+
+ def title (self) :
+ return u"DHCP Host: {self.host}".format(self=self)
+
+ def render (self) :
+ return self.render_host(self.host, self.hosts)
+
+class ListHandler (BaseHandler) :
+ def process (self, attr, value) :
+ # fake host
+ _host = { 'ip': None, 'mac': None, 'name': None }
+
+ if attr not in self.HOST_ATTRS :
+ raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr))
+
+ _host[attr] = value
+
+ self.host = Host(**_host)
+ self.expression = "{attr}: {value}".format(attr=attr, value=value)
+
+ # query
+ attr = self.HOST_ATTRS[attr]
+ log.debug("%s == %s", attr, value)
+
+ self.hosts = self.query().filter(attr == value)
+
+ def title (self) :
+ return "DHCP Hosts: {self.expression}".format(self=self)
+
+ def render (self) :
+ return self.render_host(self.host, self.hosts)
+