changeset 619:71d3edd302ae

GenshiTutorial: various updates to sync with wiki page.
author cmlenz
date Thu, 30 Aug 2007 14:19:59 +0000
parents 5a2059ce1f0b
children f007a645dfd0
files examples/tutorial/geddit/controller.py examples/tutorial/geddit/form.py examples/tutorial/geddit/model.py examples/tutorial/geddit/static/layout.css examples/tutorial/geddit/templates/comment.html examples/tutorial/geddit/templates/index.html examples/tutorial/geddit/templates/info.html
diffstat 7 files changed, 44 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/examples/tutorial/geddit/controller.py
+++ b/examples/tutorial/geddit/controller.py
@@ -8,11 +8,10 @@
 import cherrypy
 from formencode import Invalid
 from genshi.filters import HTMLFormFiller
-from paste.evalexception.middleware import EvalException
 
-from geddit.form import SubmissionForm, CommentForm
+from geddit.form import LinkForm, CommentForm
 from geddit.lib import template
-from geddit.model import Submission, Comment
+from geddit.model import Link, Comment
 
 
 class Root(object):
@@ -23,19 +22,16 @@
     @cherrypy.expose
     @template.output('index.html')
     def index(self):
-        return template.render(
-            submissions=sorted(self.data.values(),
-                               key=operator.attrgetter('time'),
-                               reverse=True)
-        )
+        links = sorted(self.data.values(), key=operator.attrgetter('time'))
+        return template.render(links=links)
 
     @cherrypy.expose
     @template.output('info.html')
     def info(self, code):
-        submission = self.data.get(code)
-        if not submission:
+        link = self.data.get(code)
+        if not link:
             raise cherrypy.NotFound()
-        return template.render(submission=submission)
+        return template.render(link=link)
 
     @cherrypy.expose
     @template.output('submit.html')
@@ -43,11 +39,11 @@
         if cherrypy.request.method == 'POST':
             if cancel:
                 raise cherrypy.HTTPRedirect('/')
-            form = SubmissionForm()
+            form = LinkForm()
             try:
                 data = form.to_python(data)
-                submission = Submission(**data)
-                self.data[submission.code] = submission
+                link = Link(**data)
+                self.data[link.id] = link
                 raise cherrypy.HTTPRedirect('/')
             except Invalid, e:
                 errors = e.unpack_errors()
@@ -59,23 +55,23 @@
     @cherrypy.expose
     @template.output('comment.html')
     def comment(self, code, cancel=False, **data):
-        submission = self.data.get(code)
-        if not submission:
+        link = self.data.get(code)
+        if not link:
             raise cherrypy.NotFound()
         if cherrypy.request.method == 'POST':
             if cancel:
-                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
+                raise cherrypy.HTTPRedirect('/info/%s' % link.id)
             form = CommentForm()
             try:
                 data = form.to_python(data)
-                comment = submission.add_comment(**data)
-                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
+                comment = link.add_comment(**data)
+                raise cherrypy.HTTPRedirect('/info/%s' % link.id)
             except Invalid, e:
                 errors = e.unpack_errors()
         else:
             errors = {}
 
-        return template.render(submission=submission, comment=None,
+        return template.render(link=link, comment=None,
                                errors=errors) | HTMLFormFiller(data=data)
 
 
@@ -99,7 +95,8 @@
             fileobj.close()
     cherrypy.engine.on_stop_engine_list.append(_save_data)
 
-    # Some global configuration; note that this could be moved into a configuration file
+    # Some global configuration; note that this could be moved into a 
+    # configuration file
     cherrypy.config.update({
         'request.throw_errors': True,
         'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
@@ -108,10 +105,7 @@
         'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
     })
 
-    # Initialize the application, and add EvalException for more helpful error messages
-    app = cherrypy.Application(Root(data))
-    app.wsgiapp.pipeline.append(('paste_exc', EvalException))
-    cherrypy.quickstart(app, '/', {
+    cherrypy.quickstart(Root(data), '/', {
         '/media': {
             'tools.staticdir.on': True,
             'tools.staticdir.dir': 'static'
--- a/examples/tutorial/geddit/form.py
+++ b/examples/tutorial/geddit/form.py
@@ -1,7 +1,7 @@
 from formencode import Schema, validators
 
 
-class SubmissionForm(Schema):
+class LinkForm(Schema):
     username = validators.UnicodeString(not_empty=True)
     url = validators.URL(not_empty=True, add_http=True, check_exists=False)
     title = validators.UnicodeString(not_empty=True)
--- a/examples/tutorial/geddit/model.py
+++ b/examples/tutorial/geddit/model.py
@@ -1,14 +1,14 @@
 from datetime import datetime
 
 
-class Submission(object):
+class Link(object):
 
     def __init__(self, username, url, title):
         self.username = username
         self.url = url
         self.title = title
         self.time = datetime.utcnow()
-        self.code = hex(hash(tuple([username, url, title, self.time])))[2:]
+        self.id = hex(hash(tuple([username, url, title, self.time])))[2:]
         self.comments = []
 
     def __repr__(self):
--- a/examples/tutorial/geddit/static/layout.css
+++ b/examples/tutorial/geddit/static/layout.css
@@ -2,6 +2,7 @@
 body { background: #ddd; color: #333; font: normal 90%/1.3 Arial,Helvetica,sans-serif; }
 :link, :visited { color: #c10000; text-decoration: none; }
 :link:hover, :visited:hover { text-decoration: underline; }
+:link img, :visited img { border: none; }
 h1 { color: #666;
   font: normal xx-large/1.5 Georgia,serif; margin: 0 0 .5em;
 }
@@ -21,7 +22,11 @@
 #footer hr { display: none; }
 .legalese { color: #999; margin: 0; }
 
-ol.submissions li .info { font-size: 85%; }
-ol.submissions li .info :link, ol.submissions li .info :visited { color: #666; }
+ol.links li .info { font-size: 85%; }
+ol.links li .info :link, ol.links li .info :visited { color: #666; }
+
 ul.comments { list-style: none; margin: 1em 0; padding: 0 0 0 1em; }
 ul.comments li { color: #999; margin: 0 0 1em; }
+ul.comments blockquote { color: #333; font-style: normal; margin: 0;
+  padding: 0;
+}
--- a/examples/tutorial/geddit/templates/comment.html
+++ b/examples/tutorial/geddit/templates/comment.html
@@ -4,10 +4,10 @@
       xmlns:py="http://genshi.edgewall.org/">
   <xi:include href="layout.html" />
   <head>
-    <title>Comment on “${submission.title}”</title>
+    <title>Comment on “${link.title}”</title>
   </head>
   <body>
-    <h1>Comment on “${submission.title}”</h1>
+    <h1>Comment on “${link.title}”</h1>
     <p py:if="comment">
       In reply to <strong>${comment.username}</strong>
       at ${comment.time.strftime('%M/%d/%Y %H:%m')}:
--- a/examples/tutorial/geddit/templates/index.html
+++ b/examples/tutorial/geddit/templates/index.html
@@ -10,14 +10,13 @@
     <h1>News</h1>
     <p><a href="${url('/submit/')}">Submit new link</a></p>
 
-    <ol py:if="submissions" class="submissions">
-      <li py:for="submission in submissions">
-        <a href="${submission.url}">${submission.title}</a>
-        posted by ${submission.username}
-        at ${submission.time.strftime('%m/%d/%Y %H:%M')}<br />
+    <ol py:if="links" class="links">
+      <li py:for="link in links">
+        <a href="${link.url}">${link.title}</a>
+        posted by ${link.username} at ${link.time.strftime('%m/%d/%Y %H:%M')}
         <div class="info">
-          <a href="${url('/info/%s/' % submission.code)}">
-            ${len(submission.comments)} comments
+          <a href="${url('/info/%s/' % link.id)}">
+            ${len(link.comments)} comments
           </a>
         </div>
       </li>
--- a/examples/tutorial/geddit/templates/info.html
+++ b/examples/tutorial/geddit/templates/info.html
@@ -4,16 +4,15 @@
       xmlns:py="http://genshi.edgewall.org/">
   <xi:include href="layout.html" />
   <head>
-    <title>${submission.title}</title>
+    <title>${link.title}</title>
   </head>
   <body>
-    <h1>${submission.title}</h1>
-    <a href="${submission.url}">${submission.url}</a><br />
-    posted by ${submission.username}
-    at ${submission.time.strftime('%m/%d/%Y %H:%M')}<br />
-    <a href="${url('/comment/%s/' % submission.code)}">comment</a>
-    <ul py:if="submission.comments" class="comments">
-      <li py:for="comment in submission.comments">
+    <h1>${link.title}</h1>
+    <a href="${link.url}">${link.url}</a><br />
+    posted by ${link.username} at ${link.time.strftime('%m/%d/%Y %H:%M')}<br />
+    <a href="${url('/comment/%s/' % link.id)}">comment</a>
+    <ul py:if="link.comments" class="comments">
+      <li py:for="comment in link.comments">
         <strong>${comment.username}</strong>
         at ${comment.time.strftime('%m/%d/%Y %H:%M')}
         <blockquote>${comment.content}</blockquote>
Copyright (C) 2012-2017 Edgewall Software