[Erp5-report] r11149 - /erp5/trunk/products/ERP5Form/

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Nov 7 12:06:58 CET 2006


Author: yo
Date: Tue Nov  7 12:06:53 2006
New Revision: 11149

URL: http://svn.erp5.org?rev=11149&view=rev
Log:
Change the way to store selection domains, because
it is unsafe to try to store acquisition wrappers in a persistent object.
This may have undesirable effect, because this change makes an acquisition
wrapper with getSelectionFor now.

Modified:
    erp5/trunk/products/ERP5Form/ListBox.py
    erp5/trunk/products/ERP5Form/Selection.py
    erp5/trunk/products/ERP5Form/SelectionTool.py

Modified: erp5/trunk/products/ERP5Form/ListBox.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/ListBox.py?rev=11149&r1=11148&r2=11149&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/ListBox.py (original)
+++ erp5/trunk/products/ERP5Form/ListBox.py Tue Nov  7 12:06:53 2006
@@ -839,6 +839,7 @@
     # Create a selection, if not present, with the default sort order.
     if selection is None:
       selection = Selection(params = self.getDefaultParamList(), default_sort_on = self.getDefaultSortColumnList())
+      selection = selection.__of__(selection_tool)
     # Or make sure all sort arguments are valid.
     else:
       # Reset the selection, if specified.
@@ -1070,19 +1071,15 @@
         if category_tool is not None:
           root = category_tool.restrictedTraverse(domain, None)
           if root is not None :
-            root_dict[base_domain] = root
+            root_dict[base_domain] = ('portal_categories', domain)
           elif domain_tool is not None:
             root = domain_tool.getDomainByPath(domain)
             if root is not None:
-              # FIXME: this is a bad hack. DomainSelection should use
-              # portal_type to determine the type of the object instead
-              # of whether it is a string or not.
-              root_dict[base_domain] = domain
+              root_dict[base_domain] = ('portal_domains', domain)
         if root is None:
-          try:
-            root_dict[None] = portal_object.restrictedTraverse(domain)
-          except KeyError:
-            pass
+          root = portal_object.restrictedTraverse(domain, None)
+          if root is not None:
+            root_dict[None] = (None, domain)
 
       return DomainSelection(domain_dict = root_dict).__of__(self.getContext())
 
@@ -1142,31 +1139,31 @@
         root = None
         if category_tool is not None:
           try:
-            obj = category_tool[category]
             if category == 'parent':
               # parent has a special treatment
-              root = root_dict[category] = root_dict[None] = self.getContext()
-              report_path = report_path[1:]
+              root = self.getContext()
+              root_dict[category] = root_dict[None] = (root, (None, root.getRelativeUrl()))
             else:
-              root = root_dict[category] = root_dict[None] = obj
-              report_path = report_path[1:]
+              root = category_tool[category]
+              root_dict[category] = root_dict[None] = (root, ('portal_categories', root.getRelativeUrl()))
+            report_path = report_path[1:]
           except KeyError:
             pass
         if root is None and domain_tool is not None:
           try:
-            obj = domain_tool[category]
-            root = root_dict[category] = root_dict[None] = obj
+            root = domain_tool[category]
+            root_dict[category] = root_dict[None] = (root, ('portal_domains', root.getRelativeUrl()))
             report_path = report_path[1:]
           except KeyError:
             pass
         if root is None:
-          try:
-            root = root_dict[None] = portal_object.unrestrictedTraverse(report_path)
-          except KeyError:
-            pass
+          root = portal_object.unrestrictedTraverse(report_path, None)
+          if root is not None:
+            root_dict[None] = (root, (None, root.getRelativeUrl()))
           report_path = ()
       else:
-        root = root_dict[None] = root_dict[category]
+        root_dict[None] = root_dict[category]
+        root = root_dict[None][0]
         report_path = report_path[1:]
       is_empty_level = (root is None or root.objectCount() == 0) and (len(report_path) != 0)
       if is_empty_level:
@@ -1188,8 +1185,11 @@
 
     for obj in obj_list:
       new_root_dict = root_dict.copy()
-      new_root_dict[None] = new_root_dict[base_category] = obj
-      domain_selection = DomainSelection(domain_dict = new_root_dict)
+      new_root_dict[None] = new_root_dict[base_category] = (obj, (new_root_dict[base_category][1][0], obj.getRelativeUrl()))
+      domain_dict = {}
+      for k, v in new_root_dict.iteritems():
+        domain_dict[k] = v[1]
+      domain_selection = DomainSelection(domain_dict = domain_dict)
 
       if base_category == 'parent':
         exception_uid_list = []

Modified: erp5/trunk/products/ERP5Form/Selection.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/Selection.py?rev=11149&r1=11148&r2=11149&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/Selection.py (original)
+++ erp5/trunk/products/ERP5Form/Selection.py Tue Nov  7 12:06:53 2006
@@ -382,7 +382,7 @@
     #LOG('DomainSelection', 0, '__init__ is called with %r' % (domain_dict,))
     if domain_dict is not None:
       self.domain_dict = domain_dict
-      for k,v in domain_dict.items():
+      for k, v in domain_dict.iteritems():
         if k is not None:
           setattr(self, k, v)
 
@@ -392,17 +392,40 @@
   security.declarePublic('getCategoryList')
   def getCategoryList(self):
     return
+
+  def _getDomainObject(self, portal, domain):
+    """Return a domain or category object.
+    """
+    if isinstance(domain, tuple):
+      # This is the new form. The first item describes the name of a tool or
+      # None if a domain is under a module. The second item is the relative
+      # URL of a domain.
+      tool = domain[0]
+      if tool is None:
+        obj = portal.restrictedTraverse(domain[1])
+      elif tool == 'portal_domains':
+        # Special case, as Domain Tool may generate a domain dynamically.
+        obj = portal.portal_domains.getDomainByPath(domain[1])
+      else:
+        obj = portal[tool].restrictedTraverse(domain[1])
+    elif isinstance(domain, str):
+      # XXX backward compatibility: a domain was represented by a string previously.
+      obj = portal.portal_domains.getDomainByPath(domain)
+    else:
+      # XXX backward compatibility: a category was represented by an object itself.
+      obj = aq_base(domain).__of__(portal)
+
+    return obj
 
   security.declarePublic('asSqlExpression')
   def asSqlExpression(self, table_map=None, domain_id=None, 
                       exclude_domain_id=None, strict_membership=0,
                       join_table="catalog", join_column="uid", base_category=None):
     select_expression = []
-    for k, d in self.domain_dict.items():
-      if isinstance(d, str):
-        # get the domain object
-        site = self.getPortalObject()
-        d = site['portal_domains'].getDomainByPath(d)
+    portal = self.getPortalObject()
+    for k, d in self.domain_dict.iteritems():
+      d = self._getDomainObject(portal, d)
+
       if k == 'parent':
         # Special treatment for parent
         select_expression.append(d.getParentSqlExpression(table='catalog', 
@@ -431,11 +454,10 @@
   def asSqlJoinExpression(self, domain_id=None, exclude_domain_id=None):
     join_expression = []
     #LOG('DomainSelection', 0, 'domain_id = %r, exclude_domain_id = %r, self.domain_dict = %r' % (domain_id, exclude_domain_id, self.domain_dict))
-    for k, d in self.domain_dict.items():
-      if isinstance(d, str):
-        # we must the domain
-        site = self.getPortalObject()
-        d = site['portal_domains'].getDomainByPath(d)
+    portal = self.getPortalObject()
+    for k, d in self.domain_dict.iteritems():
+      d = self._getDomainObject(portal, d)
+
       if k == 'parent':
         pass
       elif k is not None:

Modified: erp5/trunk/products/ERP5Form/SelectionTool.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/SelectionTool.py?rev=11149&r1=11148&r2=11149&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/SelectionTool.py (original)
+++ erp5/trunk/products/ERP5Form/SelectionTool.py Tue Nov  7 12:06:53 2006
@@ -135,7 +135,7 @@
       """
         Returns the selection instance for a given selection_name
       """
-      if not REQUEST:
+      if REQUEST is None:
         REQUEST = get_request()
 
       if not hasattr(self, 'selection_data'):
@@ -144,11 +144,11 @@
       if user_id is not None:
         if not self.selection_data.has_key(user_id):
           self.selection_data[user_id] = PersistentMapping()
-        if type(selection_name) is type(()) or type(selection_name) is type([]) :
+        if isinstance(selection_name, (tuple, list)):
           selection_name = selection_name[0]
-        return self.selection_data[user_id].get(selection_name, None)
-      else:
-        return None
+        selection = self.selection_data[user_id].get(selection_name, None)
+        if selection is not None:
+          return selection.__of__(self)
 
     security.declareProtected(ERP5Permissions.View, 'setSelectionFor')
     def setSelectionFor(self, selection_name, selection_object, REQUEST=None):




More information about the Erp5-report mailing list