[Erp5-report] r16123 - /erp5/trunk/products/ERP5Type/patches/DA.py

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Sep 6 16:49:28 CEST 2007


Author: aurel
Date: Thu Sep  6 16:49:27 2007
New Revision: 16123

URL: http://svn.erp5.org?rev=16123&view=rev
Log:
patch __call__ in order either to give connection id to zsqlmethod or
to retrieved it from preference if in skins folder

Modified:
    erp5/trunk/products/ERP5Type/patches/DA.py

Modified: erp5/trunk/products/ERP5Type/patches/DA.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/patches/DA.py?rev=16123&r1=16122&r2=16123&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/patches/DA.py (original)
+++ erp5/trunk/products/ERP5Type/patches/DA.py Thu Sep  6 16:49:27 2007
@@ -17,7 +17,14 @@
 try: from IOBTree import Bucket
 except: Bucket=lambda:{}
 from Shared.DC.ZRDB.Aqueduct import decodestring, parse
-from Shared.DC.ZRDB.DA import DA
+from Shared.DC.ZRDB.DA import DA, DatabaseError, SQLMethodTracebackSupplement
+from Shared.DC.ZRDB import RDB
+from Shared.DC.ZRDB.Results import Results
+from App.Extensions import getBrain
+from AccessControl import getSecurityManager
+from Acquisition import aq_base
+from zLOG import LOG, INFO, ERROR
+
 
 def DA_fromFile(self, filename):
   """
@@ -119,6 +126,125 @@
     return RESPONSE
 
 
+def DA__call__(self, REQUEST=None, __ick__=None, src__=0, test__=0, **kw):
+    """Call the database method
+
+    The arguments to the method should be passed via keyword
+    arguments, or in a single mapping object. If no arguments are
+    given, and if the method was invoked through the Web, then the
+    method will try to acquire and use the Web REQUEST object as
+    the argument mapping.
+
+    The returned value is a sequence of record objects.
+    """
+    __traceback_supplement__ = (SQLMethodTracebackSupplement, self)
+
+    c = kw.pop("connection_id", None)
+    #if c is not None:
+      #LOG("DA", 300, "connection %s provided to %s" %(c, self.id))
+      
+    if REQUEST is None:
+        if kw: REQUEST=kw
+        else:
+            if hasattr(self, 'REQUEST'): REQUEST=self.REQUEST
+            else: REQUEST={}
+
+    # Patch to implement dynamic connection id
+    # Connection id is retrieve from user preference
+    if c is None:
+      physical_path = self.getPhysicalPath()
+      if "portal_catalog" not in physical_path and 'cmf_activity' not in self.connection_id:
+        try:
+          archive_id = self.portal_preferences.getPreferredArchive()
+        except AttributeError:
+          pass
+        else:
+          if archive_id not in (None, ''):
+            archive_id = archive_id.split('/')[-1]
+            #LOG("DA__call__, archive_id 2", 300, archive_id)
+            archive = self.portal_archives._getOb(archive_id, None)
+            if archive is not None:
+              c = archive.getConnectionId()
+              #LOG("DA call", INFO, "retrieved connection %s from preference" %(c,))
+
+    if c is None:
+      # connection hook
+      c = self.connection_id
+      # for backwards compatability
+      hk = self.connection_hook
+      # go get the connection hook and call it
+      if hk: c = getattr(self, hk)()
+    #LOG("DA__call__ connection", 300, c)
+    try: dbc=getattr(self, c)
+    except AttributeError:
+        raise AttributeError, (
+            "The database connection <em>%s</em> cannot be found." % (
+            c))
+
+    try: DB__=dbc()
+    except: raise DatabaseError, (
+        '%s is not connected to a database' % self.id)
+
+    if hasattr(self, 'aq_parent'):
+        p=self.aq_parent
+        if self._isBeingAccessedAsZClassDefinedInstanceMethod():
+            p=p.aq_parent
+    else: p=None
+
+    argdata=self._argdata(REQUEST)
+    argdata['sql_delimiter']='\0'
+    argdata['sql_quote__']=dbc.sql_quote__
+
+    security=getSecurityManager()
+    security.addContext(self)
+    try:
+        try:     query=apply(self.template, (p,), argdata)
+        except TypeError, msg:
+            msg = str(msg)
+            if find(msg,'client') >= 0:
+                raise NameError("'client' may not be used as an " +
+                    "argument name in this context")
+            else: raise
+    finally: security.removeContext(self)
+
+    if src__: return query
+
+    if self.cache_time_ > 0 and self.max_cache_ > 0:
+        result=self._cached_result(DB__, (query, self.max_rows_))
+    else:
+      try:
+#         if 'portal_ids' in query:
+#           LOG("DA query", INFO, "query = %s" %(query,))
+        result=DB__.query(query, self.max_rows_)
+      except:
+        LOG("DA call raise", ERROR, "DB = %s, c = %s, query = %s" %(DB__, c, query))
+        raise
+
+    if hasattr(self, '_v_brain'): brain=self._v_brain
+    else:
+        brain=self._v_brain=getBrain(self.class_file_, self.class_name_)
+
+    zc=self._zclass
+    if zc is not None: zc=zc._zclass_
+
+    if type(result) is type(''):
+        f=StringIO()
+        f.write(result)
+        f.seek(0)
+        result=RDB.File(f,brain,p, zc)
+    else:
+        result=Results(result, brain, p, zc)
+    columns=result._searchable_result_columns()
+    if test__ and columns != self._col: self._col=columns
+
+    # If run in test mode, return both the query and results so
+    # that the template doesn't have to be rendered twice!
+    if test__: return query, result
+
+    return result
+
+      
+DA.__call__ = DA__call__
 DA.fromFile = DA_fromFile
 DA.fromText = DA_fromText
 DA.manage_FTPget = DA_manage_FTPget




More information about the Erp5-report mailing list