Mercurial > genshi > genshi-test
comparison examples/tutorial/geddit/controller.py @ 611:16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
author | cmlenz |
---|---|
date | Wed, 29 Aug 2007 17:51:45 +0000 |
parents | |
children | d8f961381f75 |
comparison
equal
deleted
inserted
replaced
610:6a37018199fd | 611:16b1be35c265 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import pickle | |
5 import sys | |
6 | |
7 import cherrypy | |
8 from formencode import Invalid | |
9 from genshi.filters import HTMLFormFiller | |
10 from paste.evalexception.middleware import EvalException | |
11 | |
12 from geddit.form import SubmissionForm, CommentForm | |
13 from geddit.lib import template | |
14 from geddit.model import Submission, Comment | |
15 | |
16 | |
17 class Root(object): | |
18 | |
19 def __init__(self, data): | |
20 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 | |
32 @cherrypy.expose | |
33 @template.output('index.html', method='html', doctype='html') | |
34 def index(self): | |
35 return template.render(submissions=self.data) | |
36 | |
37 @cherrypy.expose | |
38 @template.output('info.html', method='html', doctype='html') | |
39 def info(self, code): | |
40 submission = self.submission_lookup.get(code) | |
41 if not submission: | |
42 raise cherrypy.NotFound() | |
43 return template.render(submission=submission) | |
44 | |
45 @cherrypy.expose | |
46 @template.output('submit.html', method='html', doctype='html') | |
47 def submit(self, cancel=False, **data): | |
48 if cherrypy.request.method == 'POST': | |
49 if cancel: | |
50 raise cherrypy.HTTPRedirect('/') | |
51 form = SubmissionForm() | |
52 try: | |
53 data = form.to_python(data) | |
54 submission = Submission(**data) | |
55 self.data.append(submission) | |
56 raise cherrypy.HTTPRedirect('/') | |
57 except Invalid, e: | |
58 errors = e.unpack_errors() | |
59 else: | |
60 errors = {} | |
61 | |
62 return template.render(errors=errors) | HTMLFormFiller(data=data) | |
63 | |
64 @cherrypy.expose | |
65 @template.output('comment.html', method='html', doctype='html') | |
66 def comment(self, code, cancel=False, **data): | |
67 submission = self.submission_lookup.get(code) | |
68 if not submission: | |
69 raise cherrypy.NotFound() | |
70 if cherrypy.request.method == 'POST': | |
71 if cancel: | |
72 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | |
73 form = CommentForm() | |
74 try: | |
75 data = form.to_python(data) | |
76 comment = submission.add_comment(**data) | |
77 self.comment_lookup[comment.code] = comment | |
78 raise cherrypy.HTTPRedirect('/') | |
79 except Invalid, e: | |
80 errors = e.unpack_errors() | |
81 else: | |
82 errors = {} | |
83 | |
84 return template.render(submission=submission, comment=None, | |
85 errors=errors) | |
86 | |
87 @cherrypy.expose | |
88 @template.output('comment.html', method='html', doctype='html') | |
89 def reply(self, code, cancel=False, **data): | |
90 comment = self.comment_lookup.get(code) | |
91 submission = comment.submission | |
92 if not comment: | |
93 raise cherrypy.NotFound() | |
94 if cherrypy.request.method == 'POST': | |
95 if cancel: | |
96 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | |
97 form = CommentForm() | |
98 try: | |
99 data = form.to_python(data) | |
100 comment = comment.add_reply(**data) | |
101 self.comment_lookup[comment.code] = comment | |
102 raise cherrypy.HTTPRedirect('/') | |
103 except Invalid, e: | |
104 errors = e.unpack_errors() | |
105 else: | |
106 errors = {} | |
107 | |
108 return template.render(submission=submission, comment=comment, | |
109 errors=errors) | |
110 | |
111 | |
112 def main(filename): | |
113 # load data from the pickle file, or initialize it to an empty list | |
114 if os.path.exists(filename): | |
115 fileobj = open(filename, 'rb') | |
116 try: | |
117 data = pickle.load(fileobj) | |
118 finally: | |
119 fileobj.close() | |
120 else: | |
121 data = [] | |
122 | |
123 def _save_data(): | |
124 # save data back to the pickle file | |
125 fileobj = open(filename, 'wb') | |
126 try: | |
127 pickle.dump(data, fileobj) | |
128 finally: | |
129 fileobj.close() | |
130 cherrypy.engine.on_stop_engine_list.append(_save_data) | |
131 | |
132 # Some global configuration; note that this could be moved into a configuration file | |
133 cherrypy.config.update({ | |
134 'request.throw_errors': True, | |
135 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', | |
136 'tools.decode.on': True, | |
137 'tools.trailing_slash.on': True, | |
138 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), | |
139 }) | |
140 | |
141 # Initialize the application, and add EvalException for more helpful error messages | |
142 app = cherrypy.Application(Root(data)) | |
143 app.wsgiapp.pipeline.append(('paste_exc', EvalException)) | |
144 cherrypy.quickstart(app, '/', { | |
145 '/media': { | |
146 'tools.staticdir.on': True, | |
147 'tools.staticdir.dir': 'static' | |
148 } | |
149 }) | |
150 | |
151 if __name__ == '__main__': | |
152 import formencode | |
153 formencode.api.set_stdtranslation(languages=['en']) | |
154 main(sys.argv[1]) |