[syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
# 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/>."""abstract box classes for CubicWeb web client"""__docformat__="restructuredtext en"_=unicodefromlogilab.mtconverterimportxml_escapefromcubicwebimportUnauthorized,roleasget_role,targetasget_targetfromcubicweb.schemaimportdisplay_namefromcubicweb.selectorsimport(no_cnx,one_line_rset,primary_view,match_context_prop,partial_relation_possible,partial_has_related_entities)fromcubicweb.viewimportView,ReloadableMixInfromcubicweb.uilibimportdomid,jsfromcubicweb.webimportINTERNAL_FIELD_VALUE,stdmsgsfromcubicweb.web.htmlwidgetsimport(BoxLink,BoxWidget,SideBoxWidget,RawBoxItem,BoxSeparator)fromcubicweb.web.actionimportUnregisteredActionclassBoxTemplate(View):"""base template for boxes, usually a (contextual) list of possible actions. Various classes attributes may be used to control the box rendering. You may override on of the formatting callbacks is this is not necessary for your custom box. Classes inheriting from this class usually only have to override call to fetch desired actions, and then to do something like :: box.render(self.w) """__registry__='boxes'__select__=~no_cnx()&match_context_prop()categories_in_order=()cw_property_defs={_('visible'):dict(type='Boolean',default=True,help=_('display the box or not')),_('order'):dict(type='Int',default=99,help=_('display order of the box')),# XXX 'incontext' boxes are handled by the default primary view_('context'):dict(type='String',default='left',vocabulary=(_('left'),_('incontext'),_('right')),help=_('context where this box should be displayed')),}context='left'htmlitemclass='boxItem'defsort_actions(self,actions):"""return a list of (category, actions_sorted_by_title)"""result=[]actions_by_cat={}foractioninactions:actions_by_cat.setdefault(action.category,[]).append((action.title,action))forkey,valuesinactions_by_cat.items():actions_by_cat[key]=[actfortitle,actinsorted(values)]forcatinself.categories_in_order:ifcatinactions_by_cat:result.append((cat,actions_by_cat[cat]))foriteminsorted(actions_by_cat.items()):result.append(item)returnresultdefmk_action(self,title,path,escape=True,**kwargs):"""factory function to create dummy actions compatible with the .format_actions method """ifescape:title=xml_escape(title)returnself.box_action(self._action(title,path,**kwargs))def_action(self,title,path,**kwargs):returnUnregisteredAction(self._cw,self.cw_rset,title,path,**kwargs)# formating callbacksdefboxitem_link_tooltip(self,action):ifaction.__regid__:returnu'keyword: %s'%action.__regid__returnu''defbox_action(self,action):cls=getattr(action,'html_class',lambda:None)()orself.htmlitemclassreturnBoxLink(action.url(),self._cw._(action.title),cls,self.boxitem_link_tooltip(action))classRQLBoxTemplate(BoxTemplate):"""abstract box for boxes displaying the content of a rql query not related to the current result set. It rely on etype, rtype (both optional, usable to control registration according to application schema and display according to connected user's rights) and rql attributes """rql=Nonedefto_display_rql(self):assertself.rqlisnotNone,self.__regid__return(self.rql,)defcall(self,**kwargs):try:rset=self._cw.execute(*self.to_display_rql())exceptUnauthorized:# can't access to something in the query, forget this boxreturniflen(rset)==0:returnbox=BoxWidget(self._cw._(self.title),self.__regid__)fori,(teid,tname)inenumerate(rset):entity=rset.get_entity(i,0)box.append(self.mk_action(tname,entity.absolute_url()))box.render(w=self.w)classUserRQLBoxTemplate(RQLBoxTemplate):"""same as rql box template but the rql is build using the eid of the request's user """defto_display_rql(self):assertself.rqlisnotNone,self.__regid__return(self.rql,{'x':self._cw.user.eid})classEntityBoxTemplate(BoxTemplate):"""base class for boxes related to a single entity"""__select__=BoxTemplate.__select__&one_line_rset()&primary_view()context='incontext'defcall(self,row=0,col=0,**kwargs):"""classes inheriting from EntityBoxTemplate should define cell_call"""self.cell_call(row,col,**kwargs)classRelatedEntityBoxTemplate(EntityBoxTemplate):__select__=EntityBoxTemplate.__select__&partial_has_related_entities()defcell_call(self,row,col,**kwargs):entity=self.cw_rset.get_entity(row,col)limit=self._cw.property_value('navigation.related-limit')+1role=get_role(self)self.w(u'<div class="sideBox">')self.wview('sidebox',entity.related(self.rtype,role,limit=limit),title=display_name(self._cw,self.rtype,role,context=entity.__regid__))self.w(u'</div>')classEditRelationBoxTemplate(ReloadableMixIn,EntityBoxTemplate):"""base class for boxes which let add or remove entities linked by a given relation subclasses should define at least id, rtype and target class attributes. """defcell_call(self,row,col,view=None,**kwargs):self._cw.add_js('cubicweb.ajax.js')entity=self.cw_rset.get_entity(row,col)title=display_name(self._cw,self.rtype,get_role(self),context=entity.__regid__)box=SideBoxWidget(title,self.__regid__)related=self.related_boxitems(entity)unrelated=self.unrelated_boxitems(entity)box.extend(related)ifrelatedandunrelated:box.append(BoxSeparator())box.extend(unrelated)box.render(self.w)defdiv_id(self):returnself.__regid__defbox_item(self,entity,etarget,rql,label):"""builds HTML link to edit relation between `entity` and `etarget` """role,target=get_role(self),get_target(self)args={role[0]:entity.eid,target[0]:etarget.eid}url=self._cw.user_rql_callback((rql,args))# for each target, provide a link to edit the relationlabel=u'[<a href="%s">%s</a>] %s'%(xml_escape(url),label,etarget.view('incontext'))returnRawBoxItem(label,liclass=u'invisible')defrelated_boxitems(self,entity):rql='DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s'%self.rtyperelated=[]foretargetinself.related_entities(entity):related.append(self.box_item(entity,etarget,rql,u'-'))returnrelateddefunrelated_boxitems(self,entity):rql='SET S %s O WHERE S eid %%(s)s, O eid %%(o)s'%self.rtypeunrelated=[]foretargetinself.unrelated_entities(entity):unrelated.append(self.box_item(entity,etarget,rql,u'+'))returnunrelateddefrelated_entities(self,entity):returnentity.related(self.rtype,get_role(self),entities=True)defunrelated_entities(self,entity):"""returns the list of unrelated entities, using the entity's appropriate vocabulary function """skip=set(unicode(e.eid)foreinentity.related(self.rtype,get_role(self),entities=True))skip.add(None)skip.add(INTERNAL_FIELD_VALUE)filteretype=getattr(self,'etype',None)entities=[]form=self._cw.vreg['forms'].select('edition',self._cw,rset=self.cw_rset,row=self.cw_rowor0)field=form.field_by_name(self.rtype,get_role(self),entity.e_schema)for_,eidinfield.vocabulary(form):ifeidnotinskip:entity=self._cw.entity_from_eid(eid)iffilteretypeisNoneorentity.__regid__==filteretype:entities.append(entity)returnentitiesclassAjaxEditRelationBoxTemplate(EntityBoxTemplate):__select__=EntityBoxTemplate.__select__&partial_relation_possible()# view used to display related enttiesitem_vid='incontext'# values separator when multiple values are allowedseparator=','# msgid of the message to display when some new relation has been added/removedadded_msg=Noneremoved_msg=None# class attributes below *must* be set in concret classes (additionaly to# rtype / role [/ target_etype]. They should correspond to js_* methods on# the json controller# function(eid)# -> expected to return a list of values to display as input selector# vocabularyfname_vocabulary=None# function(eid, value)# -> handle the selector's input (eg create necessary entities and/or# relations). If the relation is multiple, you'll get a list of value, else# a single string value.fname_validate=None# function(eid, linked entity eid)# -> remove the relationfname_remove=Nonedefcell_call(self,row,col,**kwargs):req=self._cwentity=self.cw_rset.get_entity(row,col)related=entity.related(self.rtype,self.role)rdef=entity.e_schema.rdef(self.rtype,self.role,self.target_etype)ifself.role=='subject':mayadd=rdef.has_perm(req,'add',fromeid=entity.eid)maydel=rdef.has_perm(req,'delete',fromeid=entity.eid)else:mayadd=rdef.has_perm(req,'add',toeid=entity.eid)maydel=rdef.has_perm(req,'delete',toeid=entity.eid)ifnot(relatedormayadd):returnifmayaddormaydel:req.add_js(('cubicweb.ajax.js','cubicweb.ajax.box.js'))_=req._w=self.wdivid=domid(self.__regid__)+unicode(entity.eid)w(u'<div class="sideBox" id="%s%s">'%(domid(self.__regid__),entity.eid))w(u'<div class="sideBoxTitle"><span>%s</span></div>'%rdef.rtype.display_name(req,self.role,context=entity.__regid__))w(u'<div class="sideBox"><div class="sideBoxBody">')ifrelated:w(u'<table>')forrentityinrelated.entities():# for each related entity, provide a link to remove the relationsubview=rentity.view(self.item_vid)ifmaydel:jscall=unicode(js.ajaxBoxRemoveLinkedEntity(self.__regid__,entity.eid,rentity.eid,self.fname_remove,self.removed_msgand_(self.removed_msg)))w(u'<tr><td>[<a href="javascript: %s">-</a>]</td>''<td class="tagged">%s</td></tr>'%(xml_escape(jscall),subview))else:w(u'<tr><td class="tagged">%s</td></tr>'%(subview))w(u'</table>')else:w(_('no related entity'))ifmayadd:req.add_js('jquery.autocomplete.js')req.add_css('jquery.autocomplete.css')multiple=rdef.role_cardinality(self.role)in'*+'w(u'<table><tr><td>')jscall=unicode(js.ajaxBoxShowSelector(self.__regid__,entity.eid,self.fname_vocabulary,self.fname_validate,self.added_msgand_(self.added_msg),_(stdmsgs.BUTTON_OK[0]),_(stdmsgs.BUTTON_CANCEL[0]),multipleandself.separator))w('<a class="button sglink" href="javascript: %s">%s</a>'%(xml_escape(jscall),multipleand_('add_relation')or_('update_relation')))w(u'</td><td>')w(u'<div id="%sHolder"></div>'%divid)w(u'</td></tr></table>')w(u'</div>\n')w(u'</div></div>\n')