--- a/pvl/verkko/hosts.py Thu Oct 11 00:44:08 2012 +0300
+++ b/pvl/verkko/hosts.py Thu Oct 11 01:11:33 2012 +0300
@@ -94,10 +94,10 @@
hosts = self.db.query(Host)
# sort ?
- sort = self.request.args.get('sort')
+ self.sort = self.request.args.get('sort')
- if sort :
- sort = self.HOST_ATTRS[sort]
+ if self.sort :
+ sort = self.HOST_ATTRS[self.sort]
else :
sort = self.HOST_SORT
@@ -106,28 +106,44 @@
hosts = hosts.order_by(sort)
# k
- self.sort = sort
-
return hosts
- def render_hosts (self, hosts, title=None) :
+ def render_hosts (self, hosts, title=None, filters=False) :
COLS = (
- #title sort
- ('#', None),
- ('IP', 'ip'),
- ('MAC', 'mac'),
- ('Hostname', 'name'),
- ('Seen', 'seen'),
+ #title sort filter
+ ('IP', 'ip', 'ip' ),
+ ('MAC', 'mac', 'mac' ),
+ ('Hostname', 'name', False ),
+ ('Seen', 'seen', False ),
)
- return html.table(
+ def url (**opts) :
+ args = dict(filters)
+ args.update(opts)
+
+ return self.url(**args)
+
+ table = html.table(
html.caption(title) if title else None,
html.thead(
html.tr(
- html.th(
- html.a(href=self.url(sort=sort))(title) if sort else (title)
- ) for title, sort in COLS
- )
+ html.th('#'),
+ (
+ html.th(
+ html.a(href=url(sort=sort))(title) if sort else (title)
+ ) for title, sort, filter in COLS
+ )
+ ),
+ html.tr(class_='filter')(
+ html.td(
+ html.input(type='submit', value=u'\u00BF'),
+ ),
+ (
+ html.td(
+ html.input(type='text', name=filter, value=filters.get(filter)) if filter else None
+ ) for title, sort, filter in COLS
+ )
+ ) if filters is not False else None
),
html.tbody(
html.tr(class_=('alternate' if i % 2 else None), id=host.id)(
@@ -151,6 +167,14 @@
) for i, host in enumerate(hosts)
)
)
+
+ if filters is False :
+ return table
+ else :
+ return html.form(method='get', action=self.url())(
+ html.input(type='hidden', name='sort', value=self.sort),
+ table,
+ )
def render_host (self, host, hosts) :
attrs = (
@@ -174,16 +198,6 @@
html.a(href=self.url(ListHandler))(html('«'), 'Back'),
)
-class IndexHandler (BaseHandler) :
- def process (self) :
- self.hosts = self.query()
-
- def title (self) :
- return "DHCP Hosts"
-
- def render (self) :
- return self.render_hosts(self.hosts)
-
class ItemHandler (BaseHandler) :
def process (self, id) :
self.hosts = self.query()
@@ -202,7 +216,9 @@
class ListHandler (BaseHandler) :
def process (self) :
- hosts = self.query()
+ self.hosts = self.query()
+
+ # filter?
self.filters = {}
for attr in self.HOST_ATTRS :
@@ -212,14 +228,24 @@
continue
# preprocess
- if attr == 'mac' :
+ like = False
+
+ if value.endswith('*') :
+ like = value.replace('*', '%')
+
+ elif attr == 'mac' :
value = Host.normalize_mac(value)
# filter
- hosts = hosts.filter(self.HOST_ATTRS[attr] == value)
+ col = self.HOST_ATTRS[attr]
+
+ if like :
+ filter = (col.like(like))
+ else :
+ filter = (col == value)
+
+ self.hosts = self.hosts.filter(filter)
self.filters[attr] = value
-
- self.hosts = hosts
def title (self) :
if self.filters :
@@ -229,7 +255,7 @@
def render (self) :
return (
- self.render_hosts(self.hosts),
+ self.render_hosts(self.hosts, filters=self.filters),
html.a(href=self.url())(html('«'), 'Back') if self.filters else None,
)