pvl/verkko/hosts.py
changeset 4 b09436772d46
parent 3 5990b188c54b
child 5 91970ce3fc6b
--- a/pvl/verkko/hosts.py	Wed Oct 10 22:45:50 2012 +0300
+++ b/pvl/verkko/hosts.py	Wed Oct 10 23:16:25 2012 +0300
@@ -136,47 +136,20 @@
     )
 
 class Handler (web.Handler) :
-    TITLE = "DHCP Hosts"
+
+    def title (self) :
+        pass
     
     def index (self) :
         return render_hosts(self.hosts)
 
-    def host (self, id) :
-        host = self.hosts.get(id)
-        
-        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)
+    def detail (self) :
+        return render_host(self.host, self.hosts)
     
-    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_ATTRS[attr]
-        log.debug("%s == %s", attr, value)
-
-        hosts = self.hosts.filter(attr == value)
-        
-        # XXX
-        #self.title = "DHCP Hosts: {value}".format(value=value)
-
-        return render_host(host, hosts)
+    def list (self) :
+        return render_host(self.host, self.hosts)
     
-    def process (self) :
+    def process (self, id=None, attr=None, value=None) :
         hosts = self.db.query(Host)
 
         # sort ?
@@ -191,30 +164,40 @@
 
         hosts = hosts.order_by(sort)
         
-        # store
-        self.hosts = hosts
-    
-    def render (self) :
-        # index
-        if not self.path :
-            return self.index()
+        # 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))
         
-        # 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))
+        # lookup hosts
+        elif attr and value :
+            # fake host
+            host = { 'ip': None, 'mac': None, 'name': None }
 
-            return self.host(id)
+            if attr not in HOST_ATTRS :
+                raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr))
 
-        # query
-        elif len(self.path) == 2 :
-            attr, value = self.path
-            
-            return self.list(attr, value)
+            host[attr] = value
 
+            self.host = Host(**host)
+
+            # query
+            attr = HOST_ATTRS[attr]
+            log.debug("%s == %s", attr, value)
+
+            self.hosts = hosts.filter(attr == value)
+            self.render = self.list
+            self.title = "DHCP Hosts: {value}".format(value=value)
+
+        # list
         else :
-            raise web.NotFound
-
+            self.hosts = hosts
+            self.render = self.index
+            self.title = "DHCP Hosts"
+