[server] make internal_cnx get and keep a connection to the db
authorJulien Cristau <julien.cristau@logilab.fr>
Thu, 10 Apr 2014 11:04:13 +0200
changeset 9685 0033aa71e077
parent 9684 a6b084ea4c5b
child 9686 9a04e48e780b
[server] make internal_cnx get and keep a connection to the db - internal_session worked that way - it's less surprising for users if their internal connections "just work", rather than having to deal with the cnxset when using low-level APIs Includes a backout of 3f62606c01a2 "[repo] Fix register_user" which is no longer needed. Depend on logilab-database 1.12.1 to avoid errors due to pointless check in python's sqlite3 binding.
__pkginfo__.py
cubicweb.spec
debian/control
server/repository.py
--- a/__pkginfo__.py	Tue Apr 22 14:43:11 2014 +0200
+++ b/__pkginfo__.py	Thu Apr 10 11:04:13 2014 +0200
@@ -50,7 +50,7 @@
     'Twisted': '',
     # XXX graphviz
     # server dependencies
-    'logilab-database': '>= 1.12.0',
+    'logilab-database': '>= 1.12.1',
     'passlib': '',
     }
 
--- a/cubicweb.spec	Tue Apr 22 14:43:11 2014 +0200
+++ b/cubicweb.spec	Thu Apr 10 11:04:13 2014 +0200
@@ -24,7 +24,7 @@
 Requires:       %{python}-logilab-mtconverter >= 0.8.0
 Requires:       %{python}-rql >= 0.31.2
 Requires:       %{python}-yams >= 0.39.1
-Requires:       %{python}-logilab-database >= 1.12.0
+Requires:       %{python}-logilab-database >= 1.12.1
 Requires:       %{python}-passlib
 Requires:       %{python}-lxml
 Requires:       %{python}-twisted-web
--- a/debian/control	Tue Apr 22 14:43:11 2014 +0200
+++ b/debian/control	Thu Apr 10 11:04:13 2014 +0200
@@ -52,7 +52,7 @@
  ${python:Depends},
  cubicweb-common (= ${source:Version}),
  cubicweb-ctl (= ${source:Version}),
- python-logilab-database (>= 1.12.0),
+ python-logilab-database (>= 1.12.1),
  cubicweb-postgresql-support
  | cubicweb-mysql-support
  | python-pysqlite2,
--- a/server/repository.py	Tue Apr 22 14:43:11 2014 +0200
+++ b/server/repository.py	Thu Apr 10 11:04:13 2014 +0200
@@ -606,11 +606,10 @@
         This is a public method, not requiring a session id.
         """
         with self.internal_cnx() as cnx:
-            with cnx.ensure_cnx_set:
-                # don't use cnx.execute, we don't want rset.req set
-                return self.querier.execute(cnx, 'Any K,V WHERE P is CWProperty,'
-                                            'P pkey K, P value V, NOT P for_user U',
-                                            build_descr=False)
+            # don't use cnx.execute, we don't want rset.req set
+            return self.querier.execute(cnx, 'Any K,V WHERE P is CWProperty,'
+                                        'P pkey K, P value V, NOT P for_user U',
+                                        build_descr=False)
 
     # XXX protect this method: anonymous should be allowed and registration
     # plugged
@@ -620,35 +619,34 @@
         registration on public web site.
         """
         with self.internal_cnx() as cnx:
-            with cnx.ensure_cnx_set:
-                # for consistency, keep same error as unique check hook (although not required)
-                errmsg = cnx._('the value "%s" is already used, use another one')
-                if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
-                                build_descr=False)
-                    or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
-                                   {'login': login}, build_descr=False)):
-                    qname = role_name('login', 'subject')
-                    raise ValidationError(None, {qname: errmsg % login})
-                # we have to create the user
-                user = self.vreg['etypes'].etype_class('CWUser')(cnx)
-                if isinstance(password, unicode):
-                    # password should *always* be utf8 encoded
-                    password = password.encode('UTF8')
-                kwargs['login'] = login
-                kwargs['upassword'] = password
-                self.glob_add_entity(cnx, EditedEntity(user, **kwargs))
-                cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"',
-                            {'x': user.eid})
-                if email or '@' in login:
-                    d = {'login': login, 'email': email or login}
-                    if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
-                                   build_descr=False):
-                        qname = role_name('address', 'subject')
-                        raise ValidationError(None, {qname: errmsg % d['email']})
-                    cnx.execute('INSERT EmailAddress X: X address %(email)s, '
-                                'U primary_email X, U use_email X '
-                                'WHERE U login %(login)s', d, build_descr=False)
-                cnx.commit()
+            # for consistency, keep same error as unique check hook (although not required)
+            errmsg = cnx._('the value "%s" is already used, use another one')
+            if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
+                            build_descr=False)
+                or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
+                               {'login': login}, build_descr=False)):
+                qname = role_name('login', 'subject')
+                raise ValidationError(None, {qname: errmsg % login})
+            # we have to create the user
+            user = self.vreg['etypes'].etype_class('CWUser')(cnx)
+            if isinstance(password, unicode):
+                # password should *always* be utf8 encoded
+                password = password.encode('UTF8')
+            kwargs['login'] = login
+            kwargs['upassword'] = password
+            self.glob_add_entity(cnx, EditedEntity(user, **kwargs))
+            cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"',
+                        {'x': user.eid})
+            if email or '@' in login:
+                d = {'login': login, 'email': email or login}
+                if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
+                               build_descr=False):
+                    qname = role_name('address', 'subject')
+                    raise ValidationError(None, {qname: errmsg % d['email']})
+                cnx.execute('INSERT EmailAddress X: X address %(email)s, '
+                            'U primary_email X, U use_email X '
+                            'WHERE U login %(login)s', d, build_descr=False)
+            cnx.commit()
         return True
 
     def find_users(self, fetch_attrs, **query_attrs):
@@ -950,8 +948,11 @@
         """
         with InternalSession(self) as session:
             with session.new_cnx() as cnx:
+                # equivalent to cnx.security_enabled(False, False) because
+                # InternalSession gives full read access
                 with cnx.allow_all_hooks_but('security'):
-                    yield cnx
+                    with cnx.ensure_cnx_set:
+                        yield cnx
 
 
     def _get_session(self, sessionid, setcnxset=False, txid=None,