diff -r b0659c867226 -r 5990b188c54b pvl/verkko/hosts.py --- a/pvl/verkko/hosts.py Wed Oct 10 22:10:16 2012 +0300 +++ b/pvl/verkko/hosts.py Wed Oct 10 22:45:50 2012 +0300 @@ -64,14 +64,14 @@ #_name = db.dhcp_hosts.c.name, )) -HOST_SORT = { +HOST_ATTRS = { 'id': Host.id, 'ip': Host.ip, 'mac': Host.mac, 'name': Host.name, 'seen': Host.last_seen, - None: Host.last_seen.desc(), } +HOST_SORT = Host.last_seen.desc() def render_hosts (hosts, title=None) : COLS = ( @@ -135,62 +135,86 @@ html.a(href='/hosts')(html('«'), 'Back'), ) -def respond (request, db, path) : - """ - Handle request - """ - - hosts = db.query(Host) - - # sort ? - sort = request.args.get('sort') - sort = HOST_SORT[sort] - log.debug("sort: %s", sort) - - hosts = hosts.order_by(sort) +class Handler (web.Handler) : + TITLE = "DHCP Hosts" - # index - if not path : - title = "DHCP Hosts" - html = render_hosts(hosts) - - # id - elif len(path) == 1 : - id, = path - id = int(id) + def index (self) : + return render_hosts(self.hosts) - host = hosts.get(id) - hosts = hosts.filter((Host.ip == host.ip) | (Host.mac == host.mac)) + def host (self, id) : + host = self.hosts.get(id) - # render - title = "DHCP Host: {host}".format(host=unicode(host)) - html = render_host(host, hosts) + if not host : + raise web.NotFound("No such host: {id}".format(id=id)) + + hosts = self.hosts.filter((Host.ip == host.ip) | (Host.mac == host.mac)) + + # XXX + #self.title = "DHCP Host: {host}".format(host=unicode(host)) + + return render_host(host, hosts) - # query - elif len(path) == 2 : - attr, value = path - + def list (self, attr, 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 host = Host(**host) # query - attr = HOST_SORT[attr] + attr = HOST_ATTRS[attr] log.debug("%s == %s", attr, value) - hosts = hosts.filter(attr == value) + hosts = self.hosts.filter(attr == value) - # render - title = "DHCP Hosts: {value}".format(value=value) - html = render_host(host, hosts) + # XXX + #self.title = "DHCP Hosts: {value}".format(value=value) - else : - return Response("Not Found", status=404) + return render_host(host, hosts) + + def process (self) : + hosts = self.db.query(Host) - # render - html = web.render_layout(title, html) + # sort ? + sort = self.request.args.get('sort') - return web.Response(html, content_type='text/html; charset=UTF-8') + if sort : + sort = HOST_ATTRS[sort] + else : + sort = HOST_SORT + log.debug("sort: %s", sort) + + hosts = hosts.order_by(sort) + + # store + self.hosts = hosts + + def render (self) : + # index + if not self.path : + return self.index() + + # id + elif len(self.path) == 1 : + try : + id, = self.path + id = int(id) + except ValueError as ex : + raise web.BadRequest("Invalid host ID: {id}: {ex}".format(id=id, ex=ex)) + + return self.host(id) + + # query + elif len(self.path) == 2 : + attr, value = self.path + + return self.list(attr, value) + + else : + raise web.NotFound +