[transaction] do not attempt to reconnect if there has been some write during the transaction
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""the facets box and some basic facets"""__docformat__="restructuredtext en"fromlogilab.mtconverterimportxml_escapefromcubicweb.appobjectimportobjectify_selectorfromcubicweb.selectorsimport(non_final_entity,multi_lines_rset,match_context_prop,yes,relation_possible)fromcubicweb.webimportdumpsfromcubicweb.web.boximportBoxTemplatefromcubicweb.web.facetimport(AbstractFacet,FacetStringWidget,RelationFacet,prepare_facets_rqlst,filter_hiddens,_cleanup_rqlst,_prepare_vocabulary_rqlst)@objectify_selectordefcontextview_selector(cls,req,rset=None,row=None,col=None,view=None,**kwargs):ifviewandgetattr(view,'filter_box_context_info',lambda:None)():return1return0classFilterBox(BoxTemplate):"""filter results of a query"""__regid__='filter_box'__select__=(((non_final_entity()&multi_lines_rset())|contextview_selector())&match_context_prop())context='left'title=_('boxes_filter_box')visible=True# functionality provided by the search box by defaultorder=1roundcorners=Trueneeds_css='cubicweb.facets.css'needs_js=('cubicweb.ajax.js','cubicweb.facets.js')bk_linkbox_template=u'<div class="facetTitle">%s</div>'deffacetargs(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.cw_rsetvid,divid=None,'pageContent'paginate=viewandview.paginablereturnrset,vid,divid,paginatedefcall(self,view=None):req=self._cwreq.add_js(self.needs_js)req.add_css(self.needs_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 hijackedreturnrqlst=rset.syntax_tree()# union not yet supportediflen(rqlst.children)!=1:return()rqlst=rqlst.copy()req.vreg.rqlhelper.annotate(rqlst)mainvar,baserql=prepare_facets_rqlst(rqlst,rset.args)widgets=[]forfacetinself.get_facets(rset,rqlst.children[0],mainvar):iffacet.cw_propval('visible'):wdg=facet.get_widget()ifwdgisnotNone:widgets.append(wdg)ifnotwidgets:returnifvidisNone:vid=req.form.get('vid')ifself.bk_linkbox_template:self.display_bookmark_link(rset)w=self.ww(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">'%(divid,xml_escape(dumps([divid,vid,paginate,self.facetargs()]))))w(u'<fieldset>')hiddens={'facets':','.join(wdg.facet.__regid__forwdginwidgets),'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')defdisplay_bookmark_link(self,rset):eschema=self._cw.vreg.schema.eschema('Bookmark')ifeschema.has_perm(self._cw,'add'):bk_path='rql=%s'%self._cw.url_quote(rset.printable_rql())ifself._cw.form.get('vid'):bk_path+='&vid=%s'%self._cw.url_quote(self._cw.form['vid'])bk_path='view?'+bk_pathbk_title=self._cw._('my custom search')linkto='bookmarked_by:%s:subject'%self._cw.user.eidbk_add_url=self._cw.build_url('add/Bookmark',path=bk_path,title=bk_title,__linkto=linkto)bk_base_url=self._cw.build_url('add/Bookmark',title=bk_title,__linkto=linkto)bk_link=u'<a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a>'%(xml_escape(bk_base_url),xml_escape(bk_add_url),self._cw._('bookmark this search'))self.w(self.bk_linkbox_template%bk_link)defget_facets(self,rset,rqlst,mainvar):returnself._cw.vreg['facets'].poss_visible_objects(self._cw,rset=rset,rqlst=rqlst,context='facetbox',filtered_variable=mainvar)# facets ######################################################################classCreatedByFacet(RelationFacet):__regid__='created_by-facet'rtype='created_by'target_attr='login'classInGroupFacet(RelationFacet):__regid__='in_group-facet'rtype='in_group'target_attr='name'classInStateFacet(RelationFacet):__regid__='in_state-facet'rtype='in_state'target_attr='name'# inherit from RelationFacet to benefit from its possible_values implementationclassETypeFacet(RelationFacet):__regid__='etype-facet'__select__=yes()order=1rtype='is'target_attr='name'@propertydeftitle(self):returnself._cw._('entity type')defvocabulary(self):"""return vocabulary for this facet, eg a list of 2-uple (label, value) """etypes=self.cw_rset.column_types(0)returnsorted((self._cw._(etype),etype)foretypeinetypes)defadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self._cw.form.get(self.__regid__)ifnotvalue:returnself.rqlst.add_type_restriction(self.filtered_variable,value)defpossible_values(self):"""return a list of possible values (as string since it's used to compare to a form value in javascript) for this facet """rqlst=self.rqlstrqlst.save_state()try:_cleanup_rqlst(rqlst,self.filtered_variable)etype_var=_prepare_vocabulary_rqlst(rqlst,self.filtered_variable,self.rtype,self.role)attrvar=rqlst.make_variable()rqlst.add_selected(attrvar)rqlst.add_relation(etype_var,'name',attrvar)return[etypefor_,etypeinself.rqlexec(rqlst.as_string())]finally:rqlst.recover()classHasTextFacet(AbstractFacet):__select__=relation_possible('has_text','subject')&match_context_prop()__regid__='has_text-facet'rtype='has_text'role='subject'order=0@propertydeftitle(self):returnself._cw._('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._cw.form.get(self.__regid__)ifnotvalue:returnself.rqlst.add_constant_restriction(self.filtered_variable,'has_text',value,'String')