[Scodoc-devel] [SVN] Scolar : [1203] - Acces base utilisateurs avec psycopg2.

eviennet at lipn.univ-paris13.fr eviennet at lipn.univ-paris13.fr
Mar 12 Mar 23:57:32 CET 2013


Une pièce jointe HTML a été nettoyée...
URL: <https://www-rt.iutv.univ-paris13.fr/pipermail/scodoc-devel/attachments/20130312/6cf8d102/attachment.html>
-------------- section suivante --------------
Modified: trunk/ZScoUsers.py
===================================================================
--- trunk/ZScoUsers.py	2013-03-12 22:28:03 UTC (rev 1202)
+++ trunk/ZScoUsers.py	2013-03-12 22:57:32 UTC (rev 1203)
@@ -49,6 +49,7 @@
 
 # ---------------
 
+import notesdb
 from notesdb import *
 from notes_log import log
 from scolog import logdb
@@ -113,9 +114,9 @@
 
     # no permissions, only called from python
     def __init__(self, id, title):
-	"initialise a new instance"
+        "initialise a new instance"
         self.id = id
-	self.title = title
+        self.title = title
     
     # The form used to edit this object
     def manage_editZScousers(self, title, RESPONSE=None):
@@ -131,20 +132,13 @@
         f.close()     
         self.manage_addDTMLMethod(id,title,file)
 
-    security.declareProtected('Change DTML Documents', 'GetUsersDBConnexion')
-    def GetUsersDBConnexion(self,new=False):
-        # not published
-        try:
-            # a database adaptor called UsersDB must exist
-            self.UsersDB().encoding = 'LATIN1'
-            cnx = self.UsersDB().getconn(init=False)
-        except:
-            # backward compat: try to use same DB (usually will not work !!!)
-            log('warning: ZScoUsers using Sco DB connexion')
-            cnx = self.GetDBConnexion() 
-        cnx.commit() # sync !
-        return cnx
+    # Connexion to SQL database of users:
 
+    # Ugly but necessary during transition out of Zope:
+    _db_cnx_string = "dbname=SCOUSERS port=5432"
+    security.declareProtected('Change DTML Documents', 'GetUsersDBConnexion')
+    GetUsersDBConnexion = notesdb.GetUsersDBConnexion
+    
     # --------------------------------------------------------------------
     #
     #   Users (top level)

Modified: trunk/notesdb.py
===================================================================
--- trunk/notesdb.py	2013-03-12 22:28:03 UTC (rev 1202)
+++ trunk/notesdb.py	2013-03-12 22:57:32 UTC (rev 1203)
@@ -4,6 +4,7 @@
 import pdb, os, sys, string
 import traceback
 import psycopg2
+import psycopg2.pool
 import thread
 from notes_log import log
 from sco_exceptions import *
@@ -32,18 +33,31 @@
 # et est par d\xE9faut en autocommit
 _pools = {}
 def GetDBConnexion(context, autocommit=True):
-    pool = _pools.get( context._db_cnx_string, None)
+    pool = _pools.get(context._db_cnx_string, None)
     if not pool:
         pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=context._db_cnx_string )
         _pools[context._db_cnx_string] = pool
-        log('GetDBConnexion: created pool for "%s"' % context._db_cnx_string)
+        # log('GetDBConnexion: created pool for "%s"' % context._db_cnx_string)
     cnx = pool.getconn(key=(thread.get_ident(),autocommit))
     #log('GetDBConnexion: autocommit=%s cnx=%s' % (autocommit,cnx))
     if cnx.autocommit != autocommit:
         cnx.autocommit = autocommit
     return cnx
 
+# Same for users:
+_users_pools = {}
+def GetUsersDBConnexion(context, autocommit=True):
+    pool = _users_pools.get(context._db_cnx_string, None)
+    if not pool:
+        pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=context._db_cnx_string )
+        _users_pools[context._db_cnx_string] = pool
+        log('GetUsersDBConnexion: created pool for "%s"' % context._db_cnx_string)
+    cnx = pool.getconn(key=(thread.get_ident(),autocommit))
+    if cnx.autocommit != autocommit:
+        cnx.autocommit = autocommit
+    return cnx
 
+
 class ScoDocCursor(psycopg2.extensions.cursor):
     """A database cursor emulating some methods of psycopg v1 cursors"""
     def dictfetchall(cursor):


Plus d'informations sur la liste de diffusion Scodoc-devel