[Checkins] SVN: grok/trunk/src/grok/ Add a "self.response" property to grok views for easy access to the response

Philipp von Weitershausen philikon at philikon.de
Tue Mar 6 11:18:42 EST 2007


Log message for revision 73007:
  Add a "self.response" property to grok views for easy access to the response
  ("self.request" is there, why shouldn't the response be there for symmetrical
  reasons?)
  
  Documented a bunch of properties and methods of grok.View in the IGrokView
  interface.
  

Changed:
  U   grok/trunk/src/grok/components.py
  U   grok/trunk/src/grok/interfaces.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2007-03-06 16:02:59 UTC (rev 73006)
+++ grok/trunk/src/grok/components.py	2007-03-06 16:18:41 UTC (rev 73007)
@@ -143,9 +143,16 @@
 
     def __init__(self, context, request):
         super(View, self).__init__(context, request)
-        self.directory_resource = component.queryAdapter(self.request,
-                interface.Interface, name=self.module_info.package_dotted_name)
+        self.static = component.queryAdapter(
+            self.request,
+            interface.Interface,
+            name=self.module_info.package_dotted_name
+            )
 
+    @property
+    def response(self):
+        return self.request.response
+
     def __call__(self):
         mapply(self.update, (), self.request)
         if self.request.response.getStatus() in (302, 303):
@@ -162,7 +169,7 @@
         namespace['view'] = self
         namespace['context'] = self.context
         # XXX need to check whether we really want to put None here if missing
-        namespace['static'] = self.directory_resource
+        namespace['static'] = self.static
         return template.pt_render(namespace)
 
     def __getitem__(self, key):

Modified: grok/trunk/src/grok/interfaces.py
===================================================================
--- grok/trunk/src/grok/interfaces.py	2007-03-06 16:02:59 UTC (rev 73006)
+++ grok/trunk/src/grok/interfaces.py	2007-03-06 16:18:41 UTC (rev 73007)
@@ -14,6 +14,7 @@
 """Grok interfaces
 """
 from zope import interface
+from zope.publisher.interfaces.browser import IBrowserPage
 
 class IGrokBaseClasses(interface.Interface):
     ClassGrokker = interface.Attribute("Base class to define a class "
@@ -209,9 +210,21 @@
         """
 
 
-class IGrokView(interface.Interface):
+class IGrokView(IBrowserPage):
     """Grok views all provide this interface.
     """
+
+    context = interface.Attribute('context', "Object that the view presents.")
+
+    request = interface.Attribute('request', "Request that the view was looked"
+                                  "up with.")
+
+    response = interface.Attribute('response', "Response object that is "
+                                   "associated with the current request.")
+
+    static = interface.Attribute('static', "Directory resource containing "
+                                 "the static files of the view's package.")
+
     def redirect(url):
        """Redirect to given URL"""
 
@@ -229,11 +242,29 @@
         URL to obj/name.
         """
 
+    def update(**kw):
+        """This method is meant to be implemented by grok.View
+        subclasses.  It will be called *before* the view's associated
+        template is rendered and can be used to pre-compute values
+        for the template.
 
+        update() can take arbitrary keyword parameters which will be
+        filled in from the request (in that case they *must* be
+        present in the request)."""
+
+    def render(**kw):
+        """A view can either be rendered by an associated template, or
+        it can implement this method to render itself from Python.
+        This is useful if the view's output isn't XML/HTML but
+        something computed in Python (plain text, PDF, etc.)
+
+        render() can take arbitrary keyword parameters which will be
+        filled in from the request (in that case they *must* be
+        present in the request)."""
+
 class IApplication(interface.Interface):
     """Marker-interface for grok application factories.
 
     Used to register applications as utilities to look them up and
     provide a list of grokked applications.
-
     """



More information about the Checkins mailing list