"""entity classes for optional library entities:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromurlparseimporturlsplit,urlunsplitfrommx.DateTimeimportnowfromlogilab.common.decoratorsimportcachedfromcubicweb.common.entityimport_markerfromcubicweb.entitiesimportAnyEntity,fetch_configdefmangle_email(address):try:name,host=address.split('@',1)exceptValueError:returnaddressreturn'%s at %s'%(name,host.replace('.',' dot '))classEmailAddress(AnyEntity):id='EmailAddress'fetch_attrs,fetch_order=fetch_config(['address','alias','canonical'])widgets={'address':"EmailWidget",}defdc_title(self):ifself.alias:return'%s <%s>'%(self.alias,self.display_address())returnself.display_address()@propertydefemail_of(self):returnself.reverse_use_emailandself.reverse_use_email[0]@cacheddefcanonical_form(self):ifself.canonical:returnselfrql='EmailAddress X WHERE X identical_to Y, X canonical TRUE, Y eid %(y)s'cnrset=self.req.execute(rql,{'y':self.eid},'y')ifcnrset:returncnrset.get_entity(0,0)returnNonedefrelated_emails(self,skipeids=None):# XXX move to eemail# check email relations are in the schema firstsubjrels=self.e_schema.object_relations()ifnot('sender'insubjrelsand'recipients'insubjrels):returnrql='DISTINCT Any X, S, D ORDERBY D DESC WHERE X sender Y or X recipients Y, X subject S, X date D, Y eid %(y)s'rset=self.req.execute(rql,{'y':self.eid},'y')ifskipeidsisNone:skipeids=set()foriinxrange(len(rset)):eid=rset[i][0]ifeidinskipeids:continueskipeids.add(eid)yieldrset.get_entity(i,0)defdisplay_address(self):ifself.vreg.config['mangle-emails']:returnmangle_email(self.address)returnself.addressdefprintable_value(self,attr,value=_marker,attrtype=None,format='text/html'):"""overriden to return displayable address when necessary"""ifattr=='address':returnself.display_address()returnsuper(EmailAddress,self).printable_value(attr,value,attrtype,format)defafter_deletion_path(self):"""return (path, parameters) which should be used as redirect information when this entity is being deleted """ifself.email_of:returnself.email_of.rest_path(),{}returnsuper(EmailAddress,self).after_deletion_path()fromlogilab.common.deprecationimportclass_renamedEmailaddress=class_renamed('Emailaddress',EmailAddress)Emailaddress.id='Emailaddress'classEProperty(AnyEntity):id='EProperty'fetch_attrs,fetch_order=fetch_config(['pkey','value'])widgets={'pkey':"PropertyKeyWidget",'value':"PropertyValueWidget",}rest_attr='pkey'deftyped_value(self):returnself.vreg.typed_value(self.pkey,self.value)defdc_description(self):returnself.req._(self.vreg.property_info(self.pkey)['help'])defafter_deletion_path(self):"""return (path, parameters) which should be used as redirect information when this entity is being deleted """return'view',{}classBookmark(AnyEntity):"""customized class for Bookmark entities"""id='Bookmark'fetch_attrs,fetch_order=fetch_config(['title','path'])widgets={'path':"StringWidget",}__rtags__={'path':'primary'}defactual_url(self):url=self.req.build_url(self.path)ifself.title:urlparts=list(urlsplit(url))ifurlparts[3]:urlparts[3]+='&vtitle=%s'%self.req.url_quote(self.title)else:urlparts[3]='vtitle=%s'%self.req.url_quote(self.title)url=urlunsplit(urlparts)returnurldefaction_url(self):returnself.absolute_url()+'/follow'classCard(AnyEntity):"""customized class for Card entities"""id='Card'rest_attr='wikiid'fetch_attrs,fetch_order=fetch_config(['title'])defdc_title(self):returnself.titledefdc_description(self,format='text/plain'):returnself.synopsisoru''classECache(AnyEntity):"""Cache"""id='ECache'fetch_attrs,fetch_order=fetch_config(['name'])deftouch(self):self.req.execute('SET X timestamp %(t)s WHERE X eid %(x)s',{'t':now(),'x':self.eid},'x')defvalid(self,date):returndate<self.timestamp