diff -r 9940bc6c0a34 -r 38265b7d8f62 pvl/verkko/hosts.py --- a/pvl/verkko/hosts.py Wed Oct 24 20:00:12 2012 +0300 +++ b/pvl/verkko/hosts.py Wed Oct 24 20:27:39 2012 +0300 @@ -485,33 +485,47 @@ '/static/hosts.js', ) + COLUMNS = ( + ( 'ip', 'IP', lambda host: host.ip ), + ( 'mac', 'MAC', lambda host: host.mac ), + ( 'name', 'Hostname', lambda host: host.name ), + ( 'gw', 'Network', lambda host: host.gw ), + ( 'seen', 'Seen', Host.seen, ), + ( 'state', 'State', lambda host: host.state ), + ) + + def process (self) : hosts = self.db.query(Host).order_by(Host.last_seen.desc()) t = self.request.args.get('t') + + def host_params (host) : + yield 'id', host.id + + for name, title, fvalue in self.COLUMNS : + value = fvalue(host) + + if name == 'seen' : + # XXX: hackfix html() rendering + value = unicode(html.div(value)) + + yield name, value + + # special + yield 'state_class', host.state_class() if t : + # return json t = ts2dt(int(t)) - # update hosts = hosts.filter(Host.last_seen > t) hosts = list(hosts) hosts.reverse() if hosts : t = hosts[-1].last_seen - hosts = [dict( - id = host.id, - ip = host.ip, - mac = host.mac, - name = host.name, - gw = host.gw, - seen = unicode(html.div(host.seen())), - state = host.state, - state_class = host.state_class(), + hosts = [dict(host_params(host)) for host in hosts] - t = dt2ts(host.last_seen), - ) for host in hosts] - else : hosts = [] @@ -523,6 +537,7 @@ return web.Response(json.dumps(data), mimetype='text/json') else : + # render html self.hosts = hosts.limit(10) # XXX: testing @@ -531,34 +546,39 @@ self.t = self.hosts[0].last_seen def render (self) : + def column (name, title, fvalue, host) : + cls = name + + if name == 'state' : + cls = host.state_class() + + return html.td(class_=cls)(fvalue(host)) + params = dict( url = self.url(), t = dt2ts(self.t), host = self.url(ItemHandler, id='0'), + columns = [name for name, title, fvalue in self.COLUMNS] ) params = json.dumps(params) - COLUMNS = ( - '#', 'IP', 'MAC', 'Hostname', 'Network', 'Seen', 'State' - ) - return html.div(id='wrapper')( html.input(type='submit', id='refresh', value="Refresh"), html.table(id='hosts')( html.thead( html.tr( - html.th(title) for title in COLUMNS + html.th('#'), + ( + html.th(class_=name)(title) for name, title, fvalue in self.COLUMNS + ) ), ), html.tbody( html.tr(id=host.id)( html.td(html.a(href=self.url(ItemHandler, id=host.id))('#')), ( - html.td(value) for value in ( - host.ip, host.mac, host.name, host.gw, host.seen(), - ) + column(name, title, fvalue, host) for name, title, fvalue in self.COLUMNS ), - html.td(class_=host.state_class())(host.state), ) for host in self.hosts ) ),