[Checkins] SVN: grok/branches/grokcore.xxx/ factor out View methods in ViewMixin; move grok.url method to grokcore.view.util

Godefroid Chapelle gotcha at bubblenet.be
Thu Jul 17 08:54:37 EDT 2008


Log message for revision 88436:
  factor out View methods in ViewMixin; move grok.url method to grokcore.view.util

Changed:
  U   grok/branches/grokcore.xxx/devel/grokcore.view/src/grokcore/view/__init__.py
  U   grok/branches/grokcore.xxx/src/grok/__init__.py
  U   grok/branches/grokcore.xxx/src/grok/components.py
  U   grok/branches/grokcore.xxx/src/grok/interfaces.py
  U   grok/branches/grokcore.xxx/src/grok/meta.py
  U   grok/branches/grokcore.xxx/src/grok/util.py

-=-
Modified: grok/branches/grokcore.xxx/devel/grokcore.view/src/grokcore/view/__init__.py
===================================================================
--- grok/branches/grokcore.xxx/devel/grokcore.view/src/grokcore/view/__init__.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/devel/grokcore.view/src/grokcore/view/__init__.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -1 +1,3 @@
 from directive import layer, view, viewletmanager
+from util import url
+from components import ViewMixin

Modified: grok/branches/grokcore.xxx/src/grok/__init__.py
===================================================================
--- grok/branches/grokcore.xxx/src/grok/__init__.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/src/grok/__init__.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -59,7 +59,7 @@
 from grok.testing import grok, grok_component
 
 from grok.formlib import action, AutoFields, Fields
-from grok.util import url
+from grokcore.view import url
 
 # Our __init__ provides the grok API directly so using 'import grok' is enough.
 from grok.interfaces import IGrokAPI

Modified: grok/branches/grokcore.xxx/src/grok/components.py
===================================================================
--- grok/branches/grokcore.xxx/src/grok/components.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/src/grok/components.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -31,8 +31,8 @@
 from zope.publisher.interfaces import NotFound
 from zope.publisher.interfaces.browser import IBrowserPublisher
 from zope.publisher.interfaces.http import IHTTPRequest
-from zope.publisher.publish import mapply
 from zope.pagetemplate import pagetemplate, pagetemplatefile
+from zope.publisher.publish import mapply
 from zope.formlib import form
 from zope.annotation.interfaces import IAttributeAnnotatable
 
@@ -54,7 +54,7 @@
 from zope.viewlet.viewlet import ViewletBase
 
 import grok
-import grokcore.component
+import grokcore.view
 import z3c.flashmessage.interfaces
 import martian.util
 
@@ -145,37 +145,16 @@
 #        self.context = context
 #        self.request = request
 
-class View(BrowserPage):
+class View(BrowserPage, grokcore.view.ViewMixin):
     interface.implements(interfaces.IGrokView)
 
     def __init__(self, context, request):
         super(View, self).__init__(context, request)
-        self.__name__ = self.__view_name__
-        self.static = component.queryAdapter(
-            self.request,
-            interface.Interface,
-            name=self.module_info.package_dotted_name
-            )
+        self._initialize()
 
-    @property
-    def response(self):
-        return self.request.response
-
     def __call__(self):
-        mapply(self.update, (), self.request)
-        if self.request.response.getStatus() in (302, 303):
-            # A redirect was triggered somewhere in update().  Don't
-            # continue rendering the template or doing anything else.
-            return
+        return self._update_and_render()
 
-        template = getattr(self, 'template', None)
-        if template is not None:
-            return self._render_template()
-        return mapply(self.render, (), self.request)
-
-    def _render_template(self):
-        return self.template.render(self)
-
     def default_namespace(self):
         namespace = {}
         namespace['context'] = self.context
@@ -184,9 +163,6 @@
         namespace['view'] = self
         return namespace
 
-    def namespace(self):
-        return {}
-
     def __getitem__(self, key):
         # This is BBB code for Zope page templates only:
         if not isinstance(self.template, PageTemplate):
@@ -201,34 +177,11 @@
                       DeprecationWarning, 1)
         return value
 
+    def flash(self, message, type='message'):
+        source = component.getUtility(
+            z3c.flashmessage.interfaces.IMessageSource, name='session')
+        source.send(message, type)
 
-    def url(self, obj=None, name=None, data=None):
-        """Return string for the URL based on the obj and name. The data
-        argument is used to form a CGI query string.
-        """
-        if isinstance(obj, basestring):
-            if name is not None:
-                raise TypeError(
-                    'url() takes either obj argument, obj, string arguments, '
-                    'or string argument')
-            name = obj
-            obj = None
-
-        if name is None and obj is None:
-            # create URL to view itself
-            obj = self
-        elif name is not None and obj is None:
-            # create URL to view on context
-            obj = self.context
-
-        if data is None:
-            data = {}
-        else:
-            if not isinstance(data, dict):
-                raise TypeError('url() data argument must be a dict.')
-
-        return util.url(self.request, obj, name, data=data)
-
     def application_url(self, name=None):
         obj = self.context
         while obj is not None:
@@ -237,18 +190,7 @@
             obj = obj.__parent__
         raise ValueError("No application found.")
 
-    def redirect(self, url):
-        return self.request.response.redirect(url)
 
-    def update(self):
-        pass
-
-    def flash(self, message, type='message'):
-        source = component.getUtility(
-            z3c.flashmessage.interfaces.IMessageSource, name='session')
-        source.send(message, type)
-
-
 class XMLRPC(object):
     pass
 

Modified: grok/branches/grokcore.xxx/src/grok/interfaces.py
===================================================================
--- grok/branches/grokcore.xxx/src/grok/interfaces.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/src/grok/interfaces.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -14,13 +14,13 @@
 """Grok interfaces
 """
 from zope import interface, schema
-from zope.publisher.interfaces.browser import IBrowserPage, IBrowserView
 from zope.formlib.interfaces import reConstraint
 from zope.interface.interfaces import IInterface
 from zope.viewlet.interfaces import IViewletManager as IViewletManagerBase
 from zope.app.container.interfaces import IContainer as IContainerBase
 
 from grokcore.component.interfaces import IContext
+import grokcore.view.interfaces
 
 class IGrokBaseClasses(interface.Interface):
     ClassGrokker = interface.Attribute("Base class to define a class "
@@ -286,82 +286,9 @@
 
     IRESTSkinType = interface.Attribute('The REST skin type')
 
-class IGrokView(IBrowserPage, IBrowserView):
+class IGrokView(grokcore.view.interfaces.IGrokView):
     """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"""
-
-    def url(obj=None, name=None, data=None):
-        """Construct URL.
-
-        If no arguments given, construct URL to view itself.
-
-        If only obj argument is given, construct URL to obj.
-
-        If only name is given as the first argument, construct URL
-        to context/name.
-
-        If both object and name arguments are supplied, construct
-        URL to obj/name.
-
-        Optionally pass a 'data' keyword argument which gets added to the URL
-        as a cgi query string.
-        """
-
-    def default_namespace():
-        """Returns a dictionary of namespaces that the template
-        implementation expects to always be available.
-
-        This method is *not* intended to be overridden by application
-        developers.
-        """
-
-    def namespace():
-        """Returns a dictionary that is injected in the template
-        namespace in addition to the default namespace.
-
-        This method *is* intended to be overridden by the application
-        developer.
-        """
-
-    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)."""
-
-    def application_url(name=None):
-        """Return the URL of the closest application object in the
-        hierarchy or the URL of a named object (``name`` parameter)
-        relative to the closest application object.
-        """
-
     def flash(message, type='message'):
         """Send a short message to the user."""
 

Modified: grok/branches/grokcore.xxx/src/grok/meta.py
===================================================================
--- grok/branches/grokcore.xxx/src/grok/meta.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/src/grok/meta.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -47,6 +47,8 @@
 from martian.error import GrokError
 from martian import util
 
+import grokcore.view
+
 import grok
 from grok import components, formlib, templatereg
 from grok.util import make_checker
@@ -143,7 +145,7 @@
 
 
 class ViewGrokker(martian.ClassGrokker):
-    martian.component(grok.View)
+    martian.component(grokcore.view.ViewMixin)
     martian.directive(grok.context)
     martian.directive(grok.layer, default=IDefaultBrowserLayer)
     martian.directive(grok.name, get_default=default_view_name)
@@ -274,7 +276,7 @@
     # this needs to happen before any other grokkers execute that actually
     # use the templates
     martian.priority(1000)
-    
+
     def grok(self, name, instance, module_info, config, **kw):
         templates = module_info.getAnnotation('grok.templates', None)
         if templates is None:

Modified: grok/branches/grokcore.xxx/src/grok/util.py
===================================================================
--- grok/branches/grokcore.xxx/src/grok/util.py	2008-07-17 12:52:54 UTC (rev 88435)
+++ grok/branches/grokcore.xxx/src/grok/util.py	2008-07-17 12:54:36 UTC (rev 88436)
@@ -14,20 +14,16 @@
 """Grok utility functions.
 """
 
-import urllib
 
 import grok
 import zope.location.location
 from zope import component
 from zope import interface
-from zope.traversing.browser.interfaces import IAbsoluteURL
-from zope.traversing.browser.absoluteurl import _safe as SAFE_URL_CHARACTERS
 
 from zope.security.checker import NamesChecker, defineChecker
 from zope.security.interfaces import IPermission
 
 from martian.error import GrokError
-from martian.util import methods_from_class
 
 def make_checker(factory, view_factory, permission, method_names=None):
     """Make a checker for a view_factory associated with factory.
@@ -52,24 +48,10 @@
     """
     if component.queryUtility(IPermission,
                               name=permission) is None:
-       raise GrokError('Undefined permission %r in %r. Use '
-                       'grok.Permission first.'
-                       % (permission, factory), factory)
+        raise GrokError('Undefined permission %r in %r. Use '
+                        'grok.Permission first.'
+                        % (permission, factory), factory)
 
-def url(request, obj, name=None, data={}):
-    url = component.getMultiAdapter((obj, request), IAbsoluteURL)()
-    if name is not None:
-        url += '/' + urllib.quote(name.encode('utf-8'), SAFE_URL_CHARACTERS)
-    if data:
-        for k,v in data.items():
-            if isinstance(v, unicode):
-                data[k] = v.encode('utf-8')
-            if isinstance(v, (list, set, tuple)):
-                data[k] = [isinstance(item, unicode) and item.encode('utf-8')
-                or item for item in v]
-        url += '?' + urllib.urlencode(data, doseq=True)
-    return url
-
 def safely_locate_maybe(obj, parent, name):
     """Set an object's __parent__ (and __name__) if the object's
     __parent__ attribute doesn't exist yet or is None.



More information about the Checkins mailing list