comparison 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
comparison
equal deleted inserted replaced
612:09de73cae3d5 613:d8f961381f75
16 16
17 class Root(object): 17 class Root(object):
18 18
19 def __init__(self, data): 19 def __init__(self, data):
20 self.data = data 20 self.data = data
21 self.submission_lookup = {} 21 self._submission_lookup = {}
22 self.comment_lookup = {} 22 self._comment_lookup = {}
23 for submission in self.data: 23 for submission in self.data:
24 self.submission_lookup[submission.code] = submission 24 self._submission_lookup[submission.code] = submission
25 for comment in submission.comments: 25 for comment in submission.comments:
26 self.comment_lookup[comment.code] = comment 26 self._comment_lookup[comment.code] = comment
27 def _add_replies(comment): 27 def _add_replies(comment):
28 for reply in comment.replies: 28 for reply in comment.replies:
29 self.comment_lookup[reply.code] = reply 29 self._comment_lookup[reply.code] = reply
30 _add_replies(comment) 30 _add_replies(comment)
31 31
32 @cherrypy.expose 32 @cherrypy.expose
33 @template.output('index.html', method='html', doctype='html') 33 @template.output('index.html', method='html', doctype='html')
34 def index(self): 34 def index(self):
35 return template.render(submissions=self.data) 35 return template.render(submissions=self.data)
36 36
37 @cherrypy.expose 37 @cherrypy.expose
38 @template.output('info.html', method='html', doctype='html') 38 @template.output('info.html', method='html', doctype='html')
39 def info(self, code): 39 def info(self, code):
40 submission = self.submission_lookup.get(code) 40 submission = self._submission_lookup.get(code)
41 if not submission: 41 if not submission:
42 raise cherrypy.NotFound() 42 raise cherrypy.NotFound()
43 return template.render(submission=submission) 43 return template.render(submission=submission)
44 44
45 @cherrypy.expose 45 @cherrypy.expose
51 form = SubmissionForm() 51 form = SubmissionForm()
52 try: 52 try:
53 data = form.to_python(data) 53 data = form.to_python(data)
54 submission = Submission(**data) 54 submission = Submission(**data)
55 self.data.append(submission) 55 self.data.append(submission)
56 self._comment_lookup[comment.code] = comment
56 raise cherrypy.HTTPRedirect('/') 57 raise cherrypy.HTTPRedirect('/')
57 except Invalid, e: 58 except Invalid, e:
58 errors = e.unpack_errors() 59 errors = e.unpack_errors()
59 else: 60 else:
60 errors = {} 61 errors = {}
62 return template.render(errors=errors) | HTMLFormFiller(data=data) 63 return template.render(errors=errors) | HTMLFormFiller(data=data)
63 64
64 @cherrypy.expose 65 @cherrypy.expose
65 @template.output('comment.html', method='html', doctype='html') 66 @template.output('comment.html', method='html', doctype='html')
66 def comment(self, code, cancel=False, **data): 67 def comment(self, code, cancel=False, **data):
67 submission = self.submission_lookup.get(code) 68 submission = self._submission_lookup.get(code)
68 if not submission: 69 if not submission:
69 raise cherrypy.NotFound() 70 raise cherrypy.NotFound()
70 if cherrypy.request.method == 'POST': 71 if cherrypy.request.method == 'POST':
71 if cancel: 72 if cancel:
72 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) 73 raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
73 form = CommentForm() 74 form = CommentForm()
74 try: 75 try:
75 data = form.to_python(data) 76 data = form.to_python(data)
76 comment = submission.add_comment(**data) 77 comment = submission.add_comment(**data)
77 self.comment_lookup[comment.code] = comment 78 self._comment_lookup[comment.code] = comment
78 raise cherrypy.HTTPRedirect('/') 79 raise cherrypy.HTTPRedirect('/')
79 except Invalid, e: 80 except Invalid, e:
80 errors = e.unpack_errors() 81 errors = e.unpack_errors()
81 else: 82 else:
82 errors = {} 83 errors = {}
85 errors=errors) 86 errors=errors)
86 87
87 @cherrypy.expose 88 @cherrypy.expose
88 @template.output('comment.html', method='html', doctype='html') 89 @template.output('comment.html', method='html', doctype='html')
89 def reply(self, code, cancel=False, **data): 90 def reply(self, code, cancel=False, **data):
90 comment = self.comment_lookup.get(code) 91 comment = self._comment_lookup.get(code)
91 submission = comment.submission 92 submission = comment.submission
92 if not comment: 93 if not comment:
93 raise cherrypy.NotFound() 94 raise cherrypy.NotFound()
94 if cherrypy.request.method == 'POST': 95 if cherrypy.request.method == 'POST':
95 if cancel: 96 if cancel:
96 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) 97 raise cherrypy.HTTPRedirect('/info/%s' % submission.code)
97 form = CommentForm() 98 form = CommentForm()
98 try: 99 try:
99 data = form.to_python(data) 100 data = form.to_python(data)
100 comment = comment.add_reply(**data) 101 comment = comment.add_reply(**data)
101 self.comment_lookup[comment.code] = comment 102 self._comment_lookup[comment.code] = comment
102 raise cherrypy.HTTPRedirect('/') 103 raise cherrypy.HTTPRedirect('/')
103 except Invalid, e: 104 except Invalid, e:
104 errors = e.unpack_errors() 105 errors = e.unpack_errors()
105 else: 106 else:
106 errors = {} 107 errors = {}
Copyright (C) 2012-2017 Edgewall Software