[server] drop repo.internal_session and InternalSession
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 22 Apr 2015 18:28:58 +0200
changeset 10345 ef54ea75a642
parent 10342 28db21e0c8e5
child 10346 b926ff4ef4a8
[server] drop repo.internal_session and InternalSession
server/repository.py
server/session.py
server/test/unittest_session.py
--- a/server/repository.py	Wed Apr 22 17:45:27 2015 +0200
+++ b/server/repository.py	Wed Apr 22 18:28:58 2015 +0200
@@ -49,7 +49,7 @@
                       UniqueTogetherError, onevent)
 from cubicweb import cwvreg, schema, server
 from cubicweb.server import ShuttingDown, utils, hook, querier, sources
-from cubicweb.server.session import Session, InternalSession, InternalManager
+from cubicweb.server.session import Session, InternalManager
 from cubicweb.server.ssplanner import EditedEntity
 
 NO_CACHE_RELATIONS = set( [('owned_by', 'object'),
@@ -627,14 +627,14 @@
         for k in chain(fetch_attrs, query_attrs):
             if k not in cwuserattrs:
                 raise Exception('bad input for find_user')
-        with self.internal_session() as session:
+        with self.internal_cnx() as cnx:
             varmaker = rqlvar_maker()
             vars = [(attr, varmaker.next()) for attr in fetch_attrs]
             rql = 'Any %s WHERE X is CWUser, ' % ','.join(var[1] for var in vars)
             rql += ','.join('X %s %s' % (var[0], var[1]) for var in vars) + ','
-            rset = session.execute(rql + ','.join('X %s %%(%s)s' % (attr, attr)
-                                                  for attr in query_attrs),
-                                   query_attrs)
+            rset = cnx.execute(rql + ','.join('X %s %%(%s)s' % (attr, attr)
+                                              for attr in query_attrs),
+                               query_attrs)
             return rset.rows
 
     def new_session(self, login, **kwargs):
@@ -710,27 +710,6 @@
                 nbclosed += 1
         return nbclosed
 
-    @deprecated("[3.19] use internal_cnx now\n"
-                "(Beware that integrity hook are now enabled by default)")
-    def internal_session(self, cnxprops=None, safe=False):
-        """return a dbapi like connection/cursor using internal user which have
-        every rights on the repository. The `safe` argument is a boolean flag
-        telling if integrity hooks should be activated or not.
-
-        /!\ the safe argument is False by default.
-
-        *YOU HAVE TO* commit/rollback or close (rollback implicitly) the
-        session once the job's done, else you'll leak connections set up to the
-        time where no one is available, causing irremediable freeze...
-        """
-        session = InternalSession(self, cnxprops)
-        if not safe:
-            session.disable_hook_categories('integrity')
-        session.disable_hook_categories('security')
-        session._cnx.ctx_count += 1
-        session.set_cnxset()
-        return session
-
     @contextmanager
     def internal_cnx(self):
         """Context manager returning a Connection using internal user which have
@@ -739,7 +718,7 @@
         Beware that unlike the older :meth:`internal_session`, internal
         connections have all hooks beside security enabled.
         """
-        with InternalSession(self) as session:
+        with Session(InternalManager(), self) as session:
             with session.new_cnx() as cnx:
                 with cnx.security_enabled(read=False, write=False):
                     with cnx.ensure_cnx_set:
--- a/server/session.py	Wed Apr 22 17:45:27 2015 +0200
+++ b/server/session.py	Wed Apr 22 18:28:58 2015 +0200
@@ -34,7 +34,6 @@
 from cubicweb.req import RequestSessionBase
 from cubicweb.utils import make_uid
 from cubicweb.rqlrewrite import RQLRewriter
-from cubicweb.server import ShuttingDown
 from cubicweb.server.edition import EditedEntity
 
 
@@ -483,7 +482,7 @@
         #: is this connection from a client or internal to the repo
         self.running_dbapi_query = True
         # internal (root) session
-        self.is_internal_session = session.is_internal_session
+        self.is_internal_session = isinstance(session.user, InternalManager)
 
         #: dict containing arbitrary data cleared at the end of the transaction
         self.transaction_data = {}
@@ -506,7 +505,7 @@
 
         # undo control
         config = session.repo.config
-        if config.creating or config.repairing or session.is_internal_session:
+        if config.creating or config.repairing or self.is_internal_session:
             self.undo_actions = False
         else:
             self.undo_actions = config['undo-enabled']
@@ -1340,7 +1339,6 @@
 
     """
     is_request = False
-    is_internal_session = False
 
     def __init__(self, user, repo, cnxprops=None, _id=None):
         super(Session, self).__init__(repo.vreg)
@@ -1747,37 +1745,12 @@
 Session.DEFAULT_SECURITY = DEFAULT_SECURITY
 
 
-
-class InternalSession(Session):
-    """special session created internally by the repository"""
-    is_internal_session = True
-    running_dbapi_query = False
-
-    def __init__(self, repo, cnxprops=None, safe=False):
-        super(InternalSession, self).__init__(InternalManager(), repo, cnxprops,
-                                              _id='internal')
-        self.user._cw = self # XXX remove when "vreg = user._cw.vreg" hack in entity.py is gone
-
-    def __enter__(self):
-        return self
-
-    def __exit__(self, exctype, excvalue, tb):
-        self.close()
-
-    @property
-    def cnxset(self):
-        """connections set, set according to transaction mode for each query"""
-        if self.repo.shutting_down:
-            self.free_cnxset(True)
-            raise ShuttingDown('repository is shutting down')
-        return self._cnx.cnxset
-
-
 class InternalManager(object):
     """a manager user with all access rights used internally for task such as
     bootstrapping the repository or creating regular users according to
     repository content
     """
+
     def __init__(self, lang='en'):
         self.eid = -1
         self.login = u'__internal_manager__'
--- a/server/test/unittest_session.py	Wed Apr 22 17:45:27 2015 +0200
+++ b/server/test/unittest_session.py	Wed Apr 22 18:28:58 2015 +0200
@@ -21,22 +21,6 @@
 from cubicweb.server import hook
 from cubicweb.predicates import is_instance
 
-class InternalSessionTC(CubicWebTC):
-    def test_dbapi_query(self):
-        session = self.repo.internal_session()
-        self.assertFalse(session.running_dbapi_query)
-        session.close()
-
-    def test_integrity_hooks(self):
-        with self.repo.internal_session() as session:
-            self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
-            self.assertEqual(set(('integrity', 'security')), session.disabled_hook_categories)
-            self.assertEqual(set(), session.enabled_hook_categories)
-            session.commit()
-            self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
-            self.assertEqual(set(('integrity', 'security')), session.disabled_hook_categories)
-            self.assertEqual(set(), session.enabled_hook_categories)
-
 class SessionTC(CubicWebTC):
 
     def test_hooks_control(self):