[Checkins] SVN: grok/trunk/src/grok/ Don't automatically grok templates that occur w/o a corresponding view class.

Philipp von Weitershausen philikon at philikon.de
Wed Feb 7 19:28:44 EST 2007


Log message for revision 72433:
  Don't automatically grok templates that occur w/o a corresponding view class.
  To write any publishable view now, be it rendered in Python or by a template,
  you need to write a view class now, inheriting from grok.View.  To make existing
  templates continue to work, simply introduce empty classes like so:
  
    class Index(grok.View):
        pass
  
  

Changed:
  U   grok/trunk/src/grok/ftests/static/simple_fixture/ellie.py
  U   grok/trunk/src/grok/ftests/traversal/containertraverse.py
  U   grok/trunk/src/grok/ftests/traversal/containertraverser.py
  U   grok/trunk/src/grok/ftests/traversal/modeltraverse.py
  U   grok/trunk/src/grok/ftests/traversal/traverser.py
  U   grok/trunk/src/grok/ftests/url/url.py
  U   grok/trunk/src/grok/ftests/view/index.py
  U   grok/trunk/src/grok/ftests/view/macros.py
  U   grok/trunk/src/grok/ftests/view/view.py
  U   grok/trunk/src/grok/grokker.py
  U   grok/trunk/src/grok/templatereg.py
  D   grok/trunk/src/grok/tests/error/filesystemtemplate.py
  D   grok/trunk/src/grok/tests/error/filesystemtemplate_templates/
  U   grok/trunk/src/grok/tests/scan/stoneage/cave.py
  U   grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py
  U   grok/trunk/src/grok/tests/view/ambiguouscontext.py
  U   grok/trunk/src/grok/tests/view/dirtemplate.py
  U   grok/trunk/src/grok/tests/view/inline.py
  A   grok/trunk/src/grok/tests/view/inline_unassociated.py
  U   grok/trunk/src/grok/tests/view/missingcontext.py
  U   grok/trunk/src/grok/tests/view/templatedirectory.py
  U   grok/trunk/src/grok/tests/view/templatefile.py
  U   grok/trunk/src/grok/tests/view/templatereload.py
  A   grok/trunk/src/grok/tests/view/unassociated.py
  A   grok/trunk/src/grok/tests/view/unassociated_templates/
  A   grok/trunk/src/grok/tests/view/unassociated_templates/index.pt

-=-
Modified: grok/trunk/src/grok/ftests/static/simple_fixture/ellie.py
===================================================================
--- grok/trunk/src/grok/ftests/static/simple_fixture/ellie.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/static/simple_fixture/ellie.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -3,6 +3,9 @@
 class Mammoth(grok.Model):
     pass
 
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/traversal/containertraverse.py
===================================================================
--- grok/trunk/src/grok/ftests/traversal/containertraverse.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/traversal/containertraverse.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -92,6 +92,10 @@
         return "special view"
 
 grok.context(Mammoth)
+
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/traversal/containertraverser.py
===================================================================
--- grok/trunk/src/grok/ftests/traversal/containertraverser.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/traversal/containertraverser.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -80,6 +80,9 @@
 
 grok.context(Mammoth)
 
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/traversal/modeltraverse.py
===================================================================
--- grok/trunk/src/grok/ftests/traversal/modeltraverse.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/traversal/modeltraverse.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -43,6 +43,10 @@
         self.name = name
 
 grok.context(Mammoth)
+
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/traversal/traverser.py
===================================================================
--- grok/trunk/src/grok/ftests/traversal/traverser.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/traversal/traverser.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -3,8 +3,8 @@
 also create a separate traverser component:
 
   >>> import grok
-  >>> from grok.ftests.traversal.modeltraverse import Herd
-  >>> grok.grok('grok.ftests.traversal.modeltraverse')
+  >>> from grok.ftests.traversal.traverser import Herd
+  >>> grok.grok('grok.ftests.traversal.traverser')
   >>> getRootFolder()["herd"] = Herd()
 
   >>> from zope.testbrowser.testing import Browser
@@ -44,6 +44,10 @@
         self.name = name
 
 grok.context(Mammoth)
+
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/url/url.py
===================================================================
--- grok/trunk/src/grok/ftests/url/url.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/url/url.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -22,9 +22,9 @@
   >>> browser.open("http://localhost/herd/manfred/another")
   >>> print browser.contents
   http://localhost/herd/manfred/another
-  >>> browser.open("http://localhost/herd/manfred/yet_another")
+  >>> browser.open("http://localhost/herd/manfred/yetanother")
   >>> print browser.contents
-  http://localhost/herd/manfred/yet_another
+  http://localhost/herd/manfred/yetanother
   
 We get the views manually so we can do a greater variety of url() calls:
 
@@ -35,13 +35,13 @@
   >>> index_view.url()
   'http://127.0.0.1/herd/manfred/index'
   >>> another_view = component.getMultiAdapter((manfred, request),
-  ... name='another')
+  ...                                              name='another')
   >>> another_view.url()
   'http://127.0.0.1/herd/manfred/another'
   >>> yet_another_view = component.getMultiAdapter((manfred, request),
-  ... name='yet_another')
+  ...                                              name='yetanother')
   >>> yet_another_view.url()
-  'http://127.0.0.1/herd/manfred/yet_another'
+  'http://127.0.0.1/herd/manfred/yetanother'
 
 Now let's get a URL for a specific object:
 
@@ -121,4 +121,7 @@
     def render(self):
         return self.url()
 
-yet_another = grok.PageTemplate('<p tal:replace="view/url" />')
+class YetAnother(grok.View):
+    pass
+
+yetanother = grok.PageTemplate('<p tal:replace="view/url" />')

Modified: grok/trunk/src/grok/ftests/view/index.py
===================================================================
--- grok/trunk/src/grok/ftests/view/index.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/view/index.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -4,6 +4,8 @@
   >>> grok.grok('grok.ftests.view.index')
   >>> getRootFolder()["manfred"] = Mammoth()
 
+The default view name for a model is 'index':
+
   >>> from zope.testbrowser.testing import Browser
   >>> browser = Browser()
   >>> browser.handleErrors = False
@@ -23,6 +25,9 @@
 class Mammoth(grok.Model):
     teeth = u"Blue"
 
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/ftests/view/macros.py
===================================================================
--- grok/trunk/src/grok/ftests/view/macros.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/view/macros.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -49,6 +49,9 @@
     def update(self):
         self.spices = "Pepper and salt"
 
+class Painting(grok.View):
+    pass
+
 painting = grok.PageTemplate("""\
 <html metal:use-macro="context/@@layout/main">
 <div metal:fill-slot="slot">
@@ -57,6 +60,9 @@
 </html>
 """)
 
+class Layout(grok.View):
+    pass
+
 layout = grok.PageTemplate("""\
 <html metal:define-macro="main">
 <body>
@@ -66,15 +72,24 @@
 </body>
 </html>""")
 
+class Dancing(grok.View):
+    pass
+
 dancing = grok.PageTemplate("""\
 <html metal:use-macro="context/@@dancinghall/something">
 </html>
 """)
 
+class GrillDish(grok.View):
+    pass
+
 grilldish = grok.PageTemplate("""
 <html metal:use-macro="context/@@grilled/spices">
 </html>""")
 
+class Grilled(grok.View):
+    pass
+
 grilled = grok.PageTemplate("""\
 <html metal:define-macro="spices">
 Curry

Modified: grok/trunk/src/grok/ftests/view/view.py
===================================================================
--- grok/trunk/src/grok/ftests/view/view.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/ftests/view/view.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -21,6 +21,9 @@
 class Mammoth(grok.Model):
     pass
 
+class Painting(grok.View):
+    pass
+
 painting = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/grokker.py
===================================================================
--- grok/trunk/src/grok/grokker.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/grokker.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -1,5 +1,6 @@
 import grok
 from grok import util, templatereg
+from grok.error import GrokError
 
 class GrokkerRegistry(object):
     def __init__(self):
@@ -85,7 +86,13 @@
                                  name, component,
                                  module_info, templates)
 
-        templates.registerUnassociated(context, module_info)
+        unassociated = list(templates.listUnassociated())
+        if unassociated:
+            raise GrokError("Found the following unassociated template(s) when "
+                            "grokking %r: %s.  Define view classes inheriting "
+                            "from grok.View to enable the template(s)."
+                            % (module_info.dotted_name,
+                               ', '.join(unassociated)), module_info)
 
 
 # deep meta mode here - we define grokkers for grok.ClassGrokker,

Modified: grok/trunk/src/grok/templatereg.py
===================================================================
--- grok/trunk/src/grok/templatereg.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/templatereg.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -1,11 +1,5 @@
 import os
-
-from zope import interface, component
-from zope.publisher.interfaces.browser import IDefaultBrowserLayer
-from zope.security.checker import NamesChecker, defineChecker
-
 import grok
-from grok import util
 from grok.error import GrokError
 
 
@@ -61,25 +55,4 @@
     def listUnassociated(self):
         for name, entry in self._reg.iteritems():
             if not entry['associated']:
-                yield name, entry['template']
-
-    def registerUnassociated(self, context, module_info):
-        for name, unassociated in self.listUnassociated():
-            util.check_context(unassociated, context)
-
-            module_info_ = module_info
-            class TemplateView(grok.View):
-                template = unassociated
-                module_info = module_info_
-
-            self.markAssociated(name)
-
-            TemplateView.__view_name__ = name
-            component.provideAdapter(TemplateView,
-                                     adapts=(context, IDefaultBrowserLayer),
-                                     provides=interface.Interface,
-                                     name=name)
-
-            # protect view, public by default
-            checker = NamesChecker(['__call__'])
-            defineChecker(TemplateView, checker)
+                yield name

Deleted: grok/trunk/src/grok/tests/error/filesystemtemplate.py
===================================================================
--- grok/trunk/src/grok/tests/error/filesystemtemplate.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/error/filesystemtemplate.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -1,11 +0,0 @@
-"""
-
-This test checks for the right name in the error message if a filesystem-based
-template can not find a context to be associated with:
-
-  >>> grok.grok(__name__)
-  Traceback (most recent call last):
-  GrokError: No module-level context for <nocontext template in ...grok/tests/error/filesystemtemplate_templates/nocontext.pt>, please use grok.context.
-
-"""
-import grok

Modified: grok/trunk/src/grok/tests/scan/stoneage/cave.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/cave.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/scan/stoneage/cave.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -3,6 +3,9 @@
 class Cave(grok.Model):
     pass
 
+class Index(grok.View):
+    pass
+
 index = grok.PageTemplate("""\
 <html>
 <body>

Modified: grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -2,3 +2,6 @@
 
 class Mammoth(grok.Model):
     pass
+
+class Index(grok.View):
+    pass

Modified: grok/trunk/src/grok/tests/view/ambiguouscontext.py
===================================================================
--- grok/trunk/src/grok/tests/view/ambiguouscontext.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/ambiguouscontext.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -5,7 +5,7 @@
   Traceback (most recent call last):
     ...
   GrokError: Multiple possible contexts for
-  <club template in grok.tests.view.ambiguouscontext>, please use grok.context.
+  <class 'grok.tests.view.ambiguouscontext.Club'>, please use grok.context.
 
 """
 
@@ -17,6 +17,5 @@
 class Mammoth(grok.Model):
     pass
 
-club = grok.PageTemplate("""\
-<html><body><h1>GROK CLUB MAMMOTH!</h1></body></html>
-""")
+class Club(grok.View):
+    pass

Modified: grok/trunk/src/grok/tests/view/dirtemplate.py
===================================================================
--- grok/trunk/src/grok/tests/view/dirtemplate.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/dirtemplate.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -31,3 +31,6 @@
 
 class CavePainting(grok.View):
     pass
+
+class Food(grok.View):
+    pass

Modified: grok/trunk/src/grok/tests/view/inline.py
===================================================================
--- grok/trunk/src/grok/tests/view/inline.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/inline.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -26,13 +26,6 @@
 the template.  This shows that the association of inline PageTemplate
 and the view class is successful.
 
-Templates that are not associated with a view class will still be
-registered on the model:
-
-  >>> view = component.getMultiAdapter((manfred, request), name='club')
-  >>> print view()
-  <html><body><h1>GROK CLUB MAMMOTH!</h1></body></html>
-
 Finding a template does not depend on the view name, but on the class
 name:
 
@@ -63,10 +56,6 @@
 </html>
 """)
 
-club = grok.PageTemplate("""\
-<html><body><h1>GROK CLUB MAMMOTH!</h1></body></html>
-""")
-
 class Hunt(grok.View):
     grok.name('hunting')
 

Added: grok/trunk/src/grok/tests/view/inline_unassociated.py
===================================================================
--- grok/trunk/src/grok/tests/view/inline_unassociated.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/inline_unassociated.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -0,0 +1,20 @@
+"""
+Inline templates that are not associated with a view class will
+provoke an error:
+
+  >>> grok.grok(__name__)
+  Traceback (most recent call last):
+  ...
+  GrokError: Found the following unassociated template(s) when grokking
+  'grok.tests.view.inline_unassociated': club.  Define view classes inheriting
+  from grok.View to enable the template(s).
+
+"""
+import grok
+
+class Mammoth(grok.Model):
+    pass
+
+club = grok.PageTemplate("""\
+<html><body><h1>GROK CLUB MAMMOTH!</h1></body></html>
+""")


Property changes on: grok/trunk/src/grok/tests/view/inline_unassociated.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: grok/trunk/src/grok/tests/view/missingcontext.py
===================================================================
--- grok/trunk/src/grok/tests/view/missingcontext.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/missingcontext.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -1,16 +1,15 @@
 """
-Templates without a context cannot be grokked:
+Views without a context cannot be grokked:
 
   >>> grok.grok(__name__)
   Traceback (most recent call last):
     ...
   GrokError: No module-level context for
-  <club template in grok.tests.view.missingcontext>, please use grok.context.
+  <class 'grok.tests.view.missingcontext.Club'>, please use grok.context.
 
 """
 
 import grok
 
-club = grok.PageTemplate("""\
-<html><body><h1>GROK CLUB MAMMOTH!</h1></body></html>
-""")
+class Club(grok.View):
+    pass

Modified: grok/trunk/src/grok/tests/view/templatedirectory.py
===================================================================
--- grok/trunk/src/grok/tests/view/templatedirectory.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/templatedirectory.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -22,3 +22,6 @@
 
 class Mammoth(grok.Model):
     pass
+
+class Food(grok.View):
+    pass

Modified: grok/trunk/src/grok/tests/view/templatefile.py
===================================================================
--- grok/trunk/src/grok/tests/view/templatefile.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/templatefile.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -24,5 +24,8 @@
 class Mammoth(grok.Model):
     pass
 
+class Food(grok.View):
+    pass
+
 food = grok.PageTemplateFile(os.path.join('templatedirectoryname',
                                           'food.pt'))

Modified: grok/trunk/src/grok/tests/view/templatereload.py
===================================================================
--- grok/trunk/src/grok/tests/view/templatereload.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/templatereload.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -34,3 +34,6 @@
 
 class Mammoth(grok.Model):
     pass
+
+class Index(grok.View):
+    pass

Added: grok/trunk/src/grok/tests/view/unassociated.py
===================================================================
--- grok/trunk/src/grok/tests/view/unassociated.py	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/unassociated.py	2007-02-08 00:28:43 UTC (rev 72433)
@@ -0,0 +1,17 @@
+"""
+Templates that are not associated with a view class will provoke an
+error:
+
+  >>> grok.grok(__name__)
+  Traceback (most recent call last):
+  ...
+  GrokError: Found the following unassociated template(s) when grokking
+  'grok.tests.view.unassociated': index.  Define view classes inheriting
+  from grok.View to enable the template(s).
+
+
+"""
+import grok
+
+class Mammoth(grok.Model):
+    pass


Property changes on: grok/trunk/src/grok/tests/view/unassociated.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: grok/trunk/src/grok/tests/view/unassociated_templates/index.pt
===================================================================
--- grok/trunk/src/grok/tests/view/unassociated_templates/index.pt	2007-02-07 23:47:28 UTC (rev 72432)
+++ grok/trunk/src/grok/tests/view/unassociated_templates/index.pt	2007-02-08 00:28:43 UTC (rev 72433)
@@ -0,0 +1,3 @@
+This template does not have an associated view class (in other words,
+there's no grok.tests.view.unassociated.Index class), therefore it
+provokes a GrokError.


Property changes on: grok/trunk/src/grok/tests/view/unassociated_templates/index.pt
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list