"""helper functions for application hooks:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"fromcubicwebimportRepositoryErrorfromcubicweb.server.poolimportSingleLastOperationdefentity_name(session,eid):"""return the "name" attribute of the entity with the given eid"""returnentity_attr(session,eid,'name')defentity_attr(session,eid,attr):"""return an arbitrary attribute of the entity with the given eid"""rset=session.execute('Any N WHERE X eid %%(x)s, X %s N'%attr,{'x':eid},'x')returnrset[0][0]defrproperty(session,rtype,eidfrom,eidto,rprop):rschema=session.repo.schema[rtype]subjtype=session.describe(eidfrom)[0]objtype=session.describe(eidto)[0]returnrschema.rproperty(subjtype,objtype,rprop)defcheck_internal_entity(session,eid,internal_names):"""check that the entity's name is not in the internal_names list. raise a RepositoryError if so, else return the entity's name """name=entity_name(session,eid)ifnameininternal_names:raiseRepositoryError('%s entity can\'t be deleted'%name)returnnamedefget_user_sessions(repo,ueid):forsessioninrepo._sessions.values():ifueid==session.user.eid:yieldsession# mail related ################################################################classSendMailOp(SingleLastOperation):def__init__(self,session,msg=None,recipients=None,**kwargs):# may not specify msg yet, as# `cubicweb.sobjects.supervision.SupervisionMailOp`ifmsgisnotNone:assertrecipientsself.to_send=[(msg,recipients)]else:assertrecipientsisNoneself.to_send=[]super(SendMailOp,self).__init__(session,**kwargs)defregister(self,session):previous=super(SendMailOp,self).register(session)ifprevious:self.to_send=previous.to_send+self.to_senddefcommit_event(self):self.repo.threaded_task(self.sendmails)defsendmails(self):self.config.sendmails(self.to_send)# state related ###############################################################defprevious_state(session,eid):"""return the state of the entity with the given eid, usually since it's changing in the current transaction. Due to internal relation hooks, the relation may has been deleted at this point, so we have handle that """ifeidinsession.transaction_data.get('neweids',()):returnpending=session.transaction_data.get('pendingrelations',())foreidfrom,rtype,eidtoinreversed(pending):ifrtype=='in_state'andeidfrom==eid:rset=session.execute('Any S,N WHERE S eid %(x)s, S name N',{'x':eidto},'x')returnrset.get_entity(0,0)rset=session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N',{'x':eid},'x')ifrset:returnrset.get_entity(0,0)