[devtools] bases for instrumenting / debugging standard propagation hooks
* a class that helps writing command to check propagation, detecting standard
errors and picture the propagation as a graph
* a class to instrument sets used to configure standard propagation hooks
closes #3171980
# copyright 2003-2012 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 :mod:`cubicweb.web.facet` module contains a set of abstract classes to useas bases to build your own facetsAll facet classes inherits from the :class:`AbstractFacet` class, though you'llusually find some more handy class that do what you want.Let's see available classes.Classes you'll want to use--------------------------.. autoclass:: cubicweb.web.facet.RelationFacet.. autoclass:: cubicweb.web.facet.RelationAttributeFacet.. autoclass:: cubicweb.web.facet.HasRelationFacet.. autoclass:: cubicweb.web.facet.AttributeFacet.. autoclass:: cubicweb.web.facet.RQLPathFacet.. autoclass:: cubicweb.web.facet.RangeFacet.. autoclass:: cubicweb.web.facet.DateRangeFacet.. autoclass:: cubicweb.web.facet.BitFieldFacetClasses for facets implementor------------------------------Unless you didn't find the class that does the job you want above, you may wantto skip those classes..... autoclass:: cubicweb.web.facet.AbstractFacet.. autoclass:: cubicweb.web.facet.VocabularyFacet.. comment: XXX widgets"""__docformat__="restructuredtext en"_=unicodefromfunctoolsimportreducefromwarningsimportwarnfromcopyimportdeepcopyfromdatetimeimportdatetime,timedeltafromlogilab.mtconverterimportxml_escapefromlogilab.common.graphimporthas_pathfromlogilab.common.decoratorsimportcached,cachedpropertyfromlogilab.common.dateimportdatetime2ticks,ustrftime,ticks2datetimefromlogilab.common.compatimportallfromlogilab.common.deprecationimportdeprecatedfromlogilab.common.registryimportyesfromrqlimportnodes,utilsfromcubicwebimportUnauthorizedfromcubicweb.schemaimportdisplay_namefromcubicweb.uilibimportcss_em_num_value,domidfromcubicweb.utilsimportmake_uidfromcubicweb.predicatesimportmatch_context_prop,partial_relation_possiblefromcubicweb.appobjectimportAppObjectfromcubicweb.webimportRequestError,htmlwidgetsdefrtype_facet_title(facet):iffacet.cw_rset:ptypes=facet.cw_rset.column_types(0)iflen(ptypes)==1:returndisplay_name(facet._cw,facet.rtype,form=facet.role,context=iter(ptypes).next())returndisplay_name(facet._cw,facet.rtype,form=facet.role)defget_facet(req,facetid,select,filtered_variable):returnreq.vreg['facets'].object_by_id(facetid,req,select=select,filtered_variable=filtered_variable)@deprecated('[3.13] filter_hiddens moved to cubicweb.web.views.facets with ''slightly modified prototype')deffilter_hiddens(w,baserql,**kwargs):fromcubicweb.web.views.facetsimportfilter_hiddensreturnfilter_hiddens(w,baserql,wdgs=kwargs.pop('facets'),**kwargs)## rqlst manipulation functions used by facets ################################definit_facets(rset,select,mainvar=None):"""Alters in place the <select> for filtering and returns related data. Calls :func:`prepare_select` to prepare the syntaxtree for selection and :func:`get_filtered_variable` that selects the variable to be filtered and drops several parts of the select tree. See each function docstring for details. :param rset: ResultSet we init facet for. :type rset: :class:`~cubicweb.rset.ResultSet` :param select: Select statement to be *altered* to support filtering. :type select: :class:`~rql.stmts.Select` from the ``rset`` parameters. :param mainvar: Name of the variable we want to filter with facets. :type mainvar: string :rtype: (filtered_variable, baserql) tuple. :return filtered_variable: A rql class:`~rql.node.VariableRef` instance as returned by :func:`get_filtered_variable`. :return baserql: A string containing the rql before :func:`prepare_select` but after :func:`get_filtered_variable`. """rset.req.vreg.rqlhelper.annotate(select)filtered_variable=get_filtered_variable(select,mainvar)baserql=select.as_string(kwargs=rset.args)# before call to prepare_selectprepare_select(select,filtered_variable)returnfiltered_variable,baserqldefget_filtered_variable(select,mainvar=None):"""drop any limit/offset from select (in-place modification) and return the variable whose name is `mainvar` or the first variable selected in column 0 """select.set_limit(None)select.set_offset(None)ifmainvarisNone:vref=select.selection[0].iget_nodes(nodes.VariableRef).next()returnvref.variablereturnselect.defined_vars[mainvar]defprepare_select(select,filtered_variable):"""prepare a syntax tree to generate facet filters * remove ORDERBY/GROUPBY clauses * cleanup selection (remove everything) * undefine unnecessary variables * set DISTINCT Notice unset of LIMIT/OFFSET us expected to be done by a previous call to :func:`get_filtered_variable`. """# cleanup sort terms / group byselect.remove_sort_terms()select.remove_groups()# XXX remove aggregat from having# selection: only vocabulary entityforterminselect.selection[:]:select.remove_selected(term)# remove unbound variables which only have some type restrictionfordvarinlist(select.defined_vars.itervalues()):ifnot(dvarisfiltered_variableordvar.stinfo['relations']):select.undefine_variable(dvar)# global tree config: DISTINCT, LIMIT, OFFSETselect.set_distinct(True)@deprecated('[3.13] use init_facets instead')defprepare_facets_rqlst(rqlst,args=None):assertlen(rqlst.children)==1,'FIXME: union not yet supported'select=rqlst.children[0]filtered_variable=get_filtered_variable(select)baserql=select.as_string(args)prepare_select(select,filtered_variable)returnfiltered_variable,baserqldefprepare_vocabulary_select(select,filtered_variable,rtype,role,select_target_entity=True):"""prepare a syntax tree to generate a filter vocabulary rql using the given relation: * create a variable to filter on this relation * add the relation * add the new variable to GROUPBY clause if necessary * add the new variable to the selection """newvar=_add_rtype_relation(select,filtered_variable,rtype,role)[0]ifselect_target_entity:# if select.groupby: XXX we remove groupby now# select.add_group_var(newvar)select.add_selected(newvar)# add is restriction if necessaryiffiltered_variable.stinfo['typerel']isNone:etypes=frozenset(sol[filtered_variable.name]forsolinselect.solutions)select.add_type_restriction(filtered_variable,etypes)returnnewvardefinsert_attr_select_relation(select,filtered_variable,rtype,role,attrname,sortfuncname=None,sortasc=True,select_target_entity=True):"""modify a syntax tree to : * link a new variable to `filtered_variable` through `rtype` (where filtered_variable has `role`) * retrieve only the newly inserted variable and its `attrname` Sorting: * on `attrname` ascendant (`sortasc`=True) or descendant (`sortasc`=False) * on `sortfuncname`(`attrname`) if `sortfuncname` is specified * no sort if `sortasc` is None """cleanup_select(select,filtered_variable)var=prepare_vocabulary_select(select,filtered_variable,rtype,role,select_target_entity)attrvar=select.make_variable()select.add_relation(var,attrname,attrvar)# if query is grouped, we have to add the attribute variable#if select.groupby: XXX may not occur anymore# if not attrvar in select.groupby:# select.add_group_var(attrvar)ifsortascisnotNone:_set_orderby(select,attrvar,sortasc,sortfuncname)# add attribute variable to selectionselect.add_selected(attrvar)returnvardefcleanup_select(select,filtered_variable):"""cleanup tree from unnecessary restrictions: * attribute selection * optional relations linked to the main variable * mandatory relations linked to the main variable """ifselect.whereisNone:returnschema=select.root.schematoremove=set()vargraph=deepcopy(select.vargraph)# graph representing links between variableforrelinselect.where.get_nodes(nodes.Relation):ovar=_may_be_removed(rel,schema,filtered_variable)ifovarisnotNone:toremove.add(ovar)removed=set()whiletoremove:trvar=toremove.pop()trvarname=trvar.name# remove paths using this variable from the graphlinkedvars=vargraph.pop(trvarname)forovarnameinlinkedvars:vargraph[ovarname].remove(trvarname)# remove relation using this variableforrelintrvar.stinfo['relations']:ifrelinremoved:# already removedcontinueselect.remove_node(rel)removed.add(rel)rel=trvar.stinfo['typerel']ifrelisnotNoneandnotrelinremoved:select.remove_node(rel)removed.add(rel)# cleanup groupby clauseifselect.groupby:forvrefinselect.groupby[:]:ifvref.name==trvarname:select.remove_group_var(vref)# we can also remove all variables which are linked to this variable# and have no path to the main variableforovarnameinlinkedvars:ifovarname==filtered_variable.name:continueifnothas_path(vargraph,ovarname,filtered_variable.name):toremove.add(select.defined_vars[ovarname])def_may_be_removed(rel,schema,variable):"""if the given relation may be removed from the tree, return the variable on the other side of `variable`, else return None Conditions: * the relation is an attribute selection of the main variable * the relation is optional relation linked to the main variable * the relation is a mandatory relation linked to the main variable without any restriction on the other variable """lhs,rhs=rel.get_variable_parts()rschema=schema.rschema(rel.r_type)iflhs.variableisvariable:try:ovar=rhs.variableexceptAttributeError:# constant restriction# XXX: X title LOWER(T) if it makes sense?returnNoneifrschema.final:iflen(ovar.stinfo['relations'])==1 \andnotovar.stinfo.get('having'):# attribute selectionreturnovarreturnNoneopt='right'cardidx=0elifgetattr(rhs,'variable',None)isvariable:ovar=lhs.variableopt='left'cardidx=1else:# not directly linked to the main variablereturnNoneifrel.optionalin(opt,'both'):# optional relationreturnovarifall(rdef.cardinality[cardidx]in'1+'forrdefinrschema.rdefs.itervalues()):# mandatory relation without any restriction on the other variablefororelinovar.stinfo['relations']:ifrelisorel:continueif_may_be_removed(orel,schema,ovar)isNone:returnNonereturnovarreturnNonedef_make_relation(select,variable,rtype,role):newvar=select.make_variable()ifrole=='object':rel=nodes.make_relation(newvar,rtype,(variable,),nodes.VariableRef)else:rel=nodes.make_relation(variable,rtype,(newvar,),nodes.VariableRef)returnnewvar,reldef_add_rtype_relation(select,variable,rtype,role):"""add a relation relying `variable` to entities linked by the `rtype` relation (where `variable` has `role`) return the inserted variable for linked entities. """newvar,newrel=_make_relation(select,variable,rtype,role)select.add_restriction(newrel)returnnewvar,newreldef_remove_relation(select,rel,var):"""remove a constraint relation from the syntax tree"""# remove the relationselect.remove_node(rel)# remove relations where the filtered variable appears on the# lhs and rhs is a constant restrictionextra=[]forvrelinvar.stinfo['relations']:ifvrelisrel:continueifvrel.children[0].variableisvar:ifnotvrel.children[1].get_nodes(nodes.Constant):extra.append(vrel)select.remove_node(vrel)returnextradef_set_orderby(select,newvar,sortasc,sortfuncname):ifsortfuncnameisNone:select.add_sort_var(newvar,sortasc)else:vref=nodes.variable_ref(newvar)vref.register_reference()sortfunc=nodes.Function(sortfuncname)sortfunc.append(vref)term=nodes.SortTerm(sortfunc,sortasc)select.add_sort_term(term)def_get_var(select,varname,varmap):try:returnvarmap[varname]exceptKeyError:varmap[varname]=var=select.make_variable()returnvar_prepare_vocabulary_rqlst=deprecated('[3.13] renamed prepare_vocabulary_select')(prepare_vocabulary_select)_cleanup_rqlst=deprecated('[3.13] renamed to cleanup_select')(cleanup_select)## base facet classes ##########################################################classAbstractFacet(AppObject):"""Abstract base class for all facets. Facets are stored in their own 'facets' registry. They are similar to contextual components since the use the following configurable properties: * `visible`, boolean flag telling if a facet should be displayed or not * `order`, integer to control facets display order * `context`, telling if a facet should be displayed in the table form filter (context = 'tablefilter') or in the facet box (context = 'facetbox') or in both (context = '') The following methods define the facet API: .. automethod:: cubicweb.web.facet.AbstractFacet.get_widget .. automethod:: cubicweb.web.facet.AbstractFacet.add_rql_restrictions Facets will have the following attributes set (beside the standard :class:`~cubicweb.appobject.AppObject` ones): * `select`, the :class:`rql.stmts.Select` node of the rql syntax tree being filtered * `filtered_variable`, the variable node in this rql syntax tree that we're interested in filtering Facets implementors may also be interested in the following properties / methods: .. autoattribute:: cubicweb.web.facet.AbstractFacet.operator .. automethod:: cubicweb.web.facet.AbstractFacet.rqlexec """__abstract__=True__registry__='facets'cw_property_defs={_('visible'):dict(type='Boolean',default=True,help=_('display the facet or not')),_('order'):dict(type='Int',default=99,help=_('display order of the facet')),_('context'):dict(type='String',default='',# None <-> bothvocabulary=(_('tablefilter'),_('facetbox'),''),help=_('context where this facet should be displayed, ''leave empty for both')),}visible=Truecontext=''needs_update=Falsestart_unfolded=Trueallow_hide=Truecw_rset=None# ensure facets have a cw_rset attributedef__init__(self,req,select=None,filtered_variable=None,**kwargs):super(AbstractFacet,self).__init__(req,**kwargs)assertselectisnotNoneassertfiltered_variable# take care: facet may be retreived using `object_by_id` from an ajax call# or from `select` using the result set to filterself.select=selectself.filtered_variable=filtered_variabledef__repr__(self):return'<%s>'%self.__class__.__name__defget_widget(self):"""Return the widget instance to use to display this facet, or None if the facet can't do anything valuable (only one value in the vocabulary for instance). """raiseNotImplementedErrordefadd_rql_restrictions(self):"""When some facet criteria has been updated, this method is called to add restriction for this facet into the rql syntax tree. It should get back its value in form parameters, and modify the syntax tree (`self.select`) accordingly. """raiseNotImplementedError@propertydefoperator(self):"""Return the operator (AND or OR) to use for this facet when multiple values are selected. """# OR between selected values by defaultreturnself._cw.form.get(xml_escape(self.__regid__)+'_andor','OR')defrqlexec(self,rql,args=None):"""Utility method to execute some rql queries, and simply returning an empty list if :exc:`Unauthorized` is raised. """try:returnself._cw.execute(rql,args)exceptUnauthorized:return[]@propertydefwdgclass(self):raiseNotImplementedError@property@deprecated('[3.13] renamed .select')defrqlst(self):returnself.selectclassVocabularyFacet(AbstractFacet):"""This abstract class extend :class:`AbstractFacet` to use the :class:`FacetVocabularyWidget` as widget, suitable for facets that may restrict values according to a (usually computed) vocabulary. A class which inherits from VocabularyFacet must define at least these methods: .. automethod:: cubicweb.web.facet.VocabularyFacet.vocabulary .. automethod:: cubicweb.web.facet.VocabularyFacet.possible_values """needs_update=Truesupport_and=False@propertydefwdgclass(self):returnFacetVocabularyWidgetdefget_selected(self):returnfrozenset(int(eid)foreidinself._cw.list_form_param(self.__regid__))defget_widget(self):"""Return the widget instance to use to display this facet. This implementation expects a .vocabulary method on the facet and return a combobox displaying this vocabulary. """vocab=self.vocabulary()iflen(vocab)<=1:returnNonewdg=self.wdgclass(self)selected=self.get_selected()forlabel,valueinvocab:wdg.items.append((value,label,valueinselected))returnwdgdefvocabulary(self):"""Return vocabulary for this facet, eg a list of 2-uple (label, value). """raiseNotImplementedErrordefpossible_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. """raiseNotImplementedErrorclassRelationFacet(VocabularyFacet):"""Base facet to filter some entities according to other entities to which they are related. Create concrete facet by inheriting from this class an then configuring it by setting class attribute described below. The relation is defined by the `rtype` and `role` attributes. The `no_relation` boolean flag tells if a special 'no relation' value should be added (allowing to filter on entities which *do not* have the relation set). Default is computed according the relation's cardinality. The values displayed for related entities will be: * result of calling their `label_vid` view if specified * else their `target_attr` attribute value if specified * else their eid (you usually want something nicer...) When no `label_vid` is set, you will get translated value if `i18nable` is set. By default, `i18nable` will be set according to the schema, but you can force its value by setting it has a class attribute. You can filter out target entity types by specifying `target_type`. By default, vocabulary will be displayed sorted on `target_attr` value in an ascending way. You can control sorting with: * `sortfunc`: set this to a stored procedure name if you want to sort on the result of this function's result instead of direct value * `sortasc`: boolean flag to control ascendant/descendant sorting To illustrate this facet, let's take for example an *excerpt* of the schema of an office location search application: .. sourcecode:: python class Office(WorkflowableEntityType): price = Int(description='euros / m2 / HC / HT') surface = Int(description='m2') has_address = SubjectRelation('PostalAddress', cardinality='1?', composite='subject') proposed_by = SubjectRelation('Agency') We can simply define a facet to filter offices according to the agency proposing it: .. sourcecode:: python class AgencyFacet(RelationFacet): __regid__ = 'agency' # this facet should only be selected when visualizing offices __select__ = RelationFacet.__select__ & is_instance('Office') # this facet is a filter on the 'Agency' entities linked to the office # through the 'proposed_by' relation, where the office is the subject # of the relation rtype = 'has_address' # 'subject' is the default but setting it explicitly doesn't hurt... role = 'subject' # we want to display the agency's name target_attr = 'name' """__select__=partial_relation_possible()&match_context_prop()# class attributes to configure the relation facetrtype=Nonerole='subject'target_type=Nonetarget_attr='eid'# for subclasses parametrization, should not change if you want a# RelationFacettarget_attr_type='Int'restr_attr='eid'restr_attr_type='Int'comparator='='# could be '<', '<=', '>', '>='# set this to a stored procedure name if you want to sort on the result of# this function's result instead of direct valuesortfunc=None# ascendant/descendant sortingsortasc=True# if you want to call a view on the entity instead of using `target_attr`label_vid=None# internal purpose_select_target_entity=Truetitle=property(rtype_facet_title)no_relation_label=_('<no relation>')def__repr__(self):return'<%s on (%s-%s)>'%(self.__class__.__name__,self.rtype,self.role)# facet public API #########################################################defvocabulary(self):"""return vocabulary for this facet, eg a list of 2-uple (label, value) """select=self.selectselect.save_state()ifself.rql_sort:sort=self.sortascelse:sort=None# will be sorted on labeltry:var=insert_attr_select_relation(select,self.filtered_variable,self.rtype,self.role,self.target_attr,self.sortfunc,sort,self._select_target_entity)ifself.target_typeisnotNone:select.add_type_restriction(var,self.target_type)try:rset=self.rqlexec(select.as_string(),self.cw_rset.args)exceptException:self.exception('error while getting vocabulary for %s, rql: %s',self,select.as_string())return()finally:select.recover()# don't call rset_vocabulary on empty result set, it may be an empty# *list* (see rqlexec implementation)values=rsetandself.rset_vocabulary(rset)or[]ifself._include_no_relation():values.insert(0,(self._cw._(self.no_relation_label),''))returnvaluesdefpossible_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 """select=self.selectselect.save_state()try:cleanup_select(select,self.filtered_variable)ifself._select_target_entity:prepare_vocabulary_select(select,self.filtered_variable,self.rtype,self.role,select_target_entity=True)else:insert_attr_select_relation(select,self.filtered_variable,self.rtype,self.role,self.target_attr,select_target_entity=False)values=[unicode(x)forx,inself.rqlexec(select.as_string())]exceptException:self.exception('while computing values for %s',self)return[]finally:select.recover()ifself._include_no_relation():values.append('')returnvaluesdefadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self._cw.form.get(self.__regid__)ifvalueisNone:returnfiltered_variable=self.filtered_variablerestrvar,rel=_add_rtype_relation(self.select,filtered_variable,self.rtype,self.role)self.value_restriction(restrvar,rel,value)# internal control API #####################################################@propertydefi18nable(self):"""should label be internationalized"""ifself.target_type:eschema=self._cw.vreg.schema.eschema(self.target_type)elifself.role=='subject':eschema=self._cw.vreg.schema.rschema(self.rtype).objects()[0]else:eschema=self._cw.vreg.schema.rschema(self.rtype).subjects()[0]returngetattr(eschema.rdef(self.target_attr),'internationalizable',False)@propertydefno_relation(self):return(notself._cw.vreg.schema.rschema(self.rtype).finalandself._search_card('?*'))@propertydefrql_sort(self):"""return true if we can handle sorting in the rql query. E.g. if sortfunc is set or if we have not to transform the returned value (eg no label_vid and not i18nable) """returnself.sortfuncisnotNoneor(self.label_vidisNoneandnotself.i18nable)defrset_vocabulary(self,rset):ifself.i18nable:_=self._cw._else:_=unicodeifself.rql_sort:values=[(_(label),eid)foreid,labelinrset]else:ifself.label_vidisNone:values=[(_(label),eid)foreid,labelinrset]else:values=[(entity.view(self.label_vid),entity.eid)forentityinrset.entities()]values=sorted(values)ifnotself.sortasc:values=list(reversed(values))returnvalues@propertydefsupport_and(self):returnself._search_card('+*')# internal utilities #######################################################@cacheddef_support_and_compat(self):support=self.support_andifcallable(support):warn('[3.13] %s.support_and is now a property'%self.__class__,DeprecationWarning)support=support()returnsupportdefvalue_restriction(self,restrvar,rel,value):# XXX handle rel is None case in RQLPathFacet?ifself.restr_attr!='eid':self.select.set_distinct(True)ifisinstance(value,basestring):# only one value selectedifvalue:self.select.add_constant_restriction(restrvar,self.restr_attr,value,self.restr_attr_type)else:rel.parent.replace(rel,nodes.Not(rel))elifself.operator=='OR':# set_distinct only if rtype cardinality is > 1ifself._support_and_compat():self.select.set_distinct(True)# multiple ORed values: using IN is fineif''invalue:value.remove('')self._add_not_rel_restr(rel)self._and_restriction(rel,restrvar,value)else:# multiple values with AND operator. We've to generate a query like# "X relation A, A eid 1, X relation B, B eid 1", hence the new# relations at each iteration in the while loop below if''invalue:raiseRequestError("this doesn't make sense")self._and_restriction(rel,restrvar,value.pop())whilevalue:restrvar,rtrel=_make_relation(self.select,self.filtered_variable,self.rtype,self.role)ifrelisNone:self.select.add_restriction(rtrel)else:rel.parent.replace(rel,nodes.And(rel,rtrel))self._and_restriction(rel,restrvar,value.pop())def_and_restriction(self,rel,restrvar,value):ifrelisNone:self.select.add_constant_restriction(restrvar,self.restr_attr,value,self.restr_attr_type)else:rrel=nodes.make_constant_restriction(restrvar,self.restr_attr,value,self.restr_attr_type)rel.parent.replace(rel,nodes.And(rel,rrel))@cacheddef_search_card(self,cards):forrdefinself._iter_rdefs():ifrdef.role_cardinality(self.role)incards:returnTruereturnFalsedef_iter_rdefs(self):rschema=self._cw.vreg.schema.rschema(self.rtype)# XXX when called via ajax, no rset to compute possible typespossibletypes=self.cw_rsetandself.cw_rset.column_types(0)forrdefinrschema.rdefs.itervalues():ifpossibletypesisnotNone:ifself.role=='subject':ifrdef.subjectnotinpossibletypes:continueelifrdef.objectnotinpossibletypes:continueifself.target_typeisnotNone:ifself.role=='subject':ifrdef.object!=self.target_type:continueelifrdef.subject!=self.target_type:continueyieldrdefdef_include_no_relation(self):ifnotself.no_relation:returnFalseifself._cw.vreg.schema.rschema(self.rtype).final:returnFalseifself.role=='object':subj=utils.rqlvar_maker(defined=self.select.defined_vars,aliases=self.select.aliases).next()obj=self.filtered_variable.nameelse:subj=self.filtered_variable.nameobj=utils.rqlvar_maker(defined=self.select.defined_vars,aliases=self.select.aliases).next()restrictions=[]ifself.select.where:restrictions.append(self.select.where.as_string())ifself.select.with_:restrictions.append('WITH '+','.join(term.as_string()forterminself.select.with_))ifrestrictions:restrictions=','+','.join(restrictions)else:restrictions=''rql='Any %s LIMIT 1 WHERE NOT %s%s%s%s'%(self.filtered_variable.name,subj,self.rtype,obj,restrictions)try:returnbool(self.rqlexec(rql,self.cw_rsetandself.cw_rset.args))exceptException:# catch exception on executing rql, work-around #1356884 until a# proper fixself.exception('cant handle rql generated by %s',self)returnFalsedef_add_not_rel_restr(self,rel):nrrel=nodes.Not(_make_relation(self.select,self.filtered_variable,self.rtype,self.role)[1])rel.parent.replace(rel,nodes.Or(nrrel,rel))classRelationAttributeFacet(RelationFacet):"""Base facet to filter some entities according to an attribute of other entities to which they are related. Most things work similarly as :class:`RelationFacet`, except that: * `label_vid` doesn't make sense here * you should specify the attribute type using `target_attr_type` if it's not a String * you can specify a comparison operator using `comparator` Back to our example... if you want to search office by postal code and that you use a :class:`RelationFacet` for that, you won't get the expected behaviour: if two offices have the same postal code, they've however two different addresses. So you'll see in the facet the same postal code twice, though linked to a different address entity. There is a great chance your users won't understand that... That's where this class come in ! It's used to said that you want to filter according to the *attribute value* of a relatied entity, not to the entity itself. Now here is the source code for the facet: .. sourcecode:: python class PostalCodeFacet(RelationAttributeFacet): __regid__ = 'postalcode' # this facet should only be selected when visualizing offices __select__ = RelationAttributeFacet.__select__ & is_instance('Office') # this facet is a filter on the PostalAddress entities linked to the # office through the 'has_address' relation, where the office is the # subject of the relation rtype = 'has_address' role = 'subject' # we want to search according to address 'postal_code' attribute target_attr = 'postalcode' """_select_target_entity=False# attribute typetarget_attr_type='String'# type of comparison: default is an exact match on the attribute valuecomparator='='# could be '<', '<=', '>', '>='@propertydefrestr_attr(self):returnself.target_attr@propertydefrestr_attr_type(self):returnself.target_attr_typedefrset_vocabulary(self,rset):ifself.i18nable:_=self._cw._else:_=unicodeifself.rql_sort:return[(_(value),value)forvalue,inrset]values=[(_(value),value)forvalue,inrset]ifself.sortasc:returnsorted(values)returnreversed(sorted(values))classAttributeFacet(RelationAttributeFacet):"""Base facet to filter some entities according one of their attribute. Configuration is mostly similarly as :class:`RelationAttributeFacet`, except that: * `target_attr` doesn't make sense here (you specify the attribute using `rtype` * `role` neither, it's systematically 'subject' So, suppose that in our office search example you want to refine search according to the office's surface. Here is a code snippet achieving this: .. sourcecode:: python class SurfaceFacet(AttributeFacet): __regid__ = 'surface' __select__ = AttributeFacet.__select__ & is_instance('Office') # this facet is a filter on the office'surface rtype = 'surface' # override the default value of operator since we want to filter # according to a minimal value, not an exact one comparator = '>=' def vocabulary(self): '''override the default vocabulary method since we want to hard-code our threshold values. Not overriding would generate a filter containing all existing surfaces defined in the database. ''' return [('> 200', '200'), ('> 250', '250'), ('> 275', '275'), ('> 300', '300')] """support_and=False_select_target_entity=True@propertydefi18nable(self):"""should label be internationalized"""forrdefinself._iter_rdefs():# no 'internationalizable' property for rdef whose object is not a# Stringifnotgetattr(rdef,'internationalizable',False):returnFalsereturnTruedefvocabulary(self):"""return vocabulary for this facet, eg a list of 2-uple (label, value) """select=self.selectselect.save_state()try:filtered_variable=self.filtered_variablecleanup_select(select,filtered_variable)newvar=prepare_vocabulary_select(select,filtered_variable,self.rtype,self.role)_set_orderby(select,newvar,self.sortasc,self.sortfunc)ifself.cw_rset:args=self.cw_rset.argselse:# vocabulary used for possible_valuesargs=Nonetry:rset=self.rqlexec(select.as_string(),args)exceptException:self.exception('error while getting vocabulary for %s, rql: %s',self,select.as_string())return()finally:select.recover()# don't call rset_vocabulary on empty result set, it may be an empty# *list* (see rqlexec implementation)returnrsetandself.rset_vocabulary(rset)defadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self._cw.form.get(self.__regid__)ifnotvalue:returnfiltered_variable=self.filtered_variableself.select.add_constant_restriction(filtered_variable,self.rtype,value,self.target_attr_type,self.comparator)classRQLPathFacet(RelationFacet):"""Base facet to filter some entities according to an arbitrary rql path. Path should be specified as a list of 3-uples or triplet string, where 'X' represent the filtered variable. You should specify using `filter_variable` the snippet variable that will be used to filter out results. You may also specify a `label_variable`. If you want to filter on an attribute value, you usually don't want to specify the later since it's the same as the filter variable, though you may have to specify the attribute type using `restr_attr_type` if there are some type ambiguity in the schema for the attribute. Using this facet, we can rewrite facets we defined previously: .. sourcecode:: python class AgencyFacet(RQLPathFacet): __regid__ = 'agency' # this facet should only be selected when visualizing offices __select__ = is_instance('Office') # this facet is a filter on the 'Agency' entities linked to the office # through the 'proposed_by' relation, where the office is the subject # of the relation path = ['X has_address O', 'O name N'] filter_variable = 'O' label_variable = 'N' class PostalCodeFacet(RQLPathFacet): __regid__ = 'postalcode' # this facet should only be selected when visualizing offices __select__ = is_instance('Office') # this facet is a filter on the PostalAddress entities linked to the # office through the 'has_address' relation, where the office is the # subject of the relation path = ['X has_address O', 'O postal_code PC'] filter_variable = 'PC' Though some features, such as 'no value' or automatic internationalization, won't work. This facet class is designed to be used for cases where :class:`RelationFacet` or :class:`RelationAttributeFacet` can't do the trick (e.g when you want to filter on entities where are not directly linked to the filtered entities). """__select__=yes()# we don't want RelationFacet's selector# must be specifiedpath=Nonefilter_variable=None# may be specifiedlabel_variable=None# usually guessed, but may be explicitly specifiedrestr_attr=Nonerestr_attr_type=None# XXX disabled featuresi18nable=Falseno_relation=Falsesupport_and=Falsedef__init__(self,*args,**kwargs):super(RQLPathFacet,self).__init__(*args,**kwargs)assertself.filter_variable!=self.label_variable, \('filter_variable and label_variable should be different. ''You may want to let label_variable undefined (ie None).')assertself.pathandisinstance(self.path,(list,tuple)), \'path should be a list of 3-uples, not %s'%self.pathforpartinself.path:ifisinstance(part,basestring):part=part.split()assertlen(part)==3, \'path should be a list of 3-uples, not %s'%partdef__repr__(self):return'<%s%s>'%(self.__class__.__name__,','.join(str(p)forpinself.path))defvocabulary(self):"""return vocabulary for this facet, eg a list of (label, value)"""select=self.selectselect.save_state()ifself.rql_sort:sort=self.sortascelse:sort=None# will be sorted on labeltry:cleanup_select(select,self.filtered_variable)varmap,restrvar=self.add_path_to_select()select.append_selected(nodes.VariableRef(restrvar))ifself.label_variable:attrvar=varmap[self.label_variable]else:attrvar=restrvarselect.append_selected(nodes.VariableRef(attrvar))ifsortisnotNone:_set_orderby(select,attrvar,sort,self.sortfunc)try:rset=self.rqlexec(select.as_string(),self.cw_rset.args)exceptException:self.exception('error while getting vocabulary for %s, rql: %s',self,select.as_string())return()finally:select.recover()# don't call rset_vocabulary on empty result set, it may be an empty# *list* (see rqlexec implementation)values=rsetandself.rset_vocabulary(rset)or[]ifself._include_no_relation():values.insert(0,(self._cw._(self.no_relation_label),''))returnvaluesdefpossible_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 """select=self.selectselect.save_state()try:cleanup_select(select,self.filtered_variable)varmap,restrvar=self.add_path_to_select(skiplabel=True)select.append_selected(nodes.VariableRef(restrvar))values=[unicode(x)forx,inself.rqlexec(select.as_string())]exceptException:self.exception('while computing values for %s',self)return[]finally:select.recover()ifself._include_no_relation():values.append('')returnvaluesdefadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""value=self._cw.form.get(self.__regid__)ifvalueisNone:returnvarmap,restrvar=self.add_path_to_select(skiplabel=True,skipattrfilter=True)self.value_restriction(restrvar,None,value)defadd_path_to_select(self,skiplabel=False,skipattrfilter=False):varmap={'X':self.filtered_variable}actual_filter_variable=Noneforpartinself.path:ifisinstance(part,basestring):part=part.split()subject,rtype,object=partifskiplabelandobject==self.label_variable:continueifobject==self.filter_variable:rschema=self._cw.vreg.schema.rschema(rtype)ifrschema.final:# filter variable is an attribute variableifself.restr_attrisNone:self.restr_attr=rtypeifself.restr_attr_typeisNone:attrtypes=set(objforsubj,objinrschema.rdefs)iflen(attrtypes)>1:raiseException('ambigous attribute %s, specify attrtype on %s'%(rtype,self.__class__))self.restr_attr_type=iter(attrtypes).next()ifskipattrfilter:actual_filter_variable=subjectcontinuesubjvar=_get_var(self.select,subject,varmap)objvar=_get_var(self.select,object,varmap)rel=nodes.make_relation(subjvar,rtype,(objvar,),nodes.VariableRef)self.select.add_restriction(rel)ifself.restr_attrisNone:self.restr_attr='eid'ifself.restr_attr_typeisNone:self.restr_attr_type='Int'ifactual_filter_variable:restrvar=varmap[actual_filter_variable]else:restrvar=varmap[self.filter_variable]returnvarmap,restrvarclassRangeFacet(AttributeFacet):"""This class allows to filter entities according to an attribute of numerical type. It displays a slider using `jquery`_ to choose a lower bound and an upper bound. The example below provides an alternative to the surface facet seen earlier, in a more powerful way since * lower/upper boundaries are computed according to entities to filter * user can specify lower/upper boundaries, not only the lower one .. sourcecode:: python class SurfaceFacet(RangeFacet): __regid__ = 'surface' __select__ = RangeFacet.__select__ & is_instance('Office') # this facet is a filter on the office'surface rtype = 'surface' All this with even less code! The image below display the rendering of the slider: .. image:: ../images/facet_range.png .. _jquery: http://www.jqueryui.com/ """target_attr_type='Float'# only numerical types are supportedneeds_update=False# not supported actually@propertydefwdgclass(self):returnFacetRangeWidgetdef_range_rset(self):select=self.selectselect.save_state()try:filtered_variable=self.filtered_variablecleanup_select(select,filtered_variable)newvar=_add_rtype_relation(select,filtered_variable,self.rtype,self.role)[0]minf=nodes.Function('MIN')minf.append(nodes.VariableRef(newvar))select.add_selected(minf)maxf=nodes.Function('MAX')maxf.append(nodes.VariableRef(newvar))select.add_selected(maxf)# add is restriction if necessaryiffiltered_variable.stinfo['typerel']isNone:etypes=frozenset(sol[filtered_variable.name]forsolinselect.solutions)select.add_type_restriction(filtered_variable,etypes)try:returnself.rqlexec(select.as_string(),self.cw_rset.args)exceptException:self.exception('error while getting vocabulary for %s, rql: %s',self,select.as_string())return()finally:select.recover()defvocabulary(self):"""return vocabulary for this facet, eg a list of 2-uple (label, value) """rset=self._range_rset()ifrset:minv,maxv=rset[0]return[(unicode(minv),minv),(unicode(maxv),maxv)]return[]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. """return[strvalforstrval,valinself.vocabulary()]defget_widget(self):"""return the widget instance to use to display this facet"""values=set(valuefor_,valueinself.vocabulary()ifvalueisnotNone)# Rset with entities (the facet is selected) but without valuesiflen(values)<2:returnNonereturnself.wdgclass(self,min(values),max(values))defformatvalue(self,value):"""format `value` before in order to insert it in the RQL query"""returnunicode(value)definfvalue(self,min=False):ifmin:returnself._cw.form.get('min_%s_inf'%self.__regid__)returnself._cw.form.get('%s_inf'%self.__regid__)defsupvalue(self,max=False):ifmax:returnself._cw.form.get('max_%s_sup'%self.__regid__)returnself._cw.form.get('%s_sup'%self.__regid__)defadd_rql_restrictions(self):infvalue=self.infvalue()supvalue=self.supvalue()ifinfvalueisNoneorsupvalueisNone:# nothing sentreturn# when a value is equal to one of the limit, don't add the restriction,# else we filter out NULL values implicitlyifinfvalue!=self.infvalue(min=True):self._add_restriction(infvalue,'>=')ifsupvalue!=self.supvalue(max=True):self._add_restriction(supvalue,'<=')def_add_restriction(self,value,operator):self.select.add_constant_restriction(self.filtered_variable,self.rtype,self.formatvalue(value),self.target_attr_type,operator)classDateRangeFacet(RangeFacet):"""This class works similarly as the :class:`RangeFacet` but for attribute of date type. The image below display the rendering of the slider for a date range: .. image:: ../images/facet_date_range.png """target_attr_type='Date'# only date types are supported@propertydefwdgclass(self):returnDateFacetRangeWidgetdefformatvalue(self,value):"""format `value` before in order to insert it in the RQL query"""try:date_value=ticks2datetime(float(value))except(ValueError,OverflowError):returnu'"date out-of-range"'return'"%s"'%ustrftime(date_value,'%Y/%m/%d')classHasRelationFacet(AbstractFacet):"""This class simply filter according to the presence of a relation (whatever the entity at the other end). It display a simple checkbox that lets you refine your selection in order to get only entities that actually have this relation. You simply have to define which relation using the `rtype` and `role` attributes. Here is an example of the rendering of thos facet to filter book with image and the corresponding code: .. image:: ../images/facet_has_image.png .. sourcecode:: python class HasImageFacet(HasRelationFacet): __regid__ = 'hasimage' __select__ = HasRelationFacet.__select__ & is_instance('Book') rtype = 'has_image' role = 'subject' """__select__=partial_relation_possible()&match_context_prop()rtype=None# override me in subclassrole='subject'# role of filtered entity in the relationtitle=property(rtype_facet_title)needs_update=False# not supported actuallysupport_and=Falsedefget_widget(self):returnCheckBoxFacetWidget(self._cw,self,'%s:%s'%(self.rtype,self),self._cw.form.get(self.__regid__))defadd_rql_restrictions(self):"""add restriction for this facet into the rql syntax tree"""self.select.set_distinct(True)# XXXvalue=self._cw.form.get(self.__regid__)ifnotvalue:# no value sent for this facetreturnvar=self.select.make_variable()ifself.role=='subject':self.select.add_relation(self.filtered_variable,self.rtype,var)else:self.select.add_relation(var,self.rtype,self.filtered_variable)classBitFieldFacet(AttributeFacet):"""Base facet class for Int field holding some bit values using binary masks. label / value for each bit should be given using the :attr:`choices` attribute. See also :class:`~cubicweb.web.formwidgets.BitSelect`. """choices=None# to be set on concret classdefadd_rql_restrictions(self):value=self._cw.form.get(self.__regid__)ifnotvalue:returnifisinstance(value,list):value=reduce(lambdax,y:int(x)|int(y),value)else:value=int(value)attr_var=self.select.make_variable()self.select.add_relation(self.filtered_variable,self.rtype,attr_var)comp=nodes.Comparison('=',nodes.Constant(value,'Int'))ifvalue==0:comp.append(nodes.variable_ref(attr_var))else:comp.append(nodes.MathExpression('&',nodes.variable_ref(attr_var),nodes.Constant(value,'Int')))having=self.select.havingifhaving:self.select.replace(having[0],nodes.And(having[0],comp))else:self.select.set_having([comp])defrset_vocabulary(self,rset):mask=reduce(lambdax,y:x|(y[0]or0),rset,0)returnsorted([(self._cw._(label),val)forlabel,valinself.choicesifnotvalorval&mask])defpossible_values(self):return[unicode(val)forlabel,valinself.vocabulary()]## html widets ################################################################_DEFAULT_VOCAB_WIDGET_HEIGHT=12_DEFAULT_FACET_GROUP_HEIGHT=15classFacetVocabularyWidget(htmlwidgets.HTMLWidget):def__init__(self,facet):self.facet=facetself.items=[]@cachedpropertydefcss_overflow_limit(self):""" we try to deduce a number of displayed lines from a css property if we get another unit we're out of luck and resort to one constant hence, it is strongly advised not to specify but ems for this css prop """returncss_em_num_value(self.facet._cw.vreg,'facet_vocabMaxHeight',_DEFAULT_VOCAB_WIDGET_HEIGHT)@cachedpropertydefheight(self):""" title, optional and/or dropdown, len(items) or upper limit """return(1.5+# title + small magic constantint(self.facet._support_and_compat()+min(len(self.items),self.css_overflow_limit)))@property@cacheddefoverflows(self):returnlen(self.items)>=self.css_overflow_limitscrollbar_padding_factor=4def_render(self):w=self.wtitle=xml_escape(self.facet.title)facetid=domid(make_uid(self.facet.__regid__))w(u'<div id="%s" class="facet">\n'%facetid)cssclass='facetTitle'ifself.facet.allow_hide:cssclass+=' hideFacetBody'w(u'<div class="%s" cubicweb:facetName="%s">%s</div>\n'%(cssclass,xml_escape(self.facet.__regid__),title))ifself.facet._support_and_compat():self._render_and_or(w)cssclass='facetBody vocabularyFacet'ifnotself.facet.start_unfolded:cssclass+=' hidden'overflow=self.overflowsifoverflow:ifself.facet._support_and_compat():cssclass+=' vocabularyFacetBodyWithLogicalSelector'else:cssclass+=' vocabularyFacetBody'w(u'<div class="%s">\n'%cssclass)forvalue,label,selectedinself.items:ifvalueisNone:continueself._render_value(w,value,label,selected,overflow)w(u'</div>\n')w(u'</div>\n')def_render_and_or(self,w):_=self.facet._cw._w(u"""<select name='%s' class='radio facetOperator' title='%s'> <option value='OR'>%s</option> <option value='AND'>%s</option></select>"""%(xml_escape(self.facet.__regid__)+'_andor',_('and/or between different values'),_('OR'),_('AND')))def_render_value(self,w,value,label,selected,overflow):cssclass='facetValue facetCheckBox'ifselected:cssclass+=' facetValueSelected'w(u'<div class="%s" cubicweb:value="%s">\n'%(cssclass,xml_escape(unicode(value))))# If it is overflowed one must add padding to compensate for the vertical# scrollbar; given current css values, 4 blanks work perfectly ...padding=u' '*self.scrollbar_padding_factorifoverflowelseu''w('<span>%s</span>'%xml_escape(label))w(padding)w(u'</div>')classFacetStringWidget(htmlwidgets.HTMLWidget):def__init__(self,facet):self.facet=facetself.value=None@propertydefheight(self):return2.5def_render(self):w=self.wtitle=xml_escape(self.facet.title)facetid=make_uid(self.facet.__regid__)w(u'<div id="%s" class="facet">\n'%facetid)cssclass='facetTitle'ifself.facet.allow_hide:cssclass+=' hideFacetBody'w(u'<div class="%s" cubicweb:facetName="%s">%s</div>\n'%(cssclass,xml_escape(self.facet.__regid__),title))w(u'<input name="%s" type="text" value="%s" />\n'%(xml_escape(self.facet.__regid__),self.valueoru''))w(u'</div>\n')classFacetRangeWidget(htmlwidgets.HTMLWidget):formatter='function (value) {return value;}'onload=u''' var _formatter = %(formatter)s; jQuery("#%(sliderid)s").slider({ range: true, min: %(minvalue)s, max: %(maxvalue)s, values: [%(minvalue)s, %(maxvalue)s], stop: function(event, ui) { // submit when the user stops sliding var form = $('#%(sliderid)s').closest('form'); buildRQL.apply(null, cw.evalJSON(form.attr('cubicweb:facetargs'))); }, slide: function(event, ui) { jQuery('#%(sliderid)s_inf').html(_formatter(ui.values[0])); jQuery('#%(sliderid)s_sup').html(_formatter(ui.values[1])); jQuery('input[name="%(facetname)s_inf"]').val(ui.values[0]); jQuery('input[name="%(facetname)s_sup"]').val(ui.values[1]); } }); // use JS formatter to format value on page load jQuery('#%(sliderid)s_inf').html(_formatter(jQuery('input[name="%(facetname)s_inf"]').val())); jQuery('#%(sliderid)s_sup').html(_formatter(jQuery('input[name="%(facetname)s_sup"]').val()));'''#'# make emacs happierdef__init__(self,facet,minvalue,maxvalue):self.facet=facetself.minvalue=minvalueself.maxvalue=maxvalue@propertydefheight(self):return2.5def_render(self):w=self.wfacet=self.facetfacet._cw.add_js('jquery.ui.js')facet._cw.add_css('jquery.ui.css')sliderid=make_uid('theslider')facetname=self.facet.__regid__facetid=make_uid(facetname)facet._cw.html_headers.add_onload(self.onload%{'sliderid':sliderid,'facetid':facetid,'facetname':facetname,'minvalue':self.minvalue,'maxvalue':self.maxvalue,'formatter':self.formatter,})title=xml_escape(self.facet.title)facetname=xml_escape(facetname)w(u'<div id="%s" class="facet rangeFacet">\n'%facetid)cssclass='facetTitle'iffacet.allow_hide:cssclass+=' hideFacetBody'w(u'<div class="%s" cubicweb:facetName="%s">%s</div>\n'%(cssclass,facetname,title))cssclass='facetBody'ifnotself.facet.start_unfolded:cssclass+=' hidden'w(u'<div class="%s">\n'%cssclass)w(u'<span id="%s_inf"></span> - <span id="%s_sup"></span>'%(sliderid,sliderid))w(u'<input type="hidden" name="%s_inf" value="%s" />'%(facetname,self.minvalue))w(u'<input type="hidden" name="%s_sup" value="%s" />'%(facetname,self.maxvalue))w(u'<input type="hidden" name="min_%s_inf" value="%s" />'%(facetname,self.minvalue))w(u'<input type="hidden" name="max_%s_sup" value="%s" />'%(facetname,self.maxvalue))w(u'<div id="%s"></div>'%sliderid)w(u'</div>\n')w(u'</div>\n')classDateFacetRangeWidget(FacetRangeWidget):formatter='function (value) {return (new Date(parseFloat(value))).strftime(DATE_FMT);}'defround_max_value(self,d):'round to upper value to avoid filtering out the max value'returndatetime(d.year,d.month,d.day)+timedelta(days=1)def__init__(self,facet,minvalue,maxvalue):maxvalue=self.round_max_value(maxvalue)super(DateFacetRangeWidget,self).__init__(facet,datetime2ticks(minvalue),datetime2ticks(maxvalue))fmt=facet._cw.property_value('ui.date-format')facet._cw.html_headers.define_var('DATE_FMT',fmt)classCheckBoxFacetWidget(htmlwidgets.HTMLWidget):selected_img="black-check.png"unselected_img="black-uncheck.png"def__init__(self,req,facet,value,selected):self._cw=reqself.facet=facetself.value=valueself.selected=selected@propertydefheight(self):return1.5def_render(self):w=self.wtitle=xml_escape(self.facet.title)facetid=make_uid(self.facet.__regid__)w(u'<div id="%s" class="facet">\n'%facetid)cssclass='facetValue facetCheckBox'ifself.selected:cssclass+=' facetValueSelected'imgsrc=self._cw.data_url(self.selected_img)imgalt=self._cw._('selected')else:imgsrc=self._cw.data_url(self.unselected_img)imgalt=self._cw._('not selected')w(u'<div class="%s" cubicweb:value="%s">\n'%(cssclass,xml_escape(unicode(self.value))))w(u'<div>')w(u'<img src="%s" alt="%s" cubicweb:unselimg="true" /> '%(imgsrc,imgalt))w(u'<label class="facetTitle" cubicweb:facetName="%s">%s</label>'%(xml_escape(self.facet.__regid__),title))w(u'</div>\n')w(u'</div>\n')w(u'</div>\n')# other classes ################################################################classFilterRQLBuilder(object):"""called by javascript to get a rql string from filter form"""def__init__(self,req):self._cw=reqdefbuild_rql(self):form=self._cw.formfacetids=form['facets'].split(',')# XXX Union unsupported yetselect=self._cw.vreg.parse(self._cw,form['baserql']).children[0]filtered_variable=get_filtered_variable(select,form.get('mainvar'))toupdate=[]forfacetidinfacetids:facet=get_facet(self._cw,facetid,select,filtered_variable)facet.add_rql_restrictions()iffacet.needs_update:toupdate.append(facetid)returnselect.as_string(),toupdate