web/views/facets.py
changeset 2381 caad2367d940
parent 2293 7ded2a1416e4
parent 2340 65445ae67db0
child 2650 18aec79ec3a3
equal deleted inserted replaced
2374:ea1a44e4ad62 2381:caad2367d940
     7 """
     7 """
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 from simplejson import dumps
    10 from simplejson import dumps
    11 
    11 
    12 from logilab.mtconverter import html_escape
    12 from logilab.mtconverter import xml_escape
    13 
    13 
    14 from cubicweb.vregistry import objectify_selector
    14 from cubicweb.vregistry import objectify_selector
    15 from cubicweb.selectors import (non_final_entity, two_lines_rset,
    15 from cubicweb.selectors import (non_final_entity, two_lines_rset,
    16                                 match_context_prop, yes, relation_possible)
    16                                 match_context_prop, yes, relation_possible)
    17 from cubicweb.web.box import BoxTemplate
    17 from cubicweb.web.box import BoxTemplate
    18 from cubicweb.web.facet import (AbstractFacet, FacetStringWidget, RelationFacet,
    18 from cubicweb.web.facet import (AbstractFacet, FacetStringWidget, RelationFacet,
    19                                 prepare_facets_rqlst, filter_hiddens)
    19                                 prepare_facets_rqlst, filter_hiddens, _cleanup_rqlst,
       
    20                                 _prepare_vocabulary_rqlst)
    20 
    21 
    21 @objectify_selector
    22 @objectify_selector
    22 def contextview_selector(cls, req, rset=None, row=None, col=None, view=None,
    23 def contextview_selector(cls, req, rset=None, row=None, col=None, view=None,
    23                          **kwargs):
    24                          **kwargs):
    24     if view and getattr(view, 'filter_box_context_info', lambda: None)():
    25     if view and getattr(view, 'filter_box_context_info', lambda: None)():
    39     roundcorners = True
    40     roundcorners = True
    40 
    41 
    41     needs_css = 'cubicweb.facets.css'
    42     needs_css = 'cubicweb.facets.css'
    42     needs_js = ('cubicweb.ajax.js', 'cubicweb.formfilter.js')
    43     needs_js = ('cubicweb.ajax.js', 'cubicweb.formfilter.js')
    43 
    44 
    44     bkLinkBox_template = u'<div class="facetTitle">%s</div>'
    45     bk_linkbox_template = u'<div class="facetTitle">%s</div>'
    45 
    46 
    46     def facetargs(self):
    47     def facetargs(self):
    47         """this method returns the list of extra arguments that should
    48         """this method returns the list of extra arguments that should
    48         be used by the facet
    49         be used by the facet
    49         """
    50         """
    80                     wdg = facet.get_widget()
    81                     wdg = facet.get_widget()
    81                     if wdg is not None:
    82                     if wdg is not None:
    82                         widgets.append(wdg)
    83                         widgets.append(wdg)
    83             if not widgets:
    84             if not widgets:
    84                 return
    85                 return
    85             self.displayBookmarkLink(rset)
    86             if self.bk_linkbox_template:
       
    87                 self.display_bookmark_link(rset)
    86             w = self.w
    88             w = self.w
    87             w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">'  % (
    89             w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">'  % (
    88                 divid, html_escape(dumps([divid, vid, paginate, self.facetargs()]))))
    90                 divid, xml_escape(dumps([divid, vid, paginate, self.facetargs()]))))
    89             w(u'<fieldset>')
    91             w(u'<fieldset>')
    90             hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets),
    92             hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets),
    91                        'baserql': baserql}
    93                        'baserql': baserql}
    92             for param in ('subvid', 'vtitle'):
    94             for param in ('subvid', 'vtitle'):
    93                 if param in req.form:
    95                 if param in req.form:
    99         finally:
   101         finally:
   100             rqlst.recover()
   102             rqlst.recover()
   101             import cubicweb
   103             import cubicweb
   102             cubicweb.info('after facets with rql: %s' % repr(rqlst))
   104             cubicweb.info('after facets with rql: %s' % repr(rqlst))
   103 
   105 
   104     def displayBookmarkLink(self, rset):
   106     def display_bookmark_link(self, rset):
   105         eschema = self.schema.eschema('Bookmark')
   107         eschema = self.schema.eschema('Bookmark')
   106         if eschema.has_perm(self.req, 'add'):
   108         if eschema.has_perm(self.req, 'add'):
   107             bk_path = 'view?rql=%s' % rset.printable_rql()
   109             bk_path = 'view?rql=%s' % rset.printable_rql()
   108             bk_title = self.req._('my custom search')
   110             bk_title = self.req._('my custom search')
   109             linkto = 'bookmarked_by:%s:subject' % self.req.user.eid
   111             linkto = 'bookmarked_by:%s:subject' % self.req.user.eid
   110             bk_add_url = self.build_url('add/Bookmark', path=bk_path, title=bk_title, __linkto=linkto)
   112             bk_add_url = self.build_url('add/Bookmark', path=bk_path, title=bk_title, __linkto=linkto)
   111             bk_base_url = self.build_url('add/Bookmark', title=bk_title, __linkto=linkto)
   113             bk_base_url = self.build_url('add/Bookmark', title=bk_title, __linkto=linkto)
   112             bk_link = u'<a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a>' % (
   114             bk_link = u'<a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a>' % (
   113                     html_escape(bk_base_url),
   115                     xml_escape(bk_base_url),
   114                     html_escape(bk_add_url),
   116                     xml_escape(bk_add_url),
   115                     self.req._('bookmark this search'))
   117                     self.req._('bookmark this search'))
   116             self.w(self.bkLinkBox_template % bk_link)
   118             self.w(self.bk_linkbox_template % bk_link)
   117 
   119 
   118     def get_facets(self, rset, mainvar):
   120     def get_facets(self, rset, mainvar):
   119         return self.vreg.possible_vobjects('facets', self.req, rset=rset,
   121         return self.vreg.possible_vobjects('facets', self.req, rset=rset,
   120                                            context='facetbox',
   122                                            context='facetbox',
   121                                            filtered_variable=mainvar)
   123                                            filtered_variable=mainvar)
   160         value = self.req.form.get(self.id)
   162         value = self.req.form.get(self.id)
   161         if not value:
   163         if not value:
   162             return
   164             return
   163         self.rqlst.add_type_restriction(self.filtered_variable, value)
   165         self.rqlst.add_type_restriction(self.filtered_variable, value)
   164 
   166 
       
   167     def possible_values(self):
       
   168         """return a list of possible values (as string since it's used to
       
   169         compare to a form value in javascript) for this facet
       
   170         """
       
   171         rqlst = self.rqlst
       
   172         rqlst.save_state()
       
   173         try:
       
   174             _cleanup_rqlst(rqlst, self.filtered_variable)
       
   175             etype_var = _prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, self.role)
       
   176             attrvar = rqlst.make_variable()
       
   177             rqlst.add_selected(attrvar)
       
   178             rqlst.add_relation(etype_var, 'name', attrvar)
       
   179             return [etype for _, etype in self.rqlexec(rqlst.as_string())]
       
   180         finally:
       
   181             rqlst.recover()
   165 
   182 
   166 class HasTextFacet(AbstractFacet):
   183 class HasTextFacet(AbstractFacet):
   167     __select__ = relation_possible('has_text', 'subject') & match_context_prop()
   184     __select__ = relation_possible('has_text', 'subject') & match_context_prop()
   168     id = 'has_text-facet'
   185     id = 'has_text-facet'
   169     rtype = 'has_text'
   186     rtype = 'has_text'