[CMF-checkins] CVS: CMF/CMFCore - PortalContent.py:1.28.4.1 utils.py:1.16.4.1

seb seb@jamkit.com
Thu, 18 Oct 2001 05:52:58 -0400


Update of /cvs-repository/CMF/CMFCore
In directory cvs.zope.org:/tmp/cvs-serv9412/Products/CMFCore

Modified Files:
      Tag: seb-defaultActionRefactor-branch
	PortalContent.py utils.py 
Log Message:
Refactored getDefaultView into CMFCore.utils._getView; can now specify a view to get by name.  
Added new TypesTool unit test.


=== CMF/CMFCore/PortalContent.py 1.28 => 1.28.4.1 ===
 from interfaces.Contentish import Contentish
 from DynamicType import DynamicType
-from utils import getToolByName, _checkPermission
+from utils import getToolByName, _checkPermission, _getViewFor
 try: 
     from webdav.WriteLockInterface import WriteLockInterface
     NoWL = 0
@@ -206,39 +206,11 @@
     # Contentish interface methods
     # ----------------------------
 
-    def _verifyActionPermissions(self, action):
-        pp = action.get('permissions', ())
-        if not pp:
-            return 1
-        for p in pp:
-            if _checkPermission(p, self):
-                return 1
-        return 0
-
-    def _getDefaultView(self):
-        ti = self.getTypeInfo()
-        if ti is not None:
-            actions = ti.getActions()
-            for action in actions:
-                if action.get('id', None) == 'view':
-                    if self._verifyActionPermissions(action):
-                        return self.restrictedTraverse(action['action'])
-            # "view" action is not present or not allowed.
-            # Find something that's allowed.
-            for action in actions:
-                if self._verifyActionPermissions(action):
-                    return self.restrictedTraverse(action['action'])
-            raise 'Unauthorized', ('No accessible views available for %s' %
-                                 string.join(self.getPhysicalPath(), '/'))
-        else:
-            raise 'Not Found', ('Cannot find default view for "%s"' %
-                                string.join(self.getPhysicalPath(), '/'))
-
     def __call__(self):
         '''
         Invokes the default view.
         '''
-        view = self._getDefaultView()
+        view = _getViewFor(self)
         if getattr(aq_base(view), 'isDocTemp', 0):
             return apply(view, (self, self.REQUEST))
         else:


=== CMF/CMFCore/utils.py 1.16 => 1.16.4.1 ===
     return 0
 
+def _verifyActionPermissions(self, action):
+    pp = action.get('permissions', ())
+    if not pp:
+        return 1
+    for p in pp:
+        if _checkPermission(p, self):
+            return 1
+    return 0
+
+def _getViewFor(obj, view='view'):
+    ti = obj.getTypeInfo()
+    if ti is not None:
+        actions = ti.getActions()
+        for action in actions:
+            if action.get('id', None) == view:
+                if _verifyActionPermissions(obj, action):
+                    return obj.restrictedTraverse(action['action'])
+        # "view" action is not present or not allowed.
+        # Find something that's allowed.
+        for action in actions:
+            if _verifyActionPermissions(obj, action):
+                return obj.restrictedTraverse(action['action'])
+        raise 'Unauthorized', ('No accessible views available for %s' %
+                               string.join(obj.getPhysicalPath(), '/'))
+    else:
+        raise 'Not Found', ('Cannot find default view for "%s"' %
+                            string.join(obj.getPhysicalPath(), '/'))
+
 
 # If Zope ever provides a call to getRolesInContext() through
 # the SecurityManager API, the method below needs to be updated.