fix hooks control method name + other litle cleanups
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 09 Mar 2010 11:00:48 +0100
changeset 4843 5f7363416765
parent 4835 13b0b96d7982
child 4844 ad78b118b124
fix hooks control method name + other litle cleanups
hooks/notification.py
misc/migration/bootstrapmigration_repository.py
schema.py
server/migractions.py
server/querier.py
server/repository.py
server/session.py
sobjects/supervising.py
--- a/hooks/notification.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/hooks/notification.py	Tue Mar 09 11:00:48 2010 +0100
@@ -109,12 +109,13 @@
 
     def __call__(self):
         session = self._cw
-        if self.entity.eid in session.transaction_data.get('neweids', ()):
+        if session.added_in_transaction(self.entity.eid):
             return # entity is being created
         # then compute changes
         changes = session.transaction_data.setdefault('changes', {})
         thisentitychanges = changes.setdefault(self.entity.eid, set())
-        attrs = [k for k in self.entity.edited_attributes if not k in self.skip_attrs]
+        attrs = [k for k in self.entity.edited_attributes
+                 if not k in self.skip_attrs]
         if not attrs:
             return
         rqlsel, rqlrestr = [], ['X eid %(x)s']
--- a/misc/migration/bootstrapmigration_repository.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/misc/migration/bootstrapmigration_repository.py	Tue Mar 09 11:00:48 2010 +0100
@@ -30,7 +30,7 @@
     session.set_pool()
     permsdict = ss.deserialize_ertype_permissions(session)
 
-    changes = session.disable_hooks_category.add('integrity')
+    changes = session.disable_hook_categories.add('integrity')
     for rschema in repo.schema.relations():
         rpermsdict = permsdict.get(rschema.eid, {})
         for rdef in rschema.rdefs.values():
@@ -71,7 +71,7 @@
         drop_relation_definition('CWRType', '%s_permission' % action, 'CWGroup', commit=False)
         drop_relation_definition('CWRType', '%s_permission' % action, 'RQLExpression')
     if changes:
-        session.enable_hooks_category.add(*changes)
+        session.enable_hook_categories.add(*changes)
 
 if applcubicwebversion < (3, 4, 0) and cubicwebversion >= (3, 4, 0):
 
--- a/schema.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/schema.py	Tue Mar 09 11:00:48 2010 +0100
@@ -1084,7 +1084,7 @@
         if hasattr(cw, 'write_security'): # test it's a session and not a request
             # cw is a server session
             hasperm = not cw.write_security or \
-                      not cw.is_hooks_category_activated('integrity') or \
+                      not cw.is_hook_category_activated('integrity') or \
                       cw.user.has_permission(PERM_USE_TEMPLATE_FORMAT)
         else:
             hasperm = cw.user.has_permission(PERM_USE_TEMPLATE_FORMAT)
--- a/server/migractions.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/server/migractions.py	Tue Mar 09 11:00:48 2010 +0100
@@ -1268,13 +1268,13 @@
         if commit:
             self.commit()
 
-    @deprecated("[3.7] use session.disable_hooks_category('integrity')")
+    @deprecated("[3.7] use session.disable_hook_categories('integrity')")
     def cmd_deactivate_verification_hooks(self):
-        self.session.disable_hooks_category('integrity')
+        self.session.disable_hook_categories('integrity')
 
-    @deprecated("[3.7] use session.enable_hooks_category('integrity')")
+    @deprecated("[3.7] use session.enable_hook_categories('integrity')")
     def cmd_reactivate_verification_hooks(self):
-        self.session.enable_hooks_category('integrity')
+        self.session.enable_hook_categories('integrity')
 
 
 class ForRqlIterator:
--- a/server/querier.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/server/querier.py	Tue Mar 09 11:00:48 2010 +0100
@@ -599,16 +599,16 @@
             try:
                 self.solutions(session, rqlst, args)
             except UnknownEid:
-                # we want queries such as "Any X WHERE X eid 9999"
-                # return an empty result instead of raising UnknownEid
+                # we want queries such as "Any X WHERE X eid 9999" return an
+                # empty result instead of raising UnknownEid
                 return empty_rset(rql, args, rqlst)
             self._rql_cache[cachekey] = rqlst
         orig_rqlst = rqlst
         if not rqlst.TYPE == 'select':
             if session.read_security:
                 check_no_password_selected(rqlst)
-            # write query, ensure session's mode is 'write' so connections
-            # won't be released until commit/rollback
+            # write query, ensure session's mode is 'write' so connections won't
+            # be released until commit/rollback
             session.mode = 'write'
             cachekey = None
         else:
--- a/server/repository.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/server/repository.py	Tue Mar 09 11:00:48 2010 +0100
@@ -87,7 +87,7 @@
     # XXX we should imo rely on the orm to first fetch existing entity if any
     # then delete it.
     if session.is_internal_session \
-           or not session.is_hooks_category_activated('integrity'):
+           or not session.is_hook_category_activated('integrity'):
         return
     card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality')
     # one may be tented to check for neweids but this may cause more than one
@@ -983,7 +983,7 @@
             if not rschema.final: # inlined relation
                 relations.append((attr, entity[attr]))
         entity.set_defaults()
-        if session.is_hooks_category_activated('integrity'):
+        if session.is_hook_category_activated('integrity'):
             entity.check(creation=True)
         source.add_entity(session, entity)
         if source.uri != 'system':
@@ -1031,7 +1031,7 @@
             print 'UPDATE entity', etype, entity.eid, \
                   dict(entity), edited_attributes
         entity.edited_attributes = edited_attributes
-        if session.is_hooks_category_activated('integrity'):
+        if session.is_hook_category_activated('integrity'):
             entity.check()
         eschema = entity.e_schema
         session.set_entity_cache(entity)
--- a/server/session.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/server/session.py	Tue Mar 09 11:00:48 2010 +0100
@@ -61,16 +61,16 @@
     def __enter__(self):
         self.oldmode = self.session.set_hooks_mode(self.mode)
         if self.mode is self.session.HOOKS_DENY_ALL:
-            self.changes = self.session.enable_hooks_category(*self.categories)
+            self.changes = self.session.enable_hook_categories(*self.categories)
         else:
-            self.changes = self.session.disable_hooks_category(*self.categories)
+            self.changes = self.session.disable_hook_categories(*self.categories)
 
     def __exit__(self, exctype, exc, traceback):
         if self.changes:
             if self.mode is self.session.HOOKS_DENY_ALL:
-                self.session.disable_hooks_category(*self.changes)
+                self.session.disable_hook_categories(*self.changes)
             else:
-                self.session.enable_hooks_category(*self.changes)
+                self.session.enable_hook_categories(*self.changes)
         self.session.set_hooks_mode(self.oldmode)
 
 INDENT = ''
@@ -377,60 +377,76 @@
         return oldmode
 
     @property
-    def disabled_hooks_categories(self):
+    def disabled_hook_categories(self):
         try:
-            return getattr(self._threaddata, 'disabled_hooks_cats')
+            return getattr(self._threaddata, 'disabled_hook_cats')
         except AttributeError:
-            cats = self._threaddata.disabled_hooks_cats = set()
+            cats = self._threaddata.disabled_hook_cats = set()
             return cats
 
     @property
-    def enabled_hooks_categories(self):
+    def enabled_hook_categories(self):
         try:
-            return getattr(self._threaddata, 'enabled_hooks_cats')
+            return getattr(self._threaddata, 'enabled_hook_cats')
         except AttributeError:
-            cats = self._threaddata.enabled_hooks_cats = set()
+            cats = self._threaddata.enabled_hook_cats = set()
             return cats
 
-    def disable_hooks_category(self, *categories):
+    def disable_hook_categories(self, *categories):
+        """disable the given hook categories:
+
+        - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
+        - on HOOKS_ALLOW_ALL mode, ensure those categories are disabled
+        """
         changes = set()
         if self.hooks_mode is self.HOOKS_DENY_ALL:
-            enablecats = self.enabled_hooks_categories
+            enablecats = self.enabled_hook_categories
             for category in categories:
                 if category in enablecats:
                     enablecats.remove(category)
                     changes.add(category)
         else:
-            disablecats = self.disabled_hooks_categories
+            disablecats = self.disabled_hook_categories
             for category in categories:
                 if category not in disablecats:
                     disablecats.add(category)
                     changes.add(category)
         return tuple(changes)
 
-    def enable_hooks_category(self, *categories):
+    def enable_hook_categories(self, *categories):
+        """enable the given hook categories:
+
+        - on HOOKS_DENY_ALL mode, ensure those categories are enabled
+        - on HOOKS_ALLOW_ALL mode, ensure those categories are not disabled
+        """
         changes = set()
         if self.hooks_mode is self.HOOKS_DENY_ALL:
-            enablecats = self.enabled_hooks_categories
+            enablecats = self.enabled_hook_categories
             for category in categories:
                 if category not in enablecats:
                     enablecats.add(category)
                     changes.add(category)
         else:
-            disablecats = self.disabled_hooks_categories
+            disablecats = self.disabled_hook_categories
             for category in categories:
-                if category in self.disabled_hooks_categories:
+                if category in self.disabled_hook_categories:
                     disablecats.remove(category)
                     changes.add(category)
         return tuple(changes)
 
-    def is_hooks_category_activated(self, category):
+    def is_hook_category_activated(self, category):
+        """return a boolean telling if the given category is currently activated
+        or not
+        """
         if self.hooks_mode is self.HOOKS_DENY_ALL:
-            return category in self.enabled_hooks_categories
-        return category not in self.disabled_hooks_categories
+            return category in self.enabled_hook_categories
+        return category not in self.disabled_hook_categories
 
     def is_hook_activated(self, hook):
-        return self.is_hooks_category_activated(hook.category)
+        """return a boolean telling if the given hook class is currently
+        activated or not
+        """
+        return self.is_hook_category_activated(hook.category)
 
     # connection management ###################################################
 
@@ -878,7 +894,7 @@
         self.user.req = self # XXX remove when "vreg = user.req.vreg" hack in entity.py is gone
         self.cnxtype = 'inmemory'
         self.is_internal_session = True
-        self.disable_hooks_category('integrity')
+        self.disable_hook_categories('integrity')
 
 
 class InternalManager(object):
--- a/sobjects/supervising.py	Tue Mar 09 08:59:43 2010 +0100
+++ b/sobjects/supervising.py	Tue Mar 09 11:00:48 2010 +0100
@@ -130,16 +130,15 @@
 
     def _relation_context(self, changedescr):
         session = self._cw
-        _ = session._cw._
         def describe(eid):
             try:
-                return _(session.describe(eid)[0]).lower()
+                return session._(session.describe(eid)[0]).lower()
             except UnknownEid:
                 # may occurs when an entity has been deleted from an external
                 # source and we're cleaning its relation
-                return _('unknown external entity')
+                return session._('unknown external entity')
         eidfrom, rtype, eidto = changedescr.eidfrom, changedescr.rtype, changedescr.eidto
-        return {'rtype': _(rtype),
+        return {'rtype': session._(rtype),
                 'eidfrom': eidfrom,
                 'frometype': describe(eidfrom),
                 'eidto': eidto,