[hooks/syncsession] try to remove cnx vs session confusion
authorJulien Cristau <julien.cristau@logilab.fr>
Tue, 22 Jul 2014 17:45:38 +0200
changeset 9913 a74e9e43806a
parent 9896 4900a937838b
child 9914 30b56a56c1f4
[hooks/syncsession] try to remove cnx vs session confusion
hooks/syncsession.py
--- a/hooks/syncsession.py	Fri Jul 18 16:44:44 2014 +0200
+++ b/hooks/syncsession.py	Tue Jul 22 17:45:38 2014 +0200
@@ -42,15 +42,15 @@
     """base class for group operation"""
     cnxuser = None # make pylint happy
 
-    def __init__(self, session, *args, **kwargs):
+    def __init__(self, cnx, *args, **kwargs):
         """override to get the group name before actual groups manipulation:
 
         we may temporarily loose right access during a commit event, so
         no query should be emitted while comitting
         """
         rql = 'Any N WHERE G eid %(x)s, G name N'
-        result = session.execute(rql, {'x': kwargs['geid']}, build_descr=False)
-        hook.Operation.__init__(self, session, *args, **kwargs)
+        result = cnx.execute(rql, {'x': kwargs['geid']}, build_descr=False)
+        hook.Operation.__init__(self, cnx, *args, **kwargs)
         self.group = result[0][0]
 
 
@@ -94,14 +94,14 @@
 
 class _DelUserOp(hook.Operation):
     """close associated user's session when it is deleted"""
-    def __init__(self, session, cnxid):
-        self.cnxid = cnxid
-        hook.Operation.__init__(self, session)
+    def __init__(self, cnx, sessionid):
+        self.sessionid = sessionid
+        hook.Operation.__init__(self, cnx)
 
     def postcommit_event(self):
         """the observed connections set has been commited"""
         try:
-            self.session.repo.close(self.cnxid)
+            self.cnx.repo.close(self.sessionid)
         except BadConnectionId:
             pass # already closed
 
@@ -148,7 +148,7 @@
         """the observed connections set has been commited"""
         cwprop = self.cwprop
         if not cwprop.for_user:
-            self.session.vreg['propertyvalues'][cwprop.pkey] = cwprop.value
+            self.cnx.vreg['propertyvalues'][cwprop.pkey] = cwprop.value
         # if for_user is set, update is handled by a ChangeCWPropertyOp operation
 
 
@@ -161,19 +161,19 @@
         key, value = self.entity.pkey, self.entity.value
         if key.startswith('sources.'):
             return
-        session = self._cw
+        cnx = self._cw
         try:
-            value = session.vreg.typed_value(key, value)
+            value = cnx.vreg.typed_value(key, value)
         except UnknownProperty:
             msg = _('unknown property key %s')
             raise validation_error(self.entity, {('pkey', 'subject'): msg}, (key,))
         except ValueError as ex:
             raise validation_error(self.entity,
                                   {('value', 'subject'): str(ex)})
-        if not session.user.matching_groups('managers'):
-            session.add_relation(self.entity.eid, 'for_user', session.user.eid)
+        if not cnx.user.matching_groups('managers'):
+            cnx.add_relation(self.entity.eid, 'for_user', cnx.user.eid)
         else:
-            _AddCWPropertyOp(session, cwprop=self.entity)
+            _AddCWPropertyOp(cnx, cwprop=self.entity)
 
 
 class UpdateCWPropertyHook(AddCWPropertyHook):
@@ -188,20 +188,20 @@
         key, value = entity.pkey, entity.value
         if key.startswith('sources.'):
             return
-        session = self._cw
+        cnx = self._cw
         try:
-            value = session.vreg.typed_value(key, value)
+            value = cnx.vreg.typed_value(key, value)
         except UnknownProperty:
             return
         except ValueError as ex:
             raise validation_error(entity, {('value', 'subject'): str(ex)})
         if entity.for_user:
-            for session_ in get_user_sessions(session.repo, entity.for_user[0].eid):
-                _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties,
+            for session in get_user_sessions(cnx.repo, entity.for_user[0].eid):
+                _ChangeCWPropertyOp(cnx, cwpropdict=session.user.properties,
                                     key=key, value=value)
         else:
             # site wide properties
-            _ChangeCWPropertyOp(session, cwpropdict=session.vreg['propertyvalues'],
+            _ChangeCWPropertyOp(cnx, cwpropdict=cnx.vreg['propertyvalues'],
                               key=key, value=value)
 
 
@@ -211,13 +211,13 @@
 
     def __call__(self):
         eid = self.entity.eid
-        session = self._cw
-        for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()):
+        cnx = self._cw
+        for eidfrom, rtype, eidto in cnx.transaction_data.get('pendingrelations', ()):
             if rtype == 'for_user' and eidfrom == self.entity.eid:
                 # if for_user was set, delete has already been handled
                 break
         else:
-            _DelCWPropertyOp(session, cwpropdict=session.vreg['propertyvalues'],
+            _DelCWPropertyOp(cnx, cwpropdict=cnx.vreg['propertyvalues'],
                              key=self.entity.pkey)
 
 
@@ -227,17 +227,17 @@
     events = ('after_add_relation',)
 
     def __call__(self):
-        session = self._cw
+        cnx = self._cw
         eidfrom = self.eidfrom
-        if not session.entity_metas(eidfrom)['type'] == 'CWProperty':
+        if not cnx.entity_metas(eidfrom)['type'] == 'CWProperty':
             return
-        key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V',
+        key, value = cnx.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V',
                                      {'x': eidfrom})[0]
-        if session.vreg.property_info(key)['sitewide']:
+        if cnx.vreg.property_info(key)['sitewide']:
             msg = _("site-wide property can't be set for user")
             raise validation_error(eidfrom, {('for_user', 'subject'): msg})
-        for session_ in get_user_sessions(session.repo, self.eidto):
-            _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties,
+        for session in get_user_sessions(cnx.repo, self.eidto):
+            _ChangeCWPropertyOp(cnx, cwpropdict=session.user.properties,
                               key=key, value=value)
 
 
@@ -246,10 +246,10 @@
     events = ('after_delete_relation',)
 
     def __call__(self):
-        session = self._cw
-        key = session.execute('Any K WHERE P eid %(x)s, P pkey K',
+        cnx = self._cw
+        key = cnx.execute('Any K WHERE P eid %(x)s, P pkey K',
                               {'x': self.eidfrom})[0][0]
-        session.transaction_data.setdefault('pendingrelations', []).append(
+        cnx.transaction_data.setdefault('pendingrelations', []).append(
             (self.eidfrom, self.rtype, self.eidto))
-        for session_ in get_user_sessions(session.repo, self.eidto):
-            _DelCWPropertyOp(session, cwpropdict=session_.user.properties, key=key)
+        for session in get_user_sessions(cnx.repo, self.eidto):
+            _DelCWPropertyOp(cnx, cwpropdict=session.user.properties, key=key)