"""Set of views allowing edition of entities/relations using ajax:organization: Logilab:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromcubicweb.common.selectorsimport(chainfirst,match_form_params,match_kwargs)fromcubicweb.web.boximportEditRelationBoxTemplateclassAddRelationView(EditRelationBoxTemplate):"""base class for view which let add entities linked by a given relation subclasses should define at least id, rtype and target class attributes. """__registry__='views'__selectors__=(chainfirst(match_form_params,match_kwargs),)property_defs={}# don't want to inherit this from Boxid='xaddrelation'expected_kwargs=form_params=('rtype','target')build_js=EditRelationBoxTemplate.build_reload_js_calldefcell_call(self,row,col,rtype=None,target=None,etype=None):self.rtype=rtypeorself.req.form['rtype']self.target=targetorself.req.form['target']self.etype=etypeorself.req.form.get('etype')entity=self.entity(row,col)rschema=self.schema.rschema(self.rtype)ifnotself.etype:ifself.target=='object':etypes=rschema.objects(entity.e_schema)else:etypes=rschema.subjects(entity.e_schema)iflen(etypes)==1:self.etype=etypes[0]fakebox=[]self.w(u'<div id="%s">'%self.id)self.w(u'<h1>%s</h1>'%self.req._('relation %(relname)s of %(ent)s')%{'relname':rschema.display_name(self.req,self.xtarget()[0]),'ent':entity.view('incontext')})self.w(u'<ul>')self.w_unrelated(fakebox,entity)forboxiteminfakebox:boxitem.render(self.w)self.w(u'</ul></div>')defunrelated_entities(self,entity):"""returns the list of unrelated entities if etype is not defined on the Box's class, the default behaviour is to use the entity's appropraite vocabulary function """x,target=self.xtarget()# use entity.unrelated if we've been asked for a particular etypeifgetattr(self,'etype',None):rset=entity.unrelated(self.rtype,self.etype,x,ordermethod='fetch_order')self.pagination(self.req,rset,w=self.w)returnrset.entities()# in other cases, use vocabulary functionsentities=[]for_,eidinentity.vocabulary(self.rtype,x):ifeidisnotNone:rset=self.req.eid_rset(eid)entities.append(rset.get_entity(0,0))returnentities