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