[Checkins] SVN: grok/trunk/ - refactored wiki into two pages (edit and display)

Christian Theune ct at gocept.com
Wed Oct 18 17:44:52 EDT 2006


Log message for revision 70792:
   - refactored wiki into two pages (edit and display)
   - started using a common layout with macros
   - added __getitem__ to grok.View that provides access to macros if present
  

Changed:
  U   grok/trunk/grokwiki/src/grokwiki/configure.zcml
  U   grok/trunk/grokwiki/src/grokwiki/static/wiki.css
  U   grok/trunk/grokwiki/src/grokwiki/wiki.py
  U   grok/trunk/src/grok/components.py
  A   grok/trunk/src/grok/ftests/view/macros.py

-=-
Modified: grok/trunk/grokwiki/src/grokwiki/configure.zcml
===================================================================
--- grok/trunk/grokwiki/src/grokwiki/configure.zcml	2006-10-18 20:29:05 UTC (rev 70791)
+++ grok/trunk/grokwiki/src/grokwiki/configure.zcml	2006-10-18 21:44:51 UTC (rev 70792)
@@ -2,6 +2,7 @@
     xmlns="http://namespaces.zope.org/zope"
     xmlns:browser="http://namespaces.zope.org/browser"
     xmlns:grok="http://namespaces.zope.org/grok"
+    i18n_domain="grok"
     >
     <grok:grok package="."/>
 

Modified: grok/trunk/grokwiki/src/grokwiki/static/wiki.css
===================================================================
--- grok/trunk/grokwiki/src/grokwiki/static/wiki.css	2006-10-18 20:29:05 UTC (rev 70791)
+++ grok/trunk/grokwiki/src/grokwiki/static/wiki.css	2006-10-18 21:44:51 UTC (rev 70792)
@@ -8,3 +8,8 @@
     border:1px solid #999999;
     padding:1em;
 }
+
+#footer {
+    font-size:70%;
+    color:#999999;
+}

Modified: grok/trunk/grokwiki/src/grokwiki/wiki.py
===================================================================
--- grok/trunk/grokwiki/src/grokwiki/wiki.py	2006-10-18 20:29:05 UTC (rev 70791)
+++ grok/trunk/grokwiki/src/grokwiki/wiki.py	2006-10-18 21:44:51 UTC (rev 70792)
@@ -1,16 +1,36 @@
-import grok
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""The grok demo wiki
+"""
 import re
 
 from zope.app import zapi
+from zope.app.folder.folder import Folder
 
+import grok
+
+
 LINK_PATTERN = re.compile('\[\[(.*?)\]\]')
 find_wiki_links = LINK_PATTERN.findall
 
-from zope.app.folder.folder import Folder
 
+# First: The wiki application
+
 class Wiki(grok.Model, Folder):
     pass
 
+
 class WikiIndex(grok.View):
     grok.context(Wiki)
     grok.name('index')
@@ -18,60 +38,101 @@
     def render(self):
         self.request.response.redirect('home')
 
+
 @grok.subscribe(Wiki, grok.IObjectAddedEvent)
 def setupHomepage(wiki, event):
     page = WikiPage()
     wiki['home'] = page
 
+
+# Second: The wiki page
+
 class WikiPage(grok.Model):
 
     def __init__(self):
         self.text = u"GROK EMPTY WIKI PAGE. FILL!"
 
+grok.context(WikiPage)
+
+
 class Index(grok.View):
-    grok.context(WikiPage)
 
     def before(self):
-        text = self.request.form.get('wikidata')
-        self.wiki = self.context.__parent__
-        if text:
-            links = find_wiki_links(text)
-            for link in links:
-                if link not in self.wiki:
-                    self.wiki[link] = WikiPage()
-            self.context.text = text
+        wiki_url = zapi.absoluteURL(self.context.__parent__, self.request)
+        self.rendered_text, replacements = (
+            LINK_PATTERN.subn(r'<a href="%s/\1">\1</a>' % wiki_url, 
+                              self.context.text))
 
-        wiki_url = zapi.absoluteURL(self.wiki, self.request)
 
-        self.rendered_text, replacements = LINK_PATTERN.subn(r'<a href="%s/\1">\1</a>' % wiki_url, self.context.text)
+index = grok.PageTemplate("""\
+<html metal:use-macro="context/@@layout/main">
+    <div metal:fill-slot="content">
+        <h1 tal:content="context/__name__">WikiPage</h1>
 
+        <div tal:content="structure view/rendered_text" class="wikicontent">
+        </div>
 
-index = grok.PageTemplate("""\
-<html>
-<head>
-    <link rel="stylesheet" tal:attributes="href static/wiki.css" type="text/css">
-</head>
-<body>
-<h1 tal:content="context/__name__">WikiPage</h1>
+        <p><a tal:attributes="href string:${context/@@absolute_url}/edit">Edit this page</a></p>
+    </div>
+</html>""")
 
-<div tal:content="structure view/rendered_text" class="wikicontent">
-</div>
 
-<hr/>
+layout = grok.PageTemplate("""\
+<html metal:define-macro="main">
+    <head>
+        <link rel="stylesheet" tal:attributes="href static/wiki.css" type="text/css">
+    </head>
 
-<form tal:attributes="action request/URL" method="POST">
-<textarea name="wikidata" tal:content="context/text" cols="80" rows="20"/><br/>
-<input type="submit" value="Update"/>
-</form>
+    <body
+        tal:define="wiki context/__parent__;
+                    wiki_url wiki/@@absolute_url">
+        <div metal:define-slot="content">
+            Page content goes here ...
+        </div>
 
-<hr/>
-<h3>Other pages</h3>
-<p>
-    <span tal:repeat="page view/wiki">
-        <a tal:attributes="href string:${view/wiki/@@absolute_url}/$page"
-           tal:content="page"
-           />
-    </span>
-</p>
-</body>
+        <hr/>
+        <h3>Other pages</h3>
+        <p>
+            <span tal:repeat="page wiki">
+                <a tal:attributes="href string:$wiki_url/$page"
+                   tal:content="page"
+                   />
+            </span>
+        </p>
+        <hr/>
+        <div id="footer">
+        This Wiki was grokked by Zope 3.
+        </div>
+    </body>
 </html>""")
+
+
+class Edit(grok.View):
+
+    def before(self):
+        text = self.request.form.get('wikidata')
+        self.wiki = self.context.__parent__
+        if not text:
+            return  # Just render the template
+
+        # Update the text and redirect
+        links = find_wiki_links(text)
+        for link in links:
+            if link not in self.wiki:
+                self.wiki[link] = WikiPage()
+        self.context.text = text
+        wiki_url = zapi.absoluteURL(self.wiki, self.request)
+        self.request.response.redirect("%s/%s" % (wiki_url, self.context.__name__))
+
+
+edit = grok.PageTemplate("""\
+<html metal:use-macro="context/@@layout/main">
+    <div metal:fill-slot="content">
+        <h1>Edit &raquo;<span tal:replace="context/__name__">WikiPage</span>&laquo;</h1>
+
+        <form tal:attributes="action request/URL" method="POST">
+        <textarea name="wikidata" tal:content="context/text" cols="80" rows="20"/><br/>
+        <input type="submit" value="Update"/>
+        </form>
+    </div>
+</html>""")

Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2006-10-18 20:29:05 UTC (rev 70791)
+++ grok/trunk/src/grok/components.py	2006-10-18 21:44:51 UTC (rev 70792)
@@ -75,6 +75,10 @@
         namespace['static'] = directory_resource
         return template.pt_render(namespace)
 
+    def __getitem__(self, key):
+        # XXX give nice error message if template is None
+        return self.template.macros[key]
+
     def before(self):
         pass
 

Added: grok/trunk/src/grok/ftests/view/macros.py
===================================================================
--- grok/trunk/src/grok/ftests/view/macros.py	2006-10-18 20:29:05 UTC (rev 70791)
+++ grok/trunk/src/grok/ftests/view/macros.py	2006-10-18 21:44:51 UTC (rev 70792)
@@ -0,0 +1,81 @@
+"""
+  >>> import grok
+  >>> from grok.ftests.view.macros import Mammoth
+  >>> grok.grok('grok.ftests.view.macros')
+  >>> getRootFolder()["manfred"] = Mammoth()
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open("http://localhost/manfred/@@painting")
+  >>> print browser.contents
+  <html>
+  <body>
+  <h1>GROK MACRO!</h1>
+  <div>
+  GROK SLOT!
+  </div>
+  </body>
+  </html>
+
+Views without a template do not support macros:
+
+  >>> browser.open("http://localhost/manfred/@@dancing")
+  Traceback (most recent call last):
+  AttributeError: 'DancingHall' object has no attribute 'template'
+
+If the view has an attribute with the same name as a macro, the macro 
+shadows the view. XXX This should probably generate a warning at runtime.
+
+  >>> browser.open("http://localhost/manfred/@@grilldish")
+  >>> print browser.contents
+  <html>
+  Curry
+  </html>
+
+"""
+import grok
+
+class Mammoth(grok.Model):
+    pass
+
+class DancingHall(grok.View):
+
+    def render(self):
+        return "A nice large dancing hall for mammoths."
+
+class Grilled(grok.View):
+
+    def before(self):
+        self.spices = "Pepper and salt"
+
+painting = grok.PageTemplate("""\
+<html metal:use-macro="context/@@layout/main">
+<div metal:fill-slot="slot">
+GROK SLOT!
+</div>
+</html>
+""")
+
+layout = grok.PageTemplate("""\
+<html metal:define-macro="main">
+<body>
+<h1>GROK MACRO!</h1>
+<div metal:define-slot="slot">
+</div>
+</body>
+</html>""")
+
+dancing = grok.PageTemplate("""\
+<html metal:use-macro="context/@@dancinghall/something">
+</html>
+""")
+
+grilldish = grok.PageTemplate("""
+<html metal:use-macro="context/@@grilled/spices">
+</html>""")
+
+grilled = grok.PageTemplate("""\
+<html metal:define-macro="spices">
+Curry
+</html>""")


Property changes on: grok/trunk/src/grok/ftests/view/macros.py
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native



More information about the Checkins mailing list