Mercurial > genshi > genshi-test
comparison 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 |
comparison
equal
deleted
inserted
replaced
617:47c9c9ec204e | 618:5a2059ce1f0b |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import operator | |
3 import os | 4 import os |
4 import pickle | 5 import pickle |
5 import sys | 6 import sys |
6 | 7 |
7 import cherrypy | 8 import cherrypy |
16 | 17 |
17 class Root(object): | 18 class Root(object): |
18 | 19 |
19 def __init__(self, data): | 20 def __init__(self, data): |
20 self.data = data | 21 self.data = data |
21 self._submission_lookup = {} | |
22 self._comment_lookup = {} | |
23 for submission in self.data: | |
24 self._submission_lookup[submission.code] = submission | |
25 for comment in submission.comments: | |
26 self._comment_lookup[comment.code] = comment | |
27 def _add_replies(comment): | |
28 for reply in comment.replies: | |
29 self._comment_lookup[reply.code] = reply | |
30 _add_replies(comment) | |
31 | 22 |
32 @cherrypy.expose | 23 @cherrypy.expose |
33 @template.output('index.html') | 24 @template.output('index.html') |
34 def index(self): | 25 def index(self): |
35 return template.render(submissions=self.data) | 26 return template.render( |
27 submissions=sorted(self.data.values(), | |
28 key=operator.attrgetter('time'), | |
29 reverse=True) | |
30 ) | |
36 | 31 |
37 @cherrypy.expose | 32 @cherrypy.expose |
38 @template.output('info.html') | 33 @template.output('info.html') |
39 def info(self, code): | 34 def info(self, code): |
40 submission = self._submission_lookup.get(code) | 35 submission = self.data.get(code) |
41 if not submission: | 36 if not submission: |
42 raise cherrypy.NotFound() | 37 raise cherrypy.NotFound() |
43 return template.render(submission=submission) | 38 return template.render(submission=submission) |
44 | 39 |
45 @cherrypy.expose | 40 @cherrypy.expose |
50 raise cherrypy.HTTPRedirect('/') | 45 raise cherrypy.HTTPRedirect('/') |
51 form = SubmissionForm() | 46 form = SubmissionForm() |
52 try: | 47 try: |
53 data = form.to_python(data) | 48 data = form.to_python(data) |
54 submission = Submission(**data) | 49 submission = Submission(**data) |
55 self.data.append(submission) | 50 self.data[submission.code] = submission |
56 self._comment_lookup[comment.code] = comment | |
57 raise cherrypy.HTTPRedirect('/') | 51 raise cherrypy.HTTPRedirect('/') |
58 except Invalid, e: | 52 except Invalid, e: |
59 errors = e.unpack_errors() | 53 errors = e.unpack_errors() |
60 else: | 54 else: |
61 errors = {} | 55 errors = {} |
63 return template.render(errors=errors) | HTMLFormFiller(data=data) | 57 return template.render(errors=errors) | HTMLFormFiller(data=data) |
64 | 58 |
65 @cherrypy.expose | 59 @cherrypy.expose |
66 @template.output('comment.html') | 60 @template.output('comment.html') |
67 def comment(self, code, cancel=False, **data): | 61 def comment(self, code, cancel=False, **data): |
68 submission = self._submission_lookup.get(code) | 62 submission = self.data.get(code) |
69 if not submission: | 63 if not submission: |
70 raise cherrypy.NotFound() | 64 raise cherrypy.NotFound() |
71 if cherrypy.request.method == 'POST': | 65 if cherrypy.request.method == 'POST': |
72 if cancel: | 66 if cancel: |
73 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | 67 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) |
74 form = CommentForm() | 68 form = CommentForm() |
75 try: | 69 try: |
76 data = form.to_python(data) | 70 data = form.to_python(data) |
77 comment = submission.add_comment(**data) | 71 comment = submission.add_comment(**data) |
78 self._comment_lookup[comment.code] = comment | |
79 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | 72 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) |
80 except Invalid, e: | 73 except Invalid, e: |
81 errors = e.unpack_errors() | 74 errors = e.unpack_errors() |
82 else: | 75 else: |
83 errors = {} | 76 errors = {} |
84 | 77 |
85 return template.render(submission=submission, comment=None, | 78 return template.render(submission=submission, comment=None, |
86 errors=errors) | HTMLFormFiller(data=data) | |
87 | |
88 @cherrypy.expose | |
89 @template.output('comment.html') | |
90 def reply(self, code, cancel=False, **data): | |
91 comment = self._comment_lookup.get(code) | |
92 submission = comment.submission | |
93 if not comment: | |
94 raise cherrypy.NotFound() | |
95 if cherrypy.request.method == 'POST': | |
96 if cancel: | |
97 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | |
98 form = CommentForm() | |
99 try: | |
100 data = form.to_python(data) | |
101 comment = comment.add_reply(**data) | |
102 self._comment_lookup[comment.code] = comment | |
103 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | |
104 except Invalid, e: | |
105 errors = e.unpack_errors() | |
106 else: | |
107 errors = {} | |
108 | |
109 return template.render(submission=submission, comment=comment, | |
110 errors=errors) | HTMLFormFiller(data=data) | 79 errors=errors) | HTMLFormFiller(data=data) |
111 | 80 |
112 | 81 |
113 def main(filename): | 82 def main(filename): |
114 # load data from the pickle file, or initialize it to an empty list | 83 # load data from the pickle file, or initialize it to an empty list |
117 try: | 86 try: |
118 data = pickle.load(fileobj) | 87 data = pickle.load(fileobj) |
119 finally: | 88 finally: |
120 fileobj.close() | 89 fileobj.close() |
121 else: | 90 else: |
122 data = [] | 91 data = {} |
123 | 92 |
124 def _save_data(): | 93 def _save_data(): |
125 # save data back to the pickle file | 94 # save data back to the pickle file |
126 fileobj = open(filename, 'wb') | 95 fileobj = open(filename, 'wb') |
127 try: | 96 try: |