annotate examples/tutorial/geddit/controller.py @ 613:d8f961381f75

GenshiTutorial: fix storing of submissions.
author cmlenz
date Wed, 29 Aug 2007 19:34:39 +0000
parents 16b1be35c265
children 0dc152d128f5
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
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
21 self._submission_lookup = {}
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
22 self._comment_lookup = {}
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
23 for submission in self.data:
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
24 self._submission_lookup[submission.code] = submission
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
25 for comment in submission.comments:
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
26 self._comment_lookup[comment.code] = comment
611
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:
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
29 self._comment_lookup[reply.code] = reply
611
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):
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
40 submission = self._submission_lookup.get(code)
611
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)
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
56 self._comment_lookup[comment.code] = comment
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
57 raise cherrypy.HTTPRedirect('/')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
58 except Invalid, e:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
59 errors = e.unpack_errors()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
60 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
61 errors = {}
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
62
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
63 return template.render(errors=errors) | HTMLFormFiller(data=data)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
64
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
65 @cherrypy.expose
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
66 @template.output('comment.html', method='html', doctype='html')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
67 def comment(self, code, cancel=False, **data):
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
68 submission = self._submission_lookup.get(code)
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
69 if not submission:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
70 raise cherrypy.NotFound()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
71 if cherrypy.request.method == 'POST':
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
72 if cancel:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
73 raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
74 form = CommentForm()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
75 try:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
76 data = form.to_python(data)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
77 comment = submission.add_comment(**data)
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
78 self._comment_lookup[comment.code] = comment
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
79 raise cherrypy.HTTPRedirect('/')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
80 except Invalid, e:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
81 errors = e.unpack_errors()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
82 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
83 errors = {}
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
84
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
85 return template.render(submission=submission, comment=None,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
86 errors=errors)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
87
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
88 @cherrypy.expose
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
89 @template.output('comment.html', method='html', doctype='html')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
90 def reply(self, code, cancel=False, **data):
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
91 comment = self._comment_lookup.get(code)
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
92 submission = comment.submission
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
93 if not comment:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
94 raise cherrypy.NotFound()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
95 if cherrypy.request.method == 'POST':
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
96 if cancel:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
97 raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
98 form = CommentForm()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
99 try:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
100 data = form.to_python(data)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
101 comment = comment.add_reply(**data)
613
d8f961381f75 GenshiTutorial: fix storing of submissions.
cmlenz
parents: 611
diff changeset
102 self._comment_lookup[comment.code] = comment
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
103 raise cherrypy.HTTPRedirect('/')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
104 except Invalid, e:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
105 errors = e.unpack_errors()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
106 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
107 errors = {}
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
108
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
109 return template.render(submission=submission, comment=comment,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
110 errors=errors)
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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
113 def main(filename):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
114 # 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
115 if os.path.exists(filename):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
116 fileobj = open(filename, 'rb')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
117 try:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
118 data = pickle.load(fileobj)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
119 finally:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
120 fileobj.close()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
121 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
122 data = []
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
123
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
124 def _save_data():
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
125 # save data back to the pickle file
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
126 fileobj = open(filename, 'wb')
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
127 try:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
128 pickle.dump(data, fileobj)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
129 finally:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
130 fileobj.close()
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
131 cherrypy.engine.on_stop_engine_list.append(_save_data)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
132
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
133 # 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
134 cherrypy.config.update({
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
135 'request.throw_errors': True,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
136 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
137 'tools.decode.on': True,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
138 'tools.trailing_slash.on': True,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
139 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
142 # 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
143 app = cherrypy.Application(Root(data))
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
144 app.wsgiapp.pipeline.append(('paste_exc', EvalException))
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
145 cherrypy.quickstart(app, '/', {
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
146 '/media': {
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
147 'tools.staticdir.on': True,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
148 'tools.staticdir.dir': 'static'
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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
152 if __name__ == '__main__':
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
153 import formencode
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
154 formencode.api.set_stdtranslation(languages=['en'])
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
155 main(sys.argv[1])
Copyright (C) 2012-2017 Edgewall Software