[Checkins] SVN: grok/trunk/ actually commit the merged getApplication() functionality

Jan-Wijbrand Kolman janwijbrand at gmail.com
Wed Dec 9 04:19:29 EST 2009


Log message for revision 106316:
  actually commit the merged getApplication() functionality

Changed:
  U   grok/trunk/CHANGES.txt
  U   grok/trunk/src/grok/__init__.py
  A   grok/trunk/src/grok/ftests/application/
  U   grok/trunk/src/grok/ftests/test_grok_functional.py
  U   grok/trunk/src/grok/interfaces.py
  U   grok/trunk/src/grok/util.py

-=-
Modified: grok/trunk/CHANGES.txt
===================================================================
--- grok/trunk/CHANGES.txt	2009-12-09 08:41:25 UTC (rev 106315)
+++ grok/trunk/CHANGES.txt	2009-12-09 09:19:29 UTC (rev 106316)
@@ -4,6 +4,9 @@
 1.1a2 (unreleased)
 ==================
 
+* Add grok.getApplication() that, similar to grok.getSite() retrieves
+  the "nearest" enclosing grok.Application object.
+
 * Use zope.container instead of zope.app.container.
 
 * Use zope.catalog instead of zope.app.catalog.

Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2009-12-09 08:41:25 UTC (rev 106315)
+++ grok/trunk/src/grok/__init__.py	2009-12-09 09:19:29 UTC (rev 106316)
@@ -57,6 +57,7 @@
 
 from zope.event import notify
 from zope.site.hooks import getSite
+from grok.util import getApplication
 from zope.lifecycleevent import (
     IObjectCreatedEvent, ObjectCreatedEvent,
     IObjectModifiedEvent, ObjectModifiedEvent,

Modified: grok/trunk/src/grok/ftests/test_grok_functional.py
===================================================================
--- grok/trunk/src/grok/ftests/test_grok_functional.py	2009-12-09 08:41:25 UTC (rev 106315)
+++ grok/trunk/src/grok/ftests/test_grok_functional.py	2009-12-09 09:19:29 UTC (rev 106316)
@@ -71,7 +71,8 @@
 def test_suite():
     suite = unittest.TestSuite()
     for name in ['xmlrpc', 'traversal', 'form', 'url', 'security', 'rest',
-                 'catalog', 'site', 'viewlet', 'json', 'lifecycle']:
+                 'catalog', 'site', 'application', 'viewlet', 'json',
+                 'lifecycle']:
         suite.addTest(suiteFromPackage(name))
     return suite
 

Modified: grok/trunk/src/grok/interfaces.py
===================================================================
--- grok/trunk/src/grok/interfaces.py	2009-12-09 08:41:25 UTC (rev 106315)
+++ grok/trunk/src/grok/interfaces.py	2009-12-09 09:19:29 UTC (rev 106316)
@@ -147,6 +147,8 @@
     def getSite():
         """Get the current site."""
 
+    def getApplication():
+        """Return the nearest enclosing `grok.Application`."""
 
     IRESTSkinType = interface.Attribute('The REST skin type')
 

Modified: grok/trunk/src/grok/util.py
===================================================================
--- grok/trunk/src/grok/util.py	2009-12-09 08:41:25 UTC (rev 106315)
+++ grok/trunk/src/grok/util.py	2009-12-09 09:19:29 UTC (rev 106316)
@@ -14,6 +14,7 @@
 """Grok utility functions.
 """
 import grok
+import grok.interfaces
 import zope.event
 import zope.location.location
 from zope import interface
@@ -24,7 +25,6 @@
 from grokcore.view.util import url
 from grokcore.security.util import check_permission
 
-
 def make_checker(factory, view_factory, permission, method_names=None):
     """Make a checker for a view_factory associated with factory.
 
@@ -65,7 +65,30 @@
     ifaces.append(skin)
     interface.directlyProvides(request, *ifaces)
 
+def getApplication():
+    """Return the nearest enclosing `grok.Application`.
 
+    Raises ValueError if no Application can be found.
+    """
+    site = grok.getSite()
+    if grok.interfaces.IApplication.providedBy(site):
+        return site
+    # Another sub-site is within the application. Walk up the object
+    # tree until we get to the an application.
+    obj = site
+    while obj is not None:
+        if isinstance(obj, grok.Application):
+            return obj
+        obj = obj.__parent__
+    raise ValueError("No application found.")
+
+def application_url(request, obj, name=None, data={}):
+    """Return the URL of the nearest enclosing `grok.Application`.
+
+    Raises ValueError if no Application can be found.
+    """
+    return url(request, getApplication(), name, data)
+
 def create_application(factory, container, name):
     """Creates an application and triggers the events from
     the application lifecycle.
@@ -92,12 +115,3 @@
     grok.notify(grok.ApplicationInitializedEvent(application))
 
     return application
-
-
-def application_url(request, obj, name=None, data={}):
-    """Return the URL of the nearest enclosing `grok.Application`."""
-    while obj is not None:
-        if isinstance(obj, grok.Application):
-            return url(request, obj, name, data)
-        obj = obj.__parent__
-    raise ValueError("No application found.")



More information about the checkins mailing list