[Zope3-checkins] CVS: Zope3/src/zope/app/services - view.py:1.5

Albertas Agejevas alga@codeworks.lt
Thu, 6 Feb 2003 10:35:34 -0500


Update of /cvs-repository/Zope3/src/zope/app/services
In directory cvs.zope.org:/tmp/cvs-serv32407/src/zope/app/services

Modified Files:
	view.py 
Log Message:
Added ant 'attribute' attribute to the PageConfiguration.
That is, now a persistent page configuration in a local view service can
either specify an attribute of a view to be called, or provide a persistent
page template.

The 'attribute' attribute is not yet in the add view though.


=== Zope3/src/zope/app/services/view.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/services/view.py:1.4	Tue Jan 21 16:45:07 2003
+++ Zope3/src/zope/app/services/view.py	Thu Feb  6 10:35:03 2003
@@ -43,6 +43,7 @@
 
 from zope.app.interfaces.services.interfaces import IViewConfiguration, IPageConfiguration
 from zope.app.services.adapter import PersistentAdapterRegistry
+from zope.configuration.exceptions import ConfigurationError
 
 class ViewService(Persistent):
 
@@ -244,24 +245,46 @@
     presentationType = IBrowserPresentation
 
     def __init__(self,
-                 forInterface, viewName, permission,                 
-                 class_=None, template=None,
+                 forInterface, viewName, permission,
+                 class_=None, template=None, attribute=None,
                  layer='default'):
 
-        
         super(PageConfiguration, self).__init__(
             forInterface, viewName, self.presentationType,
             class_, permission, layer)
 
         self.template = template
+        self.attribute = attribute
+
+    def _validate(self):
+        if self.template is not None and self.attribute is not None:
+            raise ConfigurationError(
+                "PageConfiguration for %s view name %s: "
+                "Cannot have both 'template' and 'attribute' at the same time." %
+                (self.forInterface, self.viewName))
+
+        if self.template is None and self.attribute is None:
+            raise ConfigurationError(
+                "PageConfiguration for %s view name %s: "
+                "Should have a 'template' or 'attribute' attribute." %
+                (self.forInterface, self.viewName))
+
+        if self.class_ is None and self.attribute is not None:
+            raise ConfigurationError(
+                "PageConfiguration for %s view name %s: "
+                "Cannot have an 'attribute' without a 'class_'." %
+                (self.forInterface, self.viewName))
 
     def getView(self, object, request):
+
+
+        self._validate()
+
         sm = getServiceManager(self)
 
         if self.class_:
             class_ = sm.resolve(self.class_)
             class_ = type(class_.__name__, (class_, DefaultClass), {})
-            
         else:
             class_  = DefaultClass
 
@@ -270,11 +293,15 @@
         # This is needed because we need to do an unrestricted traverse
         root = removeAllProxies(getPhysicalRoot(sm))
 
-        template = traverse(root, self.template)
+        if self.attribute is not None:
+            template = getattr(view, self.attribute)
+        else:
+            template = traverse(root, self.template)
+            template = BoundTemplate(template, view)
 
         checker = NamesChecker(__call__ = self.permission)
 
-        return ProxyFactory(BoundTemplate(template, view), checker)
+        return ProxyFactory(template, checker)
 
     getView = ContextMethod(getView)