Fix AttributeError for "lang" on repo/client connections 3.24
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 17 Nov 2016 10:53:04 +0100
branch3.24
changeset 11824 d7ecf6dab085
parent 11823 4f43e64603ef
child 11825 52acf0dbf4cd
Fix AttributeError for "lang" on repo/client connections This is a regression introduced by b48020a80dc3, which removed call to set_language from the `req._set_user` method. This is fine, but we still want a language on connections, so we've to handle it if necesary in Connection.__enter__ (i.e. once connection is properly open). When using pyramid front-end, which has a users cache, we've to cache its language as well because we must not access its preferred_language method since it's not bound to a proper connection.
cubicweb/pyramid/core.py
cubicweb/pyramid/tools.py
cubicweb/req.py
cubicweb/server/session.py
cubicweb/test/unittest_repoapi.py
--- a/cubicweb/pyramid/core.py	Thu Nov 17 10:47:52 2016 +0100
+++ b/cubicweb/pyramid/core.py	Thu Nov 17 10:53:04 2016 +0100
@@ -32,6 +32,7 @@
     def __init__(self, session, *args, **kw):
         super(Connection, self).__init__(session, *args, **kw)
         self._session = session
+        self.lang = session._cached_lang
 
     def _get_session_data(self):
         return self._session.data
@@ -272,8 +273,9 @@
     """A lightweight version of
     :meth:`cubicweb.server.repository.Repository.connect` that does not keep
     track of opened sessions, removing the need of closing them"""
-    user = tools.cached_build_user(repo, eid)
+    user, lang = tools.cached_build_user(repo, eid)
     session = Session(request, user, repo)
+    session._cached_lang = lang
     tools.cnx_attach_entity(session, user)
     # Calling the hooks should be done only once, disabling it completely for
     # now
--- a/cubicweb/pyramid/tools.py	Thu Nov 17 10:47:52 2016 +0100
+++ b/cubicweb/pyramid/tools.py	Thu Nov 17 10:53:04 2016 +0100
@@ -45,19 +45,21 @@
     """
     with repo.internal_cnx() as cnx:
         if eid in _user_cache:
-            entity = clone_user(repo, _user_cache[eid])
+            user, lang = _user_cache[eid]
+            entity = clone_user(repo, user)
             # XXX the cnx is needed here so that the CWUser instance has an
             # access to the vreg, which it needs when its 'prefered_language'
             # property is accessed.
             # If this property did not need a cnx to access a vreg, we could
             # avoid the internal_cnx() and save more time.
             cnx_attach_entity(cnx, entity)
-            return entity
+            return entity, lang
 
         user = repo._build_user(cnx, eid)
+        lang = user.prefered_language()
         user.cw_clear_relation_cache()
-        _user_cache[eid] = clone_user(repo, user)
-        return user
+        _user_cache[eid] = (clone_user(repo, user), lang)
+        return user, lang
 
 
 def clear_cache():
--- a/cubicweb/req.py	Thu Nov 17 10:47:52 2016 +0100
+++ b/cubicweb/req.py	Thu Nov 17 10:53:04 2016 +0100
@@ -74,6 +74,7 @@
         # should be emptied on commit/rollback of the server session / web
         # connection
         self.user = None
+        self.lang = None
         self.local_perm_cache = {}
         self._ = text_type
 
--- a/cubicweb/server/session.py	Thu Nov 17 10:47:52 2016 +0100
+++ b/cubicweb/server/session.py	Thu Nov 17 10:53:04 2016 +0100
@@ -384,6 +384,8 @@
         assert not self._open
         self._open = True
         self.cnxset = self.repo._get_cnxset()
+        if self.lang is None:
+            self.set_language(self.user.prefered_language())
         return self
 
     def __exit__(self, exctype=None, excvalue=None, tb=None):
--- a/cubicweb/test/unittest_repoapi.py	Thu Nov 17 10:47:52 2016 +0100
+++ b/cubicweb/test/unittest_repoapi.py	Thu Nov 17 10:53:04 2016 +0100
@@ -83,6 +83,13 @@
             rset = cnx.execute('Any X WHERE X is CWUser')
             self.assertTrue(rset)
 
+    def test_cnx_has_lang(self):
+        """check that client and repo cnx have .lang set"""
+        with self.admin_access.client_cnx() as cnx:
+            self.assertEqual(cnx.lang, 'en')
+        with self.admin_access.repo_cnx() as cnx:
+            self.assertEqual(cnx.lang, 'en')
+
 
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main