[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/ZPTPage - ZPTPage.py:1.1.2.4 zptpage.zcml:1.1.2.5

Jim Fulton jim@zope.com
Thu, 23 May 2002 14:01:43 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/ZPTPage
In directory cvs.zope.org:/tmp/cvs-serv26429/lib/python/Zope/App/OFS/Content/ZPTPage

Modified Files:
      Tag: Zope-3x-branch
	ZPTPage.py zptpage.zcml 
Log Message:
This all started with wanting to be able to use url;view in a ZPT path. :)

That lead me to:

- Massive traversal refactoring.

  Namespace handling is now centralized in Zope.App.Traversing. 

- ZPT refactoring, including some renaming that touches pretty much everything. :)

  - The application specific ZPT support was moved into
    Zope.App.PageTemplate. 

  - To get page template files (for use in views):

    from Zope.App.PageTemplate import ViewPageTemplateFile

  - Fixed up security so that ZPT expressions only have access to 
    safe builtins and so that modules namespace does imports safely.

  - Got ZPTPage working!

- renaming url to absolute_url and got absolute_url to work in paths.

- Cleaned up the (as yet unused) RestrictedInterpreter module in
  Zope.Security. In particular, changed to use a separate
  RestrictedBuiltins module.



=== Zope3/lib/python/Zope/App/OFS/Content/ZPTPage/ZPTPage.py 1.1.2.3 => 1.1.2.4 ===
 
 from Interface import Interface
+from Interface.Attribute import Attribute
 from Persistence import Persistent
-from Zope.PageTemplate.ZPT import ZPT
 from Zope.App.OFS.Memento.IAttributeMementoStorable \
      import IAttributeMementoStorable
-
+from Zope.PageTemplate.PageTemplate import PageTemplate
+from Zope.App.PageTemplate.Engine import AppPT
+from Zope.ContextWrapper import ContextMethod
+from Zope.Proxy.ContextWrapper import getWrapperContainer
+from Zope.Security.Proxy import ProxyFactory
+from Zope.App.OFS.Content.IHTMLContent import IHTMLContent
 
 class IZPTPage(Interface):
     """ZPT Pages are a persistent implementation of Page Templates.
@@ -31,7 +36,7 @@
              want to start inforcing a common coding standard.
     """
 
-    def setSource(text):
+    def setSource(text, content_type='text/html'):
         """Save the source of the page template.
         """
 
@@ -40,20 +45,24 @@
         """Get the source of the page template.
         """
 
+class IRenderZPTPage(Interface):
+
+    content_type = Attribute('Content type of generated output')
+
+    def render(request, *args, **kw):
+        """Render the page template.
 
+        The first argument is bound to the top-level 'request'
+        variable. The positional arguments are bound to the 'args'
+        variable and the keyword arguments are bound to the 'options'
+        variable.
+        """
 
-class ZPTPage(Persistent, ZPT):
-    """ """
 
-    __implements__ = (
-        IZPTPage,
-        IAttributeMementoStorable,
-        )
-
-    def __init__(self, text=''):
-        """ """
-        self.setSource(text)
+class ZPTPage(AppPT, PageTemplate, Persistent):
 
+    # XXX Putting IHTMLContent at the end gives an error!
+    __implements__ = IHTMLContent, IZPTPage, IRenderZPTPage
 
     ############################################################
     # Implementation methods for interface
@@ -64,14 +73,34 @@
         return self.read()
 
 
-    def setSource(self, text):
+    def setSource(self, text, content_type='text/html'):
         '''See interface IZPTPage'''
-        self.pt_edit(text, 'text/html')
+        self.pt_edit(text, content_type)
 
     #
     ############################################################
 
-    _need__name__=1
-    _v_last_read = 0
 
-    _default_bindings = {'name_subpath': 'traverse_subpath'}
+    def pt_getContext(self, instance, request, **_kw):
+        # instance is a View component
+        namespace = super(ZPTPage, self).pt_getContext(**_kw)
+        namespace['request'] = request
+        namespace['context'] = instance
+        return namespace
+
+    def render(self, request, *args, **keywords):
+
+        instance = getWrapperContainer(self)
+
+        request = ProxyFactory(request)
+        instance = ProxyFactory(instance)
+        if args: args = ProxyFactory(args)
+        kw = ProxyFactory(keywords)
+        
+        namespace = self.pt_getContext(instance, request,
+                                       args=args, options=kw)
+
+        return self.pt_render(namespace)
+
+    render = ContextMethod(render)
+    


=== Zope3/lib/python/Zope/App/OFS/Content/ZPTPage/zptpage.zcml 1.1.2.4 => 1.1.2.5 ===
-   xmlns='http://namespaces.zope.org/zope'
-   xmlns:security='http://namespaces.zope.org/security'
-   xmlns:browser='http://namespaces.zope.org/browser'
-   xmlns:zmi='http://namespaces.zope.org/zmi'
->
-
-  <zmi:factoryFromClass name="ZPTPage"
-                    	class=".ZPTPage."
-                    	permission_id="Zope.ManageContent"
-                    	title="ZPT Page"
-                    	description="A simple, content-based Page Template" />
-
-
-  <security:protectClass name=".ZPTPage."
+<zopeConfigure
+   xmlns='http://namespaces.zope.org/zope'
+   xmlns:security='http://namespaces.zope.org/security'
+   xmlns:browser='http://namespaces.zope.org/browser'
+   xmlns:zmi='http://namespaces.zope.org/zmi'
+>
+
+  <zmi:factoryFromClass name="ZPTPage"
+                    	class=".ZPTPage."
+                    	permission_id="Zope.ManageContent"
+                    	title="ZPT Page"
+                    	description="A simple, content-based Page Template" />
+
+
+  <security:protectClass name=".ZPTPage."
     permission_id="Zope.View">
     <security:protect
-              names="_cook, pt_render"
+              names="content_type, __call__"
               permission_id="Zope.View" />
     <security:protect
-              interface="Zope.App.OFS.Content.ZPTPage.ZPTPage.IZPTPage"
-              permission_id="Zope.ManageContent" />
-  </security:protectClass>
-
-  <!-- tabs for ZPT Page -->
-
-  <zmi:tabs for=".ZPTPage.IZPTPage.">
-    <zmi:tab label="Edit" action="edit;view"/>
-    <zmi:tab label="View" action=""/>
-    <zmi:tab label="Role Permissions" action="RolePermissionsManagement;view"/>
-  </zmi:tabs>
-
-  <include package=".Views" file="views.zcml" />
-
-
-
-</zopeConfigure>
+              interface=".ZPTPage.IZPTPage"
+              permission_id="Zope.ManageContent" />
+    <security:protect
+              interface=".ZPTPage.IRenderZPTPage"
+              permission_id="Zope.View" />
+  </security:protectClass>
+
+  <!-- tabs for ZPT Page -->
+
+  <zmi:tabs for=".ZPTPage.IZPTPage.">
+    <zmi:tab label="Edit" action="edit;view"/>
+    <zmi:tab label="View" action=""/>
+    <!-- XXX This isn't working
+    <zmi:tab label="Role Permissions" action="RolePermissionsManagement;view"/>
+    -->
+  </zmi:tabs>
+
+  <include package=".Views" file="views.zcml" />
+
+
+
+</zopeConfigure>