From cd1a5a22a1643b8b3a1940e0c5be74e2ad0f9e8e Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Fri, 8 Oct 2010 11:19:10 +0000
Subject: [PATCH] Convert 'default' attribute  to a TALES expression for
 web-based Property Sheets

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38999 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/AcquiredProperty.py | 30 ++++++-----------------
 product/ERP5/Document/StandardProperty.py | 17 +++++++++++++
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/product/ERP5/Document/AcquiredProperty.py b/product/ERP5/Document/AcquiredProperty.py
index 101ed68012..15e943f123 100644
--- a/product/ERP5/Document/AcquiredProperty.py
+++ b/product/ERP5/Document/AcquiredProperty.py
@@ -59,8 +59,10 @@ class AcquiredProperty(StandardProperty):
            **StandardProperty._name_mapping_filesystem_to_web_dict)
 
   # Web-based name of attributes whose value is a TALES Expression
-  _expression_attribute_tuple = ('acquisition_portal_type',
-                                 'content_portal_type')
+  # string
+  _expression_attribute_tuple = \
+      StandardProperty._expression_attribute_tuple + \
+      ('acquisition_portal_type', 'content_portal_type')
 
   @staticmethod
   def _convertValueToTalesExpression(value):
@@ -68,7 +70,10 @@ class AcquiredProperty(StandardProperty):
     Convert a string value to a TALES expression for attributes listed
     in '_expression_attribute_tuple'
     """
-    return value is None and value or Expression(value)
+    if value is None:
+      return None
+
+    return Expression(value)
 
   security.declareProtected(Permissions.AccessContentsInformation,
                             'exportToFilesystemDefinition')
@@ -92,22 +97,3 @@ class AcquiredProperty(StandardProperty):
        'translation_acquired_property_id': self.getContentTranslationAcquiredPropertyIdList() or None})
 
     return filesystem_property_dict
-
-  def _convertFromFilesytemPropertyDict(self, filesystem_property_dict):
-    """
-    Convert a property dict coming from a Property Sheet on the
-    filesystem to a web-based property dict
-    """
-    web_property_dict = StandardProperty._convertFromFilesytemPropertyDict(
-      self, filesystem_property_dict)
-
-    # Update the properties whose values are TALES Expression
-    for attribute in self._expression_attribute_tuple:
-      if attribute in web_property_dict:
-        value = isinstance(web_property_dict[attribute], Expression) and \
-            web_property_dict[attribute].text or \
-            'python: ' + repr(web_property_dict[attribute])
-
-        web_property_dict[attribute] = value
-
-    return web_property_dict
diff --git a/product/ERP5/Document/StandardProperty.py b/product/ERP5/Document/StandardProperty.py
index a0c88bcd7f..0d48d3c998 100644
--- a/product/ERP5/Document/StandardProperty.py
+++ b/product/ERP5/Document/StandardProperty.py
@@ -27,6 +27,7 @@
 ##############################################################################
 
 from AccessControl import ClassSecurityInfo
+from Products.CMFCore.Expression import Expression
 
 from Products.ERP5Type import Permissions, PropertySheet
 from Products.ERP5Type.XMLObject import XMLObject
@@ -53,6 +54,10 @@ class StandardProperty(XMLObject):
                                           'type': 'elementary_type',
                                           'default': 'property_default'}
 
+  # Web-based name of attributes whose value is a TALES Expression
+  # string
+  _expression_attribute_tuple = ('property_default',)
+
   security.declareProtected(Permissions.AccessContentsInformation,
                             'exportToFilesystemDefinition')
   def exportToFilesystemDefinition(self):
@@ -81,12 +86,24 @@ class StandardProperty(XMLObject):
     web_property_dict = {}
 
     for fs_property_name, value in filesystem_property_dict.iteritems():
+      # Property Sheets on the filesystem defined attributes whose
+      # value is None, or an empty tuple or string, or either 0, thus
+      # skip them
+      if not value:
+        continue
+
       # Convert filesystem property name to web-based if necessary
       web_property_name = \
           fs_property_name in self._name_mapping_filesystem_to_web_dict and \
           self._name_mapping_filesystem_to_web_dict[fs_property_name] or \
           fs_property_name
 
+      # Convert existing TALES expression class or primitive type to a
+      # TALES expression string
+      if web_property_name in self._expression_attribute_tuple:
+        value = isinstance(value, Expression) and \
+            value.text or 'python: ' + repr(value)
+
       web_property_dict[web_property_name] = value
 
     return web_property_dict
-- 
2.30.9