pvl/verkko/hosts.py
changeset 4 b09436772d46
parent 3 5990b188c54b
child 5 91970ce3fc6b
equal deleted inserted replaced
3:5990b188c54b 4:b09436772d46
   134 
   134 
   135         html.a(href='/hosts')(html('«'), 'Back'),
   135         html.a(href='/hosts')(html('«'), 'Back'),
   136     )
   136     )
   137 
   137 
   138 class Handler (web.Handler) :
   138 class Handler (web.Handler) :
   139     TITLE = "DHCP Hosts"
   139 
       
   140     def title (self) :
       
   141         pass
   140     
   142     
   141     def index (self) :
   143     def index (self) :
   142         return render_hosts(self.hosts)
   144         return render_hosts(self.hosts)
   143 
   145 
   144     def host (self, id) :
   146     def detail (self) :
   145         host = self.hosts.get(id)
   147         return render_host(self.host, self.hosts)
   146         
   148     
   147         if not host :
   149     def list (self) :
   148             raise web.NotFound("No such host: {id}".format(id=id))
   150         return render_host(self.host, self.hosts)
   149 
   151     
   150         hosts = self.hosts.filter((Host.ip == host.ip) | (Host.mac == host.mac))
   152     def process (self, id=None, attr=None, value=None) :
   151         
       
   152         # XXX
       
   153         #self.title = "DHCP Host: {host}".format(host=unicode(host))
       
   154 
       
   155         return render_host(host, hosts)
       
   156     
       
   157     def list (self, attr, value) :
       
   158         # fake host
       
   159         host = { 'ip': None, 'mac': None, 'name': None }
       
   160 
       
   161         if attr not in HOST_ATTRS :
       
   162             raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr))
       
   163 
       
   164         host[attr] = value
       
   165 
       
   166         host = Host(**host)
       
   167 
       
   168         # query
       
   169         attr = HOST_ATTRS[attr]
       
   170         log.debug("%s == %s", attr, value)
       
   171 
       
   172         hosts = self.hosts.filter(attr == value)
       
   173         
       
   174         # XXX
       
   175         #self.title = "DHCP Hosts: {value}".format(value=value)
       
   176 
       
   177         return render_host(host, hosts)
       
   178     
       
   179     def process (self) :
       
   180         hosts = self.db.query(Host)
   153         hosts = self.db.query(Host)
   181 
   154 
   182         # sort ?
   155         # sort ?
   183         sort = self.request.args.get('sort')
   156         sort = self.request.args.get('sort')
   184 
   157 
   189 
   162 
   190         log.debug("sort: %s", sort)
   163         log.debug("sort: %s", sort)
   191 
   164 
   192         hosts = hosts.order_by(sort)
   165         hosts = hosts.order_by(sort)
   193         
   166         
   194         # store
   167         # lookup host
   195         self.hosts = hosts
   168         if id :
   196     
   169             self.host = hosts.get(id)
   197     def render (self) :
   170             
   198         # index
   171             if not self.host :
   199         if not self.path :
   172                 raise web.NotFound("No such host: {id}".format(id=id))
   200             return self.index()
   173 
       
   174             self.hosts = hosts.filter((Host.ip == self.host.ip) | (Host.mac == self.host.mac))
       
   175             self.render = self.detail
       
   176             self.title = "DHCP Host: {host}".format(host=unicode(self.host))
   201         
   177         
   202         # id
   178         # lookup hosts
   203         elif len(self.path) == 1 :
   179         elif attr and value :
   204             try :
   180             # fake host
   205                 id, = self.path
   181             host = { 'ip': None, 'mac': None, 'name': None }
   206                 id = int(id)
   182 
   207             except ValueError as ex :
   183             if attr not in HOST_ATTRS :
   208                 raise web.BadRequest("Invalid host ID: {id}: {ex}".format(id=id, ex=ex))
   184                 raise web.BadRequest("Invalid attribute: {attr}".format(attr=attr))
   209 
   185 
   210             return self.host(id)
   186             host[attr] = value
   211 
   187 
   212         # query
   188             self.host = Host(**host)
   213         elif len(self.path) == 2 :
   189 
   214             attr, value = self.path
   190             # query
   215             
   191             attr = HOST_ATTRS[attr]
   216             return self.list(attr, value)
   192             log.debug("%s == %s", attr, value)
   217 
   193 
   218         else :
   194             self.hosts = hosts.filter(attr == value)
   219             raise web.NotFound
   195             self.render = self.list
   220 
   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