Mercurial > genshi > genshi-test
diff examples/tutorial/geddit/controller.py @ 618:5a2059ce1f0b
Simplify the tutorial project: comments are now flat, not hierarchical.
author | cmlenz |
---|---|
date | Thu, 30 Aug 2007 09:08:06 +0000 |
parents | 80a9c247ca80 |
children | 71d3edd302ae |
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