[hooks] deprecates hookhelper module
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 14 Aug 2009 11:13:18 +0200
changeset 2840 06daf13195d4
parent 2839 6419af16faa0
child 2841 107ba1c45227
[hooks] deprecates hookhelper module
server/hook.py
server/hookhelper.py
server/session.py
--- a/server/hook.py	Fri Aug 14 11:12:26 2009 +0200
+++ b/server/hook.py	Fri Aug 14 11:13:18 2009 +0200
@@ -61,6 +61,10 @@
 class HooksRegistry(CWRegistry):
 
     def register(self, obj, **kwargs):
+        try:
+            iter(obj.events)
+        except:
+            raise Exception('bad .events attribute %s on %s' % (obj.event, obj))
         for event in obj.events:
             if event not in ALL_HOOKS:
                 raise Exception('bad event %s on %s' % (event, obj))
@@ -209,10 +213,12 @@
 
     def __init__(self, session, **kwargs):
         self.session = session
+        # XXX deprecates
         self.user = session.user
         self.repo = session.repo
         self.schema = session.repo.schema
         self.config = session.repo.config
+        # end deprecate
         self.__dict__.update(kwargs)
         self.register(session)
         # execution information
@@ -309,3 +315,27 @@
     """
     def insert_index(self):
         return None
+
+
+class SendMailOp(SingleLastOperation):
+    def __init__(self, session, msg=None, recipients=None, **kwargs):
+        # may not specify msg yet, as
+        # `cubicweb.sobjects.supervision.SupervisionMailOp`
+        if msg is not None:
+            assert recipients
+            self.to_send = [(msg, recipients)]
+        else:
+            assert recipients is None
+            self.to_send = []
+        super(SendMailOp, self).__init__(session, **kwargs)
+
+    def register(self, session):
+        previous = super(SendMailOp, self).register(session)
+        if previous:
+            self.to_send = previous.to_send + self.to_send
+
+    def commit_event(self):
+        self.repo.threaded_task(self.sendmails)
+
+    def sendmails(self):
+        self.config.sendmails(self.to_send)
--- a/server/hookhelper.py	Fri Aug 14 11:12:26 2009 +0200
+++ b/server/hookhelper.py	Fri Aug 14 11:13:18 2009 +0200
@@ -7,82 +7,21 @@
 """
 __docformat__ = "restructuredtext en"
 
-from cubicweb import RepositoryError
-from cubicweb.server.pool import SingleLastOperation
-
-
-def entity_name(session, eid):
-    """return the "name" attribute of the entity with the given eid"""
-    return entity_attr(session, eid, 'name')
-
-def entity_attr(session, eid, attr):
-    """return an arbitrary attribute of the entity with the given eid"""
-    return getattr(session.entity_from_eid(eid), attr)
+from logilab.common.deprecation import deprecated, class_moved
 
-def rproperty(session, rtype, eidfrom, eidto, rprop):
-    rschema = session.repo.schema[rtype]
-    subjtype = session.describe(eidfrom)[0]
-    objtype = session.describe(eidto)[0]
-    return rschema.rproperty(subjtype, objtype, rprop)
-
-def check_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)
-    if name in internal_names:
-        raise RepositoryError('%s entity can\'t be deleted' % name)
-    return name
-
-def get_user_sessions(repo, ueid):
-    for session in repo._sessions.values():
-        if ueid == session.user.eid:
-            yield session
+from cubicweb import RepositoryError
 
 
-# mail related ################################################################
-
-class SendMailOp(SingleLastOperation):
-    def __init__(self, session, msg=None, recipients=None, **kwargs):
-        # may not specify msg yet, as
-        # `cubicweb.sobjects.supervision.SupervisionMailOp`
-        if msg is not None:
-            assert recipients
-            self.to_send = [(msg, recipients)]
-        else:
-            assert recipients is None
-            self.to_send = []
-        super(SendMailOp, self).__init__(session, **kwargs)
-
-    def register(self, session):
-        previous = super(SendMailOp, self).register(session)
-        if previous:
-            self.to_send = previous.to_send + self.to_send
-
-    def commit_event(self):
-        self.repo.threaded_task(self.sendmails)
+@deprecated('[3.5] entity_name is deprecated, use entity.name')
+def entity_name(session, eid):
+    """return the "name" attribute of the entity with the given eid"""
+    return session.entity_from_eid(eid).name
 
-    def sendmails(self):
-        self.config.sendmails(self.to_send)
-
-
-# state related ###############################################################
+@deprecated('[3.5] rproperty is deprecated, use session.schema_rproperty')
+def rproperty(session, rtype, eidfrom, eidto, rprop):
+    return session.rproperty(rtype, eidfrom, eidto, rprop)
 
-def previous_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
-    """
-    if eid in session.transaction_data.get('neweids', ()):
-        return
-    pending = session.transaction_data.get('pendingrelations', ())
-    for eidfrom, rtype, eidto in reversed(pending):
-        if rtype == 'in_state' and eidfrom == eid:
-            rset = session.execute('Any S,N WHERE S eid %(x)s, S name N',
-                                   {'x': eidto}, 'x')
-            return rset.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')
-    if rset:
-        return rset.get_entity(0, 0)
+from cubicweb.server.hook import SendMailOp
+from cubicweb.hooks.workflow import previous_state
+SendMailOp = class_moved(SendMailOp)
+previous_state = deprecated('[3.5] use cubicweb.hooks.workflow.previous_state')(previous_state)
--- a/server/session.py	Fri Aug 14 11:12:26 2009 +0200
+++ b/server/session.py	Fri Aug 14 11:13:18 2009 +0200
@@ -169,6 +169,18 @@
         assert prop == 'lang' # this is the only one changeable property for now
         self.set_language(value)
 
+    def deleted_in_transaction(self, eid):
+        return eid in self.transaction_data.get('pendingeids', ())
+
+    def added_in_transaction(self, eid):
+        return eid in self.transaction_data.get('neweids', ())
+
+    def schema_rproperty(self, rtype, eidfrom, eidto, rprop):
+        rschema = self.repo.schema[rtype]
+        subjtype = self.describe(eidfrom)[0]
+        objtype = self.describe(eidto)[0]
+        return rschema.rproperty(subjtype, objtype, rprop)
+
     # connection management ###################################################
 
     def keep_pool_mode(self, mode):