drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
"""Core hooks: set generic metadata: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"fromdatetimeimportdatetimefromcubicweb.selectorsimportimplementsfromcubicweb.serverimporthookfromcubicweb.server.repositoryimportFTIndexEntityOpdefeschema_type_eid(session,etype):"""get eid of the CWEType entity for the given yams type"""eschema=session.repo.schema.eschema(etype)# eschema.eid is None if schema has been readen from the filesystem, not# from the database (eg during tests)ifeschema.eidisNone:eschema.eid=session.unsafe_execute('Any X WHERE X is CWEType, X name %(name)s',{'name':str(etype)})[0][0]returneschema.eidclassMetaDataHook(hook.Hook):__abstract__=Truecategory='metadata'classInitMetaAttrsHook(MetaDataHook):"""before create a new entity -> set creation and modification date this is a conveniency hook, you shouldn't have to disable it """__regid__='metaattrsinit'events=('before_add_entity',)def__call__(self):timestamp=datetime.now()self.entity.setdefault('creation_date',timestamp)self.entity.setdefault('modification_date',timestamp)ifnotself._cw.get_shared_data('do-not-insert-cwuri'):cwuri=u'%seid/%s'%(self._cw.base_url(),self.entity.eid)self.entity.setdefault('cwuri',cwuri)classUpdateMetaAttrsHook(MetaDataHook):"""update an entity -> set modification date"""__regid__='metaattrsupdate'events=('before_update_entity',)def__call__(self):self.entity.setdefault('modification_date',datetime.now())class_SetCreatorOp(hook.Operation):defprecommit_event(self):session=self.sessionifsession.deleted_in_transaction(self.entity.eid):# entity have been created and deleted in the same transactionreturnifnotself.entity.created_by:session.add_relation(self.entity.eid,'created_by',session.user.eid)classSetIsHook(MetaDataHook):"""create a new entity -> set is relation"""__regid__='setis'events=('after_add_entity',)def__call__(self):ifhasattr(self.entity,'_cw_recreating'):returnsession=self._cwentity=self.entitytry:#session.add_relation(entity.eid, 'is',# eschema_type_eid(session, entity.__regid__))session.system_sql('INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)'%(entity.eid,eschema_type_eid(session,entity.__regid__)))exceptIndexError:# during schema serialization, skipreturnforetypeinentity.e_schema.ancestors()+[entity.e_schema]:#session.add_relation(entity.eid, 'is_instance_of',# eschema_type_eid(session, etype))session.system_sql('INSERT INTO is_instance_of_relation(eid_from,eid_to) VALUES (%s,%s)'%(entity.eid,eschema_type_eid(session,etype)))classSetOwnershipHook(MetaDataHook):"""create a new entity -> set owner and creator metadata"""__regid__='setowner'events=('after_add_entity',)def__call__(self):asession=self._cw.actual_session()ifnotasession.is_internal_session:self._cw.add_relation(self.entity.eid,'owned_by',asession.user.eid)_SetCreatorOp(asession,entity=self.entity)class_SyncOwnersOp(hook.Operation):defprecommit_event(self):self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,''NOT EXISTS(X owned_by U, X eid %(x)s)',{'c':self.compositeeid,'x':self.composedeid},('c','x'))classSyncCompositeOwner(MetaDataHook):"""when adding composite relation, the composed should have the same owners has the composite """__regid__='synccompositeowner'events=('after_add_relation',)def__call__(self):ifself.rtype=='wf_info_for':# skip this special composite relation # XXX (syt) why?returneidfrom,eidto=self.eidfrom,self.eidtocomposite=self._cw.schema_rproperty(self.rtype,eidfrom,eidto,'composite')ifcomposite=='subject':_SyncOwnersOp(self._cw,compositeeid=eidfrom,composedeid=eidto)elifcomposite=='object':_SyncOwnersOp(self._cw,compositeeid=eidto,composedeid=eidfrom)classFixUserOwnershipHook(MetaDataHook):"""when a user has been created, add owned_by relation on itself"""__regid__='fixuserowner'__select__=MetaDataHook.__select__&implements('CWUser')events=('after_add_entity',)def__call__(self):self._cw.add_relation(self.entity.eid,'owned_by',self.entity.eid)classUpdateFTIHook(MetaDataHook):"""sync fulltext index when relevant relation is added / removed """__regid__='updateftirel'events=('after_add_relation','after_delete_relation')def__call__(self):rtype=self.rtypesession=self._cwifself.event=='after_add_relation':# Reindexing the contained entity is enough since it will implicitly# reindex the container entity.ftcontainer=session.vreg.schema.rschema(rtype).fulltext_containerifftcontainer=='subject':FTIndexEntityOp(session,entity=session.entity_from_eid(self.eidto))elifftcontainer=='object':FTIndexEntityOp(session,entity=session.entity_from_eid(self.eidfrom))elifsession.repo.schema.rschema(rtype).fulltext_container:FTIndexEntityOp(session,entity=session.entity_from_eid(self.eidto))FTIndexEntityOp(session,entity=session.entity_from_eid(self.eidfrom))