# HG changeset patch # User cmlenz # Date 1188464886 0 # Node ID 5a2059ce1f0b6f2332edeebeee5f64f51ab367c7 # Parent 47c9c9ec204e11c31b37d665d3970884a3760f03 Simplify the tutorial project: comments are now flat, not hierarchical. diff --git a/examples/tutorial/geddit/controller.py b/examples/tutorial/geddit/controller.py --- 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 diff --git a/examples/tutorial/geddit/model.py b/examples/tutorial/geddit/model.py --- 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 diff --git a/examples/tutorial/geddit/static/layout.css b/examples/tutorial/geddit/static/layout.css --- 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; } diff --git a/examples/tutorial/geddit/templates/comments.html b/examples/tutorial/geddit/templates/comments.html --- a/examples/tutorial/geddit/templates/comments.html +++ b/examples/tutorial/geddit/templates/comments.html @@ -7,7 +7,5 @@ ${comment.username} at ${comment.time.strftime('%M/%d/%Y %H:%m')}
${comment.content}
- reply - diff --git a/examples/tutorial/geddit/templates/index.html b/examples/tutorial/geddit/templates/index.html --- a/examples/tutorial/geddit/templates/index.html +++ b/examples/tutorial/geddit/templates/index.html @@ -14,10 +14,10 @@
  • ${submission.title} posted by ${submission.username} - at ${submission.time.strftime('%M/%d/%Y %H:%m')}
    + at ${submission.time.strftime('%m/%d/%Y %H:%M')}
  • diff --git a/examples/tutorial/geddit/templates/info.html b/examples/tutorial/geddit/templates/info.html --- a/examples/tutorial/geddit/templates/info.html +++ b/examples/tutorial/geddit/templates/info.html @@ -10,8 +10,14 @@

    ${submission.title}

    ${submission.url}
    posted by ${submission.username} - at ${submission.time.strftime('%M/%d/%Y %H:%m')}
    + at ${submission.time.strftime('%m/%d/%Y %H:%M')}
    comment - +