web/views/tableview.py
changeset 497 68d4211518c5
parent 469 365826a58664
child 513 907c18c01c60
equal deleted inserted replaced
496:e25a3c2f5393 497:68d4211518c5
    22 
    22 
    23 class TableView(AnyRsetView):
    23 class TableView(AnyRsetView):
    24     id = 'table'
    24     id = 'table'
    25     title = _('table')
    25     title = _('table')
    26     finalview = 'final'
    26     finalview = 'final'
    27     
    27 
    28     def generate_form(self, divid, baserql, facets, hidden=True, vidargs={}):
    28     def form_filter(self, divid, displaycols, displayactions, displayfilter,
       
    29                     hidden=True):
       
    30         rqlst = self.rset.syntax_tree()
       
    31         # union not yet supported
       
    32         if len(rqlst.children) != 1:
       
    33             return ()
       
    34         rqlst.save_state()
       
    35         mainvar, baserql = prepare_facets_rqlst(rqlst, self.rset.args)
       
    36         facets = [facet for facet in self.vreg.possible_vobjects(
       
    37             'facets', self.req, self.rset, context='tablefilter',
       
    38             filtered_variable=mainvar)
       
    39                   if facet.get_widget()]
       
    40         rqlst.recover()
       
    41         if facets:
       
    42             self._generate_form(divid, baserql, facets, hidden,
       
    43                                vidargs={'displaycols': displaycols,
       
    44                                         'displayactions': displayactions,
       
    45                                         'displayfilter': displayfilter})
       
    46             return self.show_hide_actions(divid, not hidden)
       
    47         return ()
       
    48     
       
    49     def _generate_form(self, divid, baserql, facets, hidden=True, vidargs={}):
    29         """display a form to filter table's content. This should only
    50         """display a form to filter table's content. This should only
    30         occurs when a context eid is given
    51         occurs when a context eid is given
    31         """
    52         """
       
    53         wdgs = [facet.get_widget() for facet in facets]
       
    54         if not wdgs:
       
    55             return
    32         self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js'))
    56         self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js'))
    33         # drop False / None values from vidargs
    57         # drop False / None values from vidargs
    34         vidargs = dict((k, v) for k, v in vidargs.iteritems() if v)
    58         vidargs = dict((k, v) for k, v in vidargs.iteritems() if v)
    35         self.w(u'<form method="post" cubicweb:facetargs="%s" action="">' %
    59         self.w(u'<form method="post" cubicweb:facetargs="%s" action="">' %
    36                html_escape(dumps([divid, 'table', False, vidargs])))
    60                html_escape(dumps([divid, 'table', False, vidargs])))
    38         self.w(u'<input type="hidden" name="divid" value="%s" />' % divid)
    62         self.w(u'<input type="hidden" name="divid" value="%s" />' % divid)
    39         filter_hiddens(self.w, facets=','.join(facet.id for facet in facets), baserql=baserql)
    63         filter_hiddens(self.w, facets=','.join(facet.id for facet in facets), baserql=baserql)
    40         self.w(u'<table class="filter">\n')
    64         self.w(u'<table class="filter">\n')
    41         self.w(u'<tr>\n')
    65         self.w(u'<tr>\n')
    42         for facet in facets:
    66         for facet in facets:
    43             wdg = facet.get_widget()
    67             self.w(u'<td>')
    44             if wdg is not None:
    68             facet.get_widget().render(w=self.w)
    45                 self.w(u'<td>')
    69             self.w(u'</td>\n')
    46                 wdg.render(w=self.w)
       
    47                 self.w(u'</td>\n')
       
    48         self.w(u'</tr>\n')
    70         self.w(u'</tr>\n')
    49         self.w(u'</table>\n')
    71         self.w(u'</table>\n')
    50         self.w(u'</fieldset>\n')
    72         self.w(u'</fieldset>\n')
    51         self.w(u'</form>\n')
    73         self.w(u'</form>\n')
    52 
    74 
   115             if not title and 'title' in req.form:
   137             if not title and 'title' in req.form:
   116                 title = req.form['title']
   138                 title = req.form['title']
   117             if title:
   139             if title:
   118                 self.w(u'<h2 class="tableTitle">%s</h2>\n' % title)
   140                 self.w(u'<h2 class="tableTitle">%s</h2>\n' % title)
   119             if displayfilter:
   141             if displayfilter:
   120                 rqlst.save_state()
   142                 actions += self.form_filter(divid, displaycols, displayfilter,
   121                 try:
   143                                             displayactions)
   122                     mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args)
       
   123                 except NotImplementedError:
       
   124                     # UNION query
       
   125                     facets = None
       
   126                 else:
       
   127                     facets = list(self.vreg.possible_vobjects('facets', req, rset,
       
   128                                                               context='tablefilter',
       
   129                                                               filtered_variable=mainvar))
       
   130                     self.generate_form(divid, baserql, facets, hidden,
       
   131                                        vidargs={'displaycols': displaycols,
       
   132                                                 'displayfilter': displayfilter,
       
   133                                                 'displayactions': displayactions})
       
   134                     actions += self.show_hide_actions(divid, not hidden)
       
   135                 rqlst.recover()
       
   136         elif displayfilter:
   144         elif displayfilter:
   137             actions += self.show_hide_actions(divid, True)
   145             actions += self.show_hide_actions(divid, True)
   138         self.w(u'<div id="%s"' % divid)
   146         self.w(u'<div id="%s"' % divid)
   139         if displayactions:
   147         if displayactions:
   140             for action in self.vreg.possible_actions(req, self.rset).get('mainactions', ()):
   148             for action in self.vreg.possible_actions(req, self.rset).get('mainactions', ()):
   306             title = self.req.form.pop('title')
   314             title = self.req.form.pop('title')
   307         if title:
   315         if title:
   308             self.w(u'<h2>%s</h2>\n' % title)
   316             self.w(u'<h2>%s</h2>\n' % title)
   309         mainindex = self.main_var_index()
   317         mainindex = self.main_var_index()
   310         if mainindex is not None:
   318         if mainindex is not None:
   311             rqlst = self.rset.syntax_tree()
   319             actions = self.form_filter(divid, displaycols, displayactions, True)
   312             # union not yet supported
   320         else:
   313             if len(rqlst.children) == 1:
   321             actions = ()
   314                 rqlst.save_state()
       
   315                 mainvar, baserql = prepare_facets_rqlst(rqlst, self.rset.args)
       
   316                 facets = list(self.vreg.possible_vobjects('facets', self.req, self.rset,
       
   317                                                           context='tablefilter',
       
   318                                                           filtered_variable=mainvar))
       
   319                 
       
   320                 if facets:
       
   321                     self.generate_form(divid, baserql, facets, 
       
   322                                        vidargs={'displaycols': displaycols,
       
   323                                                 'displayactions': displayactions,
       
   324                                                 'displayfilter': True})
       
   325                     actions = self.show_hide_actions(divid, False)
       
   326                 rqlst.recover()
       
   327         if not subvid and 'subvid' in self.req.form:
   322         if not subvid and 'subvid' in self.req.form:
   328             subvid = self.req.form.pop('subvid')
   323             subvid = self.req.form.pop('subvid')
   329         self.view('table', self.req.execute(actrql),
   324         self.view('table', self.req.execute(actrql),
   330                   'noresult', w=self.w, displayfilter=False, subvid=subvid,
   325                   'noresult', w=self.w, displayfilter=False, subvid=subvid,
   331                   displayactions=displayactions, displaycols=displaycols,
   326                   displayactions=displayactions, displaycols=displaycols,