hosts: implement filtering in ListHandler
authorTero Marttila <terom@paivola.fi>
Thu, 11 Oct 2012 01:11:33 +0300
changeset 9 3334d8ddf2f1
parent 8 f64c44640b15
child 10 513eb70e54f2
hosts: implement filtering in ListHandler
pvl/verkko/hosts.py
static/style.css
--- 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('&laquo;'), '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('&laquo;'), 'Back') if self.filters else None,
         )
--- a/static/style.css	Thu Oct 11 00:44:08 2012 +0300
+++ b/static/style.css	Thu Oct 11 01:11:33 2012 +0300
@@ -41,6 +41,31 @@
     background-color: #d0d0d0;
 }
 
+/* Filter */
+tr.filter td
+{
+    padding: 0px;
+}
+
+tr.filter td input
+{
+    width: 100%;
+    margin: 0px;
+    border: 0px;
+
+    background-color: #d0d0d0;
+}
+
+tr.filter td input:hover
+{
+    background-color: #e0e0e0;
+}
+
+tr.filter td input:focus
+{
+    background-color: #ffffff;
+}
+
 /* Rows */
 tr
 {