"""the facets box and some basic facets:organization: Logilab:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromsimplejsonimportdumpsfromlogilab.mtconverterimporthtml_escapefromcubicweb.vregistryimportobjectify_selectorfromcubicweb.selectorsimport(chainfirst,chainall,non_final_entity,two_lines_rset,match_context_prop,yes,relation_possible)fromcubicweb.web.boximportBoxTemplatefromcubicweb.web.facetimport(AbstractFacet,VocabularyFacet,FacetStringWidget,RelationFacet,prepare_facets_rqlst,filter_hiddens)@objectify_selectordefcontextview_selector(cls,req,rset,row=None,col=None,view=None,**kwargs):ifviewandgetattr(view,'filter_box_context_info',lambda:None)():return1return0classFilterBox(BoxTemplate):"""filter results of a query"""id='filter_box'__select__=(((non_final_entity()&two_lines_rset())|contextview_selector())&match_context_prop())context='left'title=_('boxes_filter_box')visible=True# functionality provided by the search box by defaultorder=1roundcorners=Truedeffacetargs(self):"""this method returns the list of extra arguments that should be used by the facet """return{}def_get_context(self,view):context=getattr(view,'filter_box_context_info',lambda:None)()ifcontext:rset,vid,divid,paginate=contextelse:rset=self.rsetvid,divid=None,'pageContent'paginate=viewandview.need_navigationreturnrset,vid,divid,paginatedefcall(self,view=None):req=self.reqreq.add_js(('cubicweb.ajax.js','cubicweb.formfilter.js'))req.add_css('cubicweb.facets.css')ifself.roundcorners:req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");')rset,vid,divid,paginate=self._get_context(view)ifrset.rowcount<2:# XXX done by selectors, though maybe necessary when rset has been hijackedreturnifvidisNone:vid=req.form.get('vid')rqlst=rset.syntax_tree()rqlst.save_state()try:mainvar,baserql=prepare_facets_rqlst(rqlst,rset.args)widgets=[]forfacetinself.get_facets(rset,mainvar):iffacet.propval('visible'):wdg=facet.get_widget()ifwdgisnotNone:widgets.append(wdg)ifnotwidgets:returnw=self.weschema=self.schema.eschema('Bookmark')ifeschema.has_perm(req,'add'):bk_path='view?rql=%s'%rset.printable_rql()bk_title=req._('my custom search')linkto='bookmarked_by:%s:subject'%self.req.user.eidbk_add_url=self.build_url('add/Bookmark',path=bk_path,title=bk_title,__linkto=linkto)bk_base_url=self.build_url('add/Bookmark',title=bk_title,__linkto=linkto)w(u'<div class="facetTitle"><a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a></div>'%(html_escape(bk_base_url),html_escape(bk_add_url),req._('bookmark this search')))w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">'%(divid,html_escape(dumps([divid,vid,paginate,self.facetargs()]))))w(u'<fieldset>')hiddens={'facets':','.join(wdg.facet.idforwdginwidgets),'baserql':baserql}forparamin('subvid','vtitle'):ifparaminreq.form:hiddens[param]=req.form[param]filter_hiddens(w,**hiddens)forwdginwidgets:wdg.render(w=self.w)w(u'</fieldset>\n</form>\n')finally:rqlst.recover()importcubicwebcubicweb.info('after facets with rql: %s'%repr(rqlst))defget_facets(self,rset,mainvar):returnself.vreg.possible_vobjects('facets',self.req,rset,context='facetbox',filtered_variable=mainvar)# facets ######################################################################classCreatedByFacet(RelationFacet):id='created_by-facet'rtype='created_by'target_attr='login'classInGroupFacet(RelationFacet):id='in_group-facet'rtype='in_group'target_attr='name'classInStateFacet(RelationFacet):id='in_state-facet'rtype='in_state'target_attr='name'# inherit from RelationFacet to benefit from its possible_values implementationclassETypeFacet(RelationFacet):id='etype-facet'__select__=yes()order=1rtype='is'target_attr='name'@propertydeftitle(self):returnself.req._('entity type')defvocabulary(self):"""return vocabulary for this facet, eg a list of 2-uple (label, value) """etypes=self.rset.column_types(0)returnsorted((self.req._(etype),etype)foretypeinetypes)defadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self.req.form.get(self.id)ifnotvalue:returnself.rqlst.add_type_restriction(self.filtered_variable,value)classHasTextFacet(AbstractFacet):__select__=relation_possible('has_text','subject')&match_context_prop()id='has_text-facet'rtype='has_text'role='subject'order=0@propertydeftitle(self):returnself.req._('has_text')defget_widget(self):"""return the widget instance to use to display this facet default implentation expects a .vocabulary method on the facet and return a combobox displaying this vocabulary """returnFacetStringWidget(self)defadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self.req.form.get(self.id)ifnotvalue:returnself.rqlst.add_constant_restriction(self.filtered_variable,'has_text',value,'String')