changeset 615:0dc152d128f5

GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
author cmlenz
date Wed, 29 Aug 2007 20:12:54 +0000
parents d8f961381f75
children 80a9c247ca80
files examples/tutorial/geddit/controller.py examples/tutorial/geddit/lib/template.py examples/tutorial/geddit/templates/comments.html examples/tutorial/geddit/templates/index.html examples/tutorial/geddit/templates/info.html examples/tutorial/geddit/templates/layout.html
diffstat 6 files changed, 23 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/examples/tutorial/geddit/controller.py
+++ b/examples/tutorial/geddit/controller.py
@@ -30,12 +30,12 @@
                 _add_replies(comment)
 
     @cherrypy.expose
-    @template.output('index.html', method='html', doctype='html')
+    @template.output('index.html')
     def index(self):
         return template.render(submissions=self.data)
 
     @cherrypy.expose
-    @template.output('info.html', method='html', doctype='html')
+    @template.output('info.html')
     def info(self, code):
         submission = self._submission_lookup.get(code)
         if not submission:
@@ -43,7 +43,7 @@
         return template.render(submission=submission)
 
     @cherrypy.expose
-    @template.output('submit.html', method='html', doctype='html')
+    @template.output('submit.html')
     def submit(self, cancel=False, **data):
         if cherrypy.request.method == 'POST':
             if cancel:
@@ -63,7 +63,7 @@
         return template.render(errors=errors) | HTMLFormFiller(data=data)
 
     @cherrypy.expose
-    @template.output('comment.html', method='html', doctype='html')
+    @template.output('comment.html')
     def comment(self, code, cancel=False, **data):
         submission = self._submission_lookup.get(code)
         if not submission:
@@ -76,7 +76,7 @@
                 data = form.to_python(data)
                 comment = submission.add_comment(**data)
                 self._comment_lookup[comment.code] = comment
-                raise cherrypy.HTTPRedirect('/')
+                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
             except Invalid, e:
                 errors = e.unpack_errors()
         else:
@@ -86,7 +86,7 @@
                                errors=errors)
 
     @cherrypy.expose
-    @template.output('comment.html', method='html', doctype='html')
+    @template.output('comment.html')
     def reply(self, code, cancel=False, **data):
         comment = self._comment_lookup.get(code)
         submission = comment.submission
@@ -100,7 +100,7 @@
                 data = form.to_python(data)
                 comment = comment.add_reply(**data)
                 self._comment_lookup[comment.code] = comment
-                raise cherrypy.HTTPRedirect('/')
+                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
             except Invalid, e:
                 errors = e.unpack_errors()
         else:
--- a/examples/tutorial/geddit/lib/template.py
+++ b/examples/tutorial/geddit/lib/template.py
@@ -3,14 +3,14 @@
 import cherrypy
 from genshi.core import Stream
 from genshi.output import encode, get_serializer
-from genshi.template import TemplateLoader
+from genshi.template import Context, TemplateLoader
 
 loader = TemplateLoader(
     os.path.join(os.path.dirname(__file__), '..', 'templates'),
     auto_reload=True
 )
 
-def output(filename, method=None, encoding='utf-8', **options):
+def output(filename, method='html', encoding='utf-8', **options):
     """Decorator for exposed methods to specify what template the should use
     for rendering, and which serialization method and options should be
     applied.
@@ -18,6 +18,8 @@
     def decorate(func):
         def wrapper(*args, **kwargs):
             cherrypy.thread_data.template = loader.load(filename)
+            if method == 'html':
+                options.setdefault('doctype', 'html')
             serializer = get_serializer(method, **options)
             stream = func(*args, **kwargs)
             if not isinstance(stream, Stream):
@@ -37,4 +39,6 @@
         template = loader.load(args[0])
     else:
         template = cherrypy.thread_data.template
-    return template.generate(**kwargs)
+    ctxt = Context(url=cherrypy.url)
+    ctxt.push(kwargs)
+    return template.generate(ctxt)
--- a/examples/tutorial/geddit/templates/comments.html
+++ b/examples/tutorial/geddit/templates/comments.html
@@ -7,7 +7,7 @@
     <strong>${comment.username}</strong>
     at ${comment.time.strftime('%M/%d/%Y %H:%m')}
     <blockquote>${comment.content}</blockquote>
-    <a href="/reply/${comment.code}/">reply</a>
+    <a href="${url('/reply/%s/' % comment.code)}">reply</a>
     <xi:include href="comments.html" py:with="comments=comment.replies" />
   </li>
 </ul>
--- a/examples/tutorial/geddit/templates/index.html
+++ b/examples/tutorial/geddit/templates/index.html
@@ -8,7 +8,7 @@
   </head>
   <body>
     <h1>News</h1>
-    <p><a href="/submit/">Submit new link</a></p>
+    <p><a href="${url('/submit/')}">Submit new link</a></p>
 
     <ol py:if="submissions" class="submissions">
       <li py:for="submission in submissions">
@@ -16,7 +16,9 @@
         posted by ${submission.username}
         at ${submission.time.strftime('%M/%d/%Y %H:%m')}<br />
         <div class="info">
-          <a href="/info/${submission.code}/">${len(submission.total_comments)} comments</a>
+          <a href="${url('/info/%s/' % submission.code)}">
+            ${len(submission.total_comments)} comments
+          </a>
         </div>
       </li>
     </ol>
--- a/examples/tutorial/geddit/templates/info.html
+++ b/examples/tutorial/geddit/templates/info.html
@@ -11,7 +11,7 @@
     <a href="${submission.url}">${submission.url}</a><br />
     posted by ${submission.username}
     at ${submission.time.strftime('%M/%d/%Y %H:%m')}<br />
-    <a href="/comment/${submission.code}/">comment</a>
+    <a href="${url('/comment/%s/' % submission.code)}">comment</a>
     <xi:include href="comments.html" py:with="comments=submission.comments" />
   </body>
 </html>
--- a/examples/tutorial/geddit/templates/layout.html
+++ b/examples/tutorial/geddit/templates/layout.html
@@ -7,8 +7,8 @@
       <title py:with="title = list(select('title/text()'))">
         geddit<py:if test="title">: ${title}</py:if>
       </title>
-      <link rel="stylesheet" href="/media/layout.css" type="text/css" />
-      <script type="text/javascript" src="/media/jquery.js"></script>
+      <link rel="stylesheet" href="${url('/media/layout.css')}" type="text/css" />
+      <script type="text/javascript" src="${url('/media/jquery.js')}"></script>
       ${select('*[local-name()!="title"]')}
     </head>
   </py:match>
@@ -16,7 +16,7 @@
   <py:match path="body" once="true">
     <body py:attrs="select('@*')"><div id="wrap">
       <div id="header">
-        <a href="/"><img src="/media/logo.gif" width="201" height="79" alt="geddit?" /></a>
+        <a href="/"><img src="${url('/media/logo.gif')}" width="201" height="79" alt="geddit?" /></a>
       </div>
       <div id="content">
         ${select('*|text()')}
Copyright (C) 2012-2017 Edgewall Software