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