refactor table views to share more code and to avoid empty filter table in some case
authorsylvain.thenault@logilab.fr
Wed, 28 Jan 2009 16:28:44 +0100
changeset 497 68d4211518c5
parent 496 e25a3c2f5393
child 500 14befdd50126
refactor table views to share more code and to avoid empty filter table in some case
common/view.py
web/views/tableview.py
--- a/common/view.py	Wed Jan 28 15:28:32 2009 +0100
+++ b/common/view.py	Wed Jan 28 16:28:44 2009 +0100
@@ -2,7 +2,7 @@
 
 
 :organization: Logilab
-:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
--- a/web/views/tableview.py	Wed Jan 28 15:28:32 2009 +0100
+++ b/web/views/tableview.py	Wed Jan 28 16:28:44 2009 +0100
@@ -24,11 +24,35 @@
     id = 'table'
     title = _('table')
     finalview = 'final'
+
+    def form_filter(self, divid, displaycols, displayactions, displayfilter,
+                    hidden=True):
+        rqlst = self.rset.syntax_tree()
+        # union not yet supported
+        if len(rqlst.children) != 1:
+            return ()
+        rqlst.save_state()
+        mainvar, baserql = prepare_facets_rqlst(rqlst, self.rset.args)
+        facets = [facet for facet in self.vreg.possible_vobjects(
+            'facets', self.req, self.rset, context='tablefilter',
+            filtered_variable=mainvar)
+                  if facet.get_widget()]
+        rqlst.recover()
+        if facets:
+            self._generate_form(divid, baserql, facets, hidden,
+                               vidargs={'displaycols': displaycols,
+                                        'displayactions': displayactions,
+                                        'displayfilter': displayfilter})
+            return self.show_hide_actions(divid, not hidden)
+        return ()
     
-    def generate_form(self, divid, baserql, facets, hidden=True, vidargs={}):
+    def _generate_form(self, divid, baserql, facets, hidden=True, vidargs={}):
         """display a form to filter table's content. This should only
         occurs when a context eid is given
         """
+        wdgs = [facet.get_widget() for facet in facets]
+        if not wdgs:
+            return
         self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js'))
         # drop False / None values from vidargs
         vidargs = dict((k, v) for k, v in vidargs.iteritems() if v)
@@ -40,11 +64,9 @@
         self.w(u'<table class="filter">\n')
         self.w(u'<tr>\n')
         for facet in facets:
-            wdg = facet.get_widget()
-            if wdg is not None:
-                self.w(u'<td>')
-                wdg.render(w=self.w)
-                self.w(u'</td>\n')
+            self.w(u'<td>')
+            facet.get_widget().render(w=self.w)
+            self.w(u'</td>\n')
         self.w(u'</tr>\n')
         self.w(u'</table>\n')
         self.w(u'</fieldset>\n')
@@ -117,22 +139,8 @@
             if title:
                 self.w(u'<h2 class="tableTitle">%s</h2>\n' % title)
             if displayfilter:
-                rqlst.save_state()
-                try:
-                    mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args)
-                except NotImplementedError:
-                    # UNION query
-                    facets = None
-                else:
-                    facets = list(self.vreg.possible_vobjects('facets', req, rset,
-                                                              context='tablefilter',
-                                                              filtered_variable=mainvar))
-                    self.generate_form(divid, baserql, facets, hidden,
-                                       vidargs={'displaycols': displaycols,
-                                                'displayfilter': displayfilter,
-                                                'displayactions': displayactions})
-                    actions += self.show_hide_actions(divid, not hidden)
-                rqlst.recover()
+                actions += self.form_filter(divid, displaycols, displayfilter,
+                                            displayactions)
         elif displayfilter:
             actions += self.show_hide_actions(divid, True)
         self.w(u'<div id="%s"' % divid)
@@ -308,22 +316,9 @@
             self.w(u'<h2>%s</h2>\n' % title)
         mainindex = self.main_var_index()
         if mainindex is not None:
-            rqlst = self.rset.syntax_tree()
-            # union not yet supported
-            if len(rqlst.children) == 1:
-                rqlst.save_state()
-                mainvar, baserql = prepare_facets_rqlst(rqlst, self.rset.args)
-                facets = list(self.vreg.possible_vobjects('facets', self.req, self.rset,
-                                                          context='tablefilter',
-                                                          filtered_variable=mainvar))
-                
-                if facets:
-                    self.generate_form(divid, baserql, facets, 
-                                       vidargs={'displaycols': displaycols,
-                                                'displayactions': displayactions,
-                                                'displayfilter': True})
-                    actions = self.show_hide_actions(divid, False)
-                rqlst.recover()
+            actions = self.form_filter(divid, displaycols, displayactions, True)
+        else:
+            actions = ()
         if not subvid and 'subvid' in self.req.form:
             subvid = self.req.form.pop('subvid')
         self.view('table', self.req.execute(actrql),