hooks/metadata.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Core hooks: set generic metadata"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 
       
    22 from datetime import datetime
       
    23 from base64 import b64encode
       
    24 
       
    25 from pytz import utc
       
    26 
       
    27 from cubicweb.predicates import is_instance
       
    28 from cubicweb.server import hook
       
    29 from cubicweb.server.edition import EditedEntity
       
    30 
       
    31 
       
    32 class MetaDataHook(hook.Hook):
       
    33     __abstract__ = True
       
    34     category = 'metadata'
       
    35 
       
    36 
       
    37 class InitMetaAttrsHook(MetaDataHook):
       
    38     """before create a new entity -> set creation and modification date
       
    39 
       
    40     this is a conveniency hook, you shouldn't have to disable it
       
    41     """
       
    42     __regid__ = 'metaattrsinit'
       
    43     events = ('before_add_entity',)
       
    44 
       
    45     def __call__(self):
       
    46         timestamp = datetime.now(utc)
       
    47         edited = self.entity.cw_edited
       
    48         if not edited.get('creation_date'):
       
    49             edited['creation_date'] = timestamp
       
    50         if not edited.get('modification_date'):
       
    51             edited['modification_date'] = timestamp
       
    52         if not self._cw.transaction_data.get('do-not-insert-cwuri'):
       
    53             cwuri = u'%s%s' % (self._cw.base_url(), self.entity.eid)
       
    54             edited.setdefault('cwuri', cwuri)
       
    55 
       
    56 
       
    57 class UpdateMetaAttrsHook(MetaDataHook):
       
    58     """update an entity -> set modification date"""
       
    59     __regid__ = 'metaattrsupdate'
       
    60     events = ('before_update_entity',)
       
    61 
       
    62     def __call__(self):
       
    63         # repairing is true during c-c upgrade/shell and similar commands. We
       
    64         # usually don't want to update modification date in such cases.
       
    65         #
       
    66         # XXX to be really clean, we should turn off modification_date update
       
    67         # explicitly on each command where we do not want that behaviour.
       
    68         if not self._cw.vreg.config.repairing:
       
    69             self.entity.cw_edited.setdefault('modification_date', datetime.now(utc))
       
    70 
       
    71 
       
    72 class SetCreatorOp(hook.DataOperationMixIn, hook.Operation):
       
    73 
       
    74     def precommit_event(self):
       
    75         cnx = self.cnx
       
    76         relations = [(eid, cnx.user.eid) for eid in self.get_data()
       
    77                 # don't consider entities that have been created and deleted in
       
    78                 # the same transaction, nor ones where created_by has been
       
    79                 # explicitly set
       
    80                 if not cnx.deleted_in_transaction(eid) and \
       
    81                    not cnx.entity_from_eid(eid).created_by]
       
    82         cnx.add_relations([('created_by', relations)])
       
    83 
       
    84 
       
    85 class SetOwnershipHook(MetaDataHook):
       
    86     """create a new entity -> set owner and creator metadata"""
       
    87     __regid__ = 'setowner'
       
    88     events = ('after_add_entity',)
       
    89 
       
    90     def __call__(self):
       
    91         if not self._cw.is_internal_session:
       
    92             self._cw.add_relation(self.entity.eid, 'owned_by', self._cw.user.eid)
       
    93             SetCreatorOp.get_instance(self._cw).add_data(self.entity.eid)
       
    94 
       
    95 
       
    96 class SyncOwnersOp(hook.DataOperationMixIn, hook.Operation):
       
    97     def precommit_event(self):
       
    98         for compositeeid, composedeid in self.get_data():
       
    99             if self.cnx.deleted_in_transaction(compositeeid):
       
   100                 continue
       
   101             if self.cnx.deleted_in_transaction(composedeid):
       
   102                 continue
       
   103             self.cnx.execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
       
   104                                  'NOT EXISTS(X owned_by U, X eid %(x)s)',
       
   105                                  {'c': compositeeid, 'x': composedeid})
       
   106 
       
   107 
       
   108 class SyncCompositeOwner(MetaDataHook):
       
   109     """when adding composite relation, the composed should have the same owners
       
   110     has the composite
       
   111     """
       
   112     __regid__ = 'synccompositeowner'
       
   113     events = ('after_add_relation',)
       
   114 
       
   115     def __call__(self):
       
   116         if self.rtype == 'wf_info_for':
       
   117             # skip this special composite relation # XXX (syt) why?
       
   118             return
       
   119         eidfrom, eidto = self.eidfrom, self.eidto
       
   120         composite = self._cw.rtype_eids_rdef(self.rtype, eidfrom, eidto).composite
       
   121         if composite == 'subject':
       
   122             SyncOwnersOp.get_instance(self._cw).add_data( (eidfrom, eidto) )
       
   123         elif composite == 'object':
       
   124             SyncOwnersOp.get_instance(self._cw).add_data( (eidto, eidfrom) )
       
   125 
       
   126 
       
   127 class FixUserOwnershipHook(MetaDataHook):
       
   128     """when a user has been created, add owned_by relation on itself"""
       
   129     __regid__ = 'fixuserowner'
       
   130     __select__ = MetaDataHook.__select__ & is_instance('CWUser')
       
   131     events = ('after_add_entity',)
       
   132 
       
   133     def __call__(self):
       
   134         self._cw.add_relation(self.entity.eid, 'owned_by', self.entity.eid)
       
   135 
       
   136 
       
   137 class UpdateFTIHook(MetaDataHook):
       
   138     """sync fulltext index text index container when a relation with
       
   139     fulltext_container set is added / removed
       
   140     """
       
   141     __regid__ = 'updateftirel'
       
   142     events = ('after_add_relation', 'after_delete_relation')
       
   143 
       
   144     def __call__(self):
       
   145         rtype = self.rtype
       
   146         cnx = self._cw
       
   147         ftcontainer = cnx.vreg.schema.rschema(rtype).fulltext_container
       
   148         if ftcontainer == 'subject':
       
   149             cnx.repo.system_source.index_entity(
       
   150                 cnx, cnx.entity_from_eid(self.eidfrom))
       
   151         elif ftcontainer == 'object':
       
   152             cnx.repo.system_source.index_entity(
       
   153                 cnx, cnx.entity_from_eid(self.eidto))
       
   154 
       
   155 
       
   156 
       
   157 # entity source handling #######################################################
       
   158 
       
   159 class ChangeEntitySourceUpdateCaches(hook.Operation):
       
   160     oldsource = newsource = entity = None # make pylint happy
       
   161 
       
   162     def postcommit_event(self):
       
   163         self.oldsource.reset_caches()
       
   164         repo = self.cnx.repo
       
   165         entity = self.entity
       
   166         extid = entity.cw_metainformation()['extid']
       
   167         repo._type_source_cache[entity.eid] = (
       
   168             entity.cw_etype, None, self.newsource.uri)
       
   169         repo._extid_cache[extid] = -entity.eid
       
   170 
       
   171 
       
   172 class ChangeEntitySourceDeleteHook(MetaDataHook):
       
   173     """support for moving an entity from an external source by watching 'Any
       
   174     cw_source CWSource' relation
       
   175     """
       
   176 
       
   177     __regid__ = 'cw.metadata.source-change'
       
   178     __select__ = MetaDataHook.__select__ & hook.match_rtype('cw_source')
       
   179     events = ('before_delete_relation',)
       
   180 
       
   181     def __call__(self):
       
   182         if (self._cw.deleted_in_transaction(self.eidfrom)
       
   183             or self._cw.deleted_in_transaction(self.eidto)):
       
   184             return
       
   185         schange = self._cw.transaction_data.setdefault('cw_source_change', {})
       
   186         schange[self.eidfrom] = self.eidto
       
   187 
       
   188 
       
   189 class ChangeEntitySourceAddHook(MetaDataHook):
       
   190     __regid__ = 'cw.metadata.source-change'
       
   191     __select__ = MetaDataHook.__select__ & hook.match_rtype('cw_source')
       
   192     events = ('before_add_relation',)
       
   193 
       
   194     def __call__(self):
       
   195         schange = self._cw.transaction_data.get('cw_source_change')
       
   196         if schange is not None and self.eidfrom in schange:
       
   197             newsource = self._cw.entity_from_eid(self.eidto)
       
   198             if newsource.name != 'system':
       
   199                 raise Exception('changing source to something else than the '
       
   200                                 'system source is unsupported')
       
   201             syssource = newsource.repo_source
       
   202             oldsource = self._cw.entity_from_eid(schange[self.eidfrom])
       
   203             entity = self._cw.entity_from_eid(self.eidfrom)
       
   204             # we don't want the moved entity to be reimported later.  To
       
   205             # distinguish this state, move the record from the 'entities' table
       
   206             # to 'moved_entities'.  External source will then have consider
       
   207             # case where `extid2eid` returns a negative eid as 'this entity was
       
   208             # known but has been moved, ignore it'.
       
   209             extid = self._cw.entity_metas(entity.eid)['extid']
       
   210             assert extid is not None
       
   211             attrs = {'eid': entity.eid, 'extid': b64encode(extid).decode('ascii')}
       
   212             self._cw.system_sql(syssource.sqlgen.insert('moved_entities', attrs), attrs)
       
   213             attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': None,
       
   214                      'asource': 'system'}
       
   215             self._cw.system_sql(syssource.sqlgen.update('entities', attrs, ['eid']), attrs)
       
   216             # register an operation to update repository/sources caches
       
   217             ChangeEntitySourceUpdateCaches(self._cw, entity=entity,
       
   218                                            oldsource=oldsource.repo_source,
       
   219                                            newsource=syssource)