[Checkins] SVN: Products.CMFCore/trunk/Products/CMFCore/ - added new 'getIconURL' and 'getIconExprObject' methods

Yvo Schubbe y.2009 at wcm-solutions.de
Tue Nov 17 12:54:04 EST 2009


Log message for revision 105763:
  - added new 'getIconURL' and 'getIconExprObject' methods
  - marked 'getIcon' methods as deprecated

Changed:
  U   Products.CMFCore/trunk/Products/CMFCore/CHANGES.txt
  U   Products.CMFCore/trunk/Products/CMFCore/DynamicType.py
  U   Products.CMFCore/trunk/Products/CMFCore/TypesTool.py
  U   Products.CMFCore/trunk/Products/CMFCore/interfaces/_content.py
  U   Products.CMFCore/trunk/Products/CMFCore/interfaces/_tools.py
  U   Products.CMFCore/trunk/Products/CMFCore/tests/base/dummy.py
  U   Products.CMFCore/trunk/Products/CMFCore/tests/test_TypesTool.py

-=-
Modified: Products.CMFCore/trunk/Products/CMFCore/CHANGES.txt
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/CHANGES.txt	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/CHANGES.txt	2009-11-17 17:54:04 UTC (rev 105763)
@@ -4,7 +4,13 @@
 2.2.0-beta (unreleased)
 -----------------------
 
+- DynamicType: Added 'getIconURL' method.
+  This replaces the now deprecated 'getIcon' method.
 
+- TypeInformation: Added 'getIconExprObject' method.
+  This replaces the now deprecated 'getIcon' method.
+
+
 2.2.0-alpha (2009-11-13)
 ------------------------
 

Modified: Products.CMFCore/trunk/Products/CMFCore/DynamicType.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/DynamicType.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/DynamicType.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -15,14 +15,13 @@
 $Id$
 """
 
-from urllib import quote
-
 from AccessControl.SecurityInfo import ClassSecurityInfo
 from App.class_init import InitializeClass
 from zope.app.publisher.browser import queryDefaultViewName
 from zope.component import queryMultiAdapter
 from zope.interface import implements
 
+from Products.CMFCore.Expression import getExprContext
 from Products.CMFCore.interfaces import IDynamicType
 from Products.CMFCore.utils import getToolByName
 
@@ -48,6 +47,9 @@
         """
         self.portal_type = pt
 
+    #
+    #   'IDynamicType' interface methods
+    #
     security.declarePublic('getPortalTypeName')
     def getPortalTypeName(self):
         """ Get the portal type name that can be passed to portal_types.
@@ -83,27 +85,43 @@
                         action_chain, '/'.join(self.getPhysicalPath()))
             raise ValueError(msg)
 
-    # Support for dynamic icons
+    security.declarePublic('getIconURL')
+    def getIconURL(self):
+        """ Get the absolute URL of the icon for the object.
+        """
+        ti = self.getTypeInfo()
+        if ti is None:
+            utool = getToolByName(self, 'portal_url')
+            return '%s/misc_/OFSP/dtmldoc.gif' % utool()
+        icon_expr_object = ti.getIconExprObject()
+        if icon_expr_object is None:
+            return ''
+        ec = getExprContext(self)
+        return icon_expr_object(ec)
 
-    security.declarePublic('getIcon')
-    def getIcon(self, relative_to_portal=0):
+    #
+    #   'IItem' interface method
+    #
+    security.declarePublic('icon')
+    def icon(self, relative_to_portal=0):
         """
         Using this method allows the content class
         creator to grab icons on the fly instead of using a fixed
         attribute on the class.
         """
-        ti = self.getTypeInfo()
-        if ti is not None:
-            if relative_to_portal:
-                icon = quote(ti.getIcon())
-                if icon:
-                    return icon
-            else:
-                return ti.getIcon(absolute=True)
-        return 'misc_/OFSP/dtmldoc.gif'
+        utool = getToolByName(self, 'portal_url')
+        portal_url = utool()
+        icon = self.getIconURL()
+        if icon.startswith(portal_url):
+            icon = icon[len(portal_url)+1:]
+            if not relative_to_portal:
+                # Relative to REQUEST['BASEPATH1']
+                icon = '%s/%s' % (utool(relative=1), icon)
+        return icon
 
-    security.declarePublic('icon')
-    icon = getIcon  # For the ZMI
+    # deprecated alias
+    security.declarePublic('getIcon')
+    getIcon = icon
 
     def __before_publishing_traverse__(self, arg1, arg2=None):
         """ Pre-traversal hook.

Modified: Products.CMFCore/trunk/Products/CMFCore/TypesTool.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/TypesTool.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/TypesTool.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -41,7 +41,6 @@
 from Products.CMFCore.exceptions import AccessControl_Unauthorized
 from Products.CMFCore.exceptions import BadRequest
 from Products.CMFCore.exceptions import zExceptions_Unauthorized
-from Products.CMFCore.Expression import getExprContext
 from Products.CMFCore.Expression import Expression
 from Products.CMFCore.interfaces import IAction
 from Products.CMFCore.interfaces import ITypeInformation
@@ -243,21 +242,22 @@
         return self.content_meta_type
 
     security.declareProtected(View, 'getIcon')
-    def getIcon(self, absolute=False):
+    def getIcon(self):
+        """ Returns the icon for this content object.
         """
-            Returns the icon for this content object.
+        warn('getIcon() is deprecated and provides only limited backwards '
+             'compatibility. It will be removed in CMF 2.4. Please use '
+             'getIconExprObject() instead.',
+             DeprecationWarning, stacklevel=2)
+        if self.icon_expr.startswith('string:${portal_url}/'):
+            return self.icon_expr[len('string:${portal_url}/'):]
+        return self.icon_expr
+
+    security.declarePrivate('getIconExprObject')
+    def getIconExprObject(self):
+        """ Get the expression object representing the icon for this type.
         """
-        if self.content_icon:
-            return self.content_icon
-        icon_expr = getattr(self, 'icon_expr_object', None)
-        if icon_expr:
-            ec = getExprContext(self)
-            icon = icon_expr(ec)
-            if absolute:
-                return icon
-            if isinstance(icon, basestring):
-                return icon.split('/')[-1]
-        return ''
+        return getattr(self, 'icon_expr_object', None)
 
     security.declarePublic('allowType')
     def allowType( self, contentType ):
@@ -397,10 +397,6 @@
         if self.icon_expr:
             lazy_map['icon'] = self.icon_expr_object
             lazy_keys.append('icon')
-        elif self.content_icon:
-            lazy_map['icon'] = Expression('string:${portal_url}/%s'
-                                          % self.content_icon)
-            lazy_keys.append('icon')
         else:
             lazy_map['icon'] = ''
         lazy_map['link_target'] = self.link_target

Modified: Products.CMFCore/trunk/Products/CMFCore/interfaces/_content.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/interfaces/_content.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/interfaces/_content.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -498,8 +498,8 @@
         o Permission:  Public
         """
 
-    def getIcon(relative_to_portal=False):
-        """ Get the path to an object's icon.
+    def getIconURL():
+        """ Get the absolute URL of the icon for the object.
 
         o This method is used in the 'folder_contents' view to generate an
           appropriate icon for the items found in the folder.
@@ -508,10 +508,6 @@
           return a "default" icon path (e.g., '/misc_/dtmldoc.gif', which is
           the icon used for DTML Documents).
 
-        o If 'relative_to_portal' is True, return only the portion of
-          the icon's URL which finds it "within" the portal;  otherwise,
-          return it as an absolute URL.
-
         o Permission:  Public
         """
 

Modified: Products.CMFCore/trunk/Products/CMFCore/interfaces/_tools.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/interfaces/_tools.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/interfaces/_tools.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -1398,8 +1398,8 @@
         o Individual objects may still disable discussion.
         """
 
-    def getIcon():
-        """ Return the portal-relative URL for the icon for this type.
+    def getIconExprObject():
+        """ Get the expression object representing the icon for this type.
         """
 
     def getMethodAliases():

Modified: Products.CMFCore/trunk/Products/CMFCore/tests/base/dummy.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/tests/base/dummy.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/tests/base/dummy.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -40,7 +40,7 @@
 class DummyObject(Implicit):
     """
     A dummy callable object.
-    Comes with getIcon and restrictedTraverse
+    Comes with getIconURL and restrictedTraverse
     methods.
     """
     def __init__(self, id='dummy',**kw):
@@ -66,9 +66,12 @@
 
         return parent
 
-    def getIcon(self, relative=0):
-        return 'Site: %s' % relative
+    def icon(self):
+        return '%s ICON' % self._id
 
+    def getIconURL(self):
+        return '%s ICON' % self._id
+
     def getId(self):
         return self._id
 
@@ -373,9 +376,6 @@
     def __init__(self, anon=1):
         self.anon = anon
 
-    def getIcon( self, relative=0 ):
-        return 'Tool: %s' % relative
-
     # IMembershipTool
     def getAuthenticatedMember(self):
         return DummyUser()
@@ -404,11 +404,9 @@
         return (self._type_id,)
 
     # IURLTool
-    root = 'DummyTool'
+    def __call__(self, relative=0):
+        return self.getPortalObject().absolute_url()
 
-    def __call__(self):
-        return self.root
-
     def getPortalObject(self):
         return aq_parent(aq_inner(self))
 

Modified: Products.CMFCore/trunk/Products/CMFCore/tests/test_TypesTool.py
===================================================================
--- Products.CMFCore/trunk/Products/CMFCore/tests/test_TypesTool.py	2009-11-17 17:05:18 UTC (rev 105762)
+++ Products.CMFCore/trunk/Products/CMFCore/tests/test_TypesTool.py	2009-11-17 17:54:04 UTC (rev 105763)
@@ -277,22 +277,19 @@
         from Products.CMFCore.interfaces import ITypeInformation
         verifyObject(ITypeInformation, self._makeOne())
 
-    def test_construction( self ):
-        ti = self._makeOne( 'Foo'
-                               , description='Description'
-                               , meta_type='Foo'
-                               , icon='foo.gif'
-                               )
+    def test_construction(self):
+        ti = self._makeOne('Foo', description='Description', meta_type='Foo',
+                           icon='foo.gif')
         self.assertEqual( ti.getId(), 'Foo' )
         self.assertEqual( ti.Title(), 'Foo' )
         self.assertEqual( ti.Description(), 'Description' )
         self.assertEqual( ti.Metatype(), 'Foo' )
+        self.assertEqual( ti.getIconExprObject().text,
+                          'string:${portal_url}/foo.gif' )
         self.assertEqual( ti.getIcon(), 'foo.gif' )
         self.assertEqual( ti.immediate_view, '' )
 
-        ti = self._makeOne( 'Foo'
-                               , immediate_view='foo_view'
-                               )
+        ti = self._makeOne('Foo', immediate_view='foo_view')
         self.assertEqual( ti.immediate_view, 'foo_view' )
 
     def _makeAndSetInstance(self, id, **kw):



More information about the checkins mailing list