[CMF-checkins] CVS: Products/CMFCore - PortalContent.py:1.29 utils.py:1.17

Tres Seaver tseaver@zope.com
Thu, 18 Oct 2001 11:57:17 -0400


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

Modified Files:
	PortalContent.py utils.py 
Log Message:
 - Commit Seb's refactoring of 'getDefaultView'.

=== Products/CMFCore/PortalContent.py 1.28 => 1.29 ===
 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:


=== Products/CMFCore/utils.py 1.16 => 1.17 ===
     return 0
 
+def _verifyActionPermissions(obj, action):
+    pp = action.get('permissions', ())
+    if not pp:
+        return 1
+    for p in pp:
+        if _checkPermission(p, obj):
+            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.