changeset 618:5a2059ce1f0b

Simplify the tutorial project: comments are now flat, not hierarchical.
author cmlenz
date Thu, 30 Aug 2007 09:08:06 +0000
parents 47c9c9ec204e
children 71d3edd302ae
files examples/tutorial/geddit/controller.py examples/tutorial/geddit/model.py examples/tutorial/geddit/static/layout.css examples/tutorial/geddit/templates/comments.html examples/tutorial/geddit/templates/index.html examples/tutorial/geddit/templates/info.html
diffstat 6 files changed, 29 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/examples/tutorial/geddit/controller.py
+++ b/examples/tutorial/geddit/controller.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import operator
 import os
 import pickle
 import sys
@@ -18,26 +19,20 @@
 
     def __init__(self, data):
         self.data = data
-        self._submission_lookup = {}
-        self._comment_lookup = {}
-        for submission in self.data:
-            self._submission_lookup[submission.code] = submission
-            for comment in submission.comments:
-                self._comment_lookup[comment.code] = comment
-                def _add_replies(comment):
-                    for reply in comment.replies:
-                        self._comment_lookup[reply.code] = reply
-                _add_replies(comment)
 
     @cherrypy.expose
     @template.output('index.html')
     def index(self):
-        return template.render(submissions=self.data)
+        return template.render(
+            submissions=sorted(self.data.values(),
+                               key=operator.attrgetter('time'),
+                               reverse=True)
+        )
 
     @cherrypy.expose
     @template.output('info.html')
     def info(self, code):
-        submission = self._submission_lookup.get(code)
+        submission = self.data.get(code)
         if not submission:
             raise cherrypy.NotFound()
         return template.render(submission=submission)
@@ -52,8 +47,7 @@
             try:
                 data = form.to_python(data)
                 submission = Submission(**data)
-                self.data.append(submission)
-                self._comment_lookup[comment.code] = comment
+                self.data[submission.code] = submission
                 raise cherrypy.HTTPRedirect('/')
             except Invalid, e:
                 errors = e.unpack_errors()
@@ -65,7 +59,7 @@
     @cherrypy.expose
     @template.output('comment.html')
     def comment(self, code, cancel=False, **data):
-        submission = self._submission_lookup.get(code)
+        submission = self.data.get(code)
         if not submission:
             raise cherrypy.NotFound()
         if cherrypy.request.method == 'POST':
@@ -75,7 +69,6 @@
             try:
                 data = form.to_python(data)
                 comment = submission.add_comment(**data)
-                self._comment_lookup[comment.code] = comment
                 raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
             except Invalid, e:
                 errors = e.unpack_errors()
@@ -85,30 +78,6 @@
         return template.render(submission=submission, comment=None,
                                errors=errors) | HTMLFormFiller(data=data)
 
-    @cherrypy.expose
-    @template.output('comment.html')
-    def reply(self, code, cancel=False, **data):
-        comment = self._comment_lookup.get(code)
-        submission = comment.submission
-        if not comment:
-            raise cherrypy.NotFound()
-        if cherrypy.request.method == 'POST':
-            if cancel:
-                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
-            form = CommentForm()
-            try:
-                data = form.to_python(data)
-                comment = comment.add_reply(**data)
-                self._comment_lookup[comment.code] = comment
-                raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
-            except Invalid, e:
-                errors = e.unpack_errors()
-        else:
-            errors = {}
-
-        return template.render(submission=submission, comment=comment,
-                               errors=errors) | HTMLFormFiller(data=data)
-
 
 def main(filename):
     # load data from the pickle file, or initialize it to an empty list
@@ -119,7 +88,7 @@
         finally:
             fileobj.close()
     else:
-        data = []
+        data = {}
 
     def _save_data():
         # save data back to the pickle file
--- a/examples/tutorial/geddit/model.py
+++ b/examples/tutorial/geddit/model.py
@@ -8,64 +8,22 @@
         self.url = url
         self.title = title
         self.time = datetime.utcnow()
+        self.code = hex(hash(tuple([username, url, title, self.time])))[2:]
         self.comments = []
 
     def __repr__(self):
         return '<%s %r>' % (type(self).__name__, self.title)
 
     def add_comment(self, username, content):
-        comment = Comment(username, content, in_reply_to=self)
-        self.comments.append(comment)
-        return comment
-
-    @property
-    def code(self):
-        uid = tuple([self.username, self.url, self.title, self.time])
-        return hex(hash(uid))[2:]
-
-    @property
-    def total_comments(self):
-        retval = []
-        for comment in self.comments:
-            retval.append(comment)
-            retval.extend(comment.total_replies)
-        return retval
+        self.comments.append(Comment(username, content))
 
 
 class Comment(object):
 
-    def __init__(self, username, content, in_reply_to=None):
+    def __init__(self, username, content):
         self.username = username
         self.content = content
-        self.in_reply_to = in_reply_to
         self.time = datetime.utcnow()
-        self.replies = []
 
     def __repr__(self):
         return '<%s>' % (type(self).__name__)
-
-    def add_reply(self, username, content):
-        reply = Comment(username, content, in_reply_to=self)
-        self.replies.append(reply)
-        return reply
-
-    @property
-    def code(self):
-        uid = tuple([self.in_reply_to.code, self.username, self.time])
-        return hex(hash(uid))[2:]
-
-    @property
-    def submission(self):
-        ref = self.in_reply_to
-        while ref:
-            if isinstance(ref, Submission):
-                return ref
-            ref = ref.in_reply_to
-
-    @property
-    def total_replies(self):
-        retval = []
-        for reply in self.replies:
-            retval.append(reply)
-            retval.extend(reply.total_replies)
-        return retval
--- a/examples/tutorial/geddit/static/layout.css
+++ b/examples/tutorial/geddit/static/layout.css
@@ -6,6 +6,10 @@
   font: normal xx-large/1.5 Georgia,serif; margin: 0 0 .5em;
 }
 blockquote { font-style: italic; }
+form table { margin-bottom: 1em; }
+form table tbody th { font-weight: normal; padding-top: .3em; text-align: right;
+  vertical-align: top;
+}
 
 #wrap { background: #fff; width: 600px; margin: 30px auto; }
 #content { border-left: 10px solid #b00; min-height: 240px; padding: 10px; }
@@ -19,13 +23,5 @@
 
 ol.submissions li .info { font-size: 85%; }
 ol.submissions li .info :link, ol.submissions li .info :visited { color: #666; }
-
-form table { margin-bottom: 1em; }
-form table tbody th { font-weight: normal; padding-top: .3em; text-align: right;
-  vertical-align: top;
-}
-
-ul.comments { list-style: none; margin: 1em 0; padding: 0; }
-ul.comments li { color: #999; }
-ul.comments ul { padding: 0 0 0 2em; }
-ul.comments blockquote { color: #333; font-style: normal; margin: 0; padding: 0; }
+ul.comments { list-style: none; margin: 1em 0; padding: 0 0 0 1em; }
+ul.comments li { color: #999; margin: 0 0 1em; }
--- a/examples/tutorial/geddit/templates/comments.html
+++ b/examples/tutorial/geddit/templates/comments.html
@@ -7,7 +7,5 @@
     <strong>${comment.username}</strong>
     at ${comment.time.strftime('%M/%d/%Y %H:%m')}
     <blockquote>${comment.content}</blockquote>
-    <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
@@ -14,10 +14,10 @@
       <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 />
+        at ${submission.time.strftime('%m/%d/%Y %H:%M')}<br />
         <div class="info">
           <a href="${url('/info/%s/' % submission.code)}">
-            ${len(submission.total_comments)} comments
+            ${len(submission.comments)} comments
           </a>
         </div>
       </li>
--- a/examples/tutorial/geddit/templates/info.html
+++ b/examples/tutorial/geddit/templates/info.html
@@ -10,8 +10,14 @@
     <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 />
+    at ${submission.time.strftime('%m/%d/%Y %H:%M')}<br />
     <a href="${url('/comment/%s/' % submission.code)}">comment</a>
-    <xi:include href="comments.html" py:with="comments=submission.comments" />
+    <ul py:if="submission.comments" class="comments">
+      <li py:for="comment in submission.comments">
+        <strong>${comment.username}</strong>
+        at ${comment.time.strftime('%m/%d/%Y %H:%M')}
+        <blockquote>${comment.content}</blockquote>
+      </li>
+    </ul>
   </body>
 </html>
Copyright (C) 2012-2017 Edgewall Software