Mercurial > genshi > mirror
comparison examples/tutorial/geddit/controller.py @ 619:756e7418e10c trunk
GenshiTutorial: various updates to sync with wiki page.
author | cmlenz |
---|---|
date | Thu, 30 Aug 2007 14:19:59 +0000 |
parents | b6706f9346ac |
children | 6780f1b1b20d |
comparison
equal
deleted
inserted
replaced
618:b6706f9346ac | 619:756e7418e10c |
---|---|
6 import sys | 6 import sys |
7 | 7 |
8 import cherrypy | 8 import cherrypy |
9 from formencode import Invalid | 9 from formencode import Invalid |
10 from genshi.filters import HTMLFormFiller | 10 from genshi.filters import HTMLFormFiller |
11 from paste.evalexception.middleware import EvalException | |
12 | 11 |
13 from geddit.form import SubmissionForm, CommentForm | 12 from geddit.form import LinkForm, CommentForm |
14 from geddit.lib import template | 13 from geddit.lib import template |
15 from geddit.model import Submission, Comment | 14 from geddit.model import Link, Comment |
16 | 15 |
17 | 16 |
18 class Root(object): | 17 class Root(object): |
19 | 18 |
20 def __init__(self, data): | 19 def __init__(self, data): |
21 self.data = data | 20 self.data = data |
22 | 21 |
23 @cherrypy.expose | 22 @cherrypy.expose |
24 @template.output('index.html') | 23 @template.output('index.html') |
25 def index(self): | 24 def index(self): |
26 return template.render( | 25 links = sorted(self.data.values(), key=operator.attrgetter('time')) |
27 submissions=sorted(self.data.values(), | 26 return template.render(links=links) |
28 key=operator.attrgetter('time'), | |
29 reverse=True) | |
30 ) | |
31 | 27 |
32 @cherrypy.expose | 28 @cherrypy.expose |
33 @template.output('info.html') | 29 @template.output('info.html') |
34 def info(self, code): | 30 def info(self, code): |
35 submission = self.data.get(code) | 31 link = self.data.get(code) |
36 if not submission: | 32 if not link: |
37 raise cherrypy.NotFound() | 33 raise cherrypy.NotFound() |
38 return template.render(submission=submission) | 34 return template.render(link=link) |
39 | 35 |
40 @cherrypy.expose | 36 @cherrypy.expose |
41 @template.output('submit.html') | 37 @template.output('submit.html') |
42 def submit(self, cancel=False, **data): | 38 def submit(self, cancel=False, **data): |
43 if cherrypy.request.method == 'POST': | 39 if cherrypy.request.method == 'POST': |
44 if cancel: | 40 if cancel: |
45 raise cherrypy.HTTPRedirect('/') | 41 raise cherrypy.HTTPRedirect('/') |
46 form = SubmissionForm() | 42 form = LinkForm() |
47 try: | 43 try: |
48 data = form.to_python(data) | 44 data = form.to_python(data) |
49 submission = Submission(**data) | 45 link = Link(**data) |
50 self.data[submission.code] = submission | 46 self.data[link.id] = link |
51 raise cherrypy.HTTPRedirect('/') | 47 raise cherrypy.HTTPRedirect('/') |
52 except Invalid, e: | 48 except Invalid, e: |
53 errors = e.unpack_errors() | 49 errors = e.unpack_errors() |
54 else: | 50 else: |
55 errors = {} | 51 errors = {} |
57 return template.render(errors=errors) | HTMLFormFiller(data=data) | 53 return template.render(errors=errors) | HTMLFormFiller(data=data) |
58 | 54 |
59 @cherrypy.expose | 55 @cherrypy.expose |
60 @template.output('comment.html') | 56 @template.output('comment.html') |
61 def comment(self, code, cancel=False, **data): | 57 def comment(self, code, cancel=False, **data): |
62 submission = self.data.get(code) | 58 link = self.data.get(code) |
63 if not submission: | 59 if not link: |
64 raise cherrypy.NotFound() | 60 raise cherrypy.NotFound() |
65 if cherrypy.request.method == 'POST': | 61 if cherrypy.request.method == 'POST': |
66 if cancel: | 62 if cancel: |
67 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | 63 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
68 form = CommentForm() | 64 form = CommentForm() |
69 try: | 65 try: |
70 data = form.to_python(data) | 66 data = form.to_python(data) |
71 comment = submission.add_comment(**data) | 67 comment = link.add_comment(**data) |
72 raise cherrypy.HTTPRedirect('/info/%s' % submission.code) | 68 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
73 except Invalid, e: | 69 except Invalid, e: |
74 errors = e.unpack_errors() | 70 errors = e.unpack_errors() |
75 else: | 71 else: |
76 errors = {} | 72 errors = {} |
77 | 73 |
78 return template.render(submission=submission, comment=None, | 74 return template.render(link=link, comment=None, |
79 errors=errors) | HTMLFormFiller(data=data) | 75 errors=errors) | HTMLFormFiller(data=data) |
80 | 76 |
81 | 77 |
82 def main(filename): | 78 def main(filename): |
83 # load data from the pickle file, or initialize it to an empty list | 79 # load data from the pickle file, or initialize it to an empty list |
97 pickle.dump(data, fileobj) | 93 pickle.dump(data, fileobj) |
98 finally: | 94 finally: |
99 fileobj.close() | 95 fileobj.close() |
100 cherrypy.engine.on_stop_engine_list.append(_save_data) | 96 cherrypy.engine.on_stop_engine_list.append(_save_data) |
101 | 97 |
102 # Some global configuration; note that this could be moved into a configuration file | 98 # Some global configuration; note that this could be moved into a |
99 # configuration file | |
103 cherrypy.config.update({ | 100 cherrypy.config.update({ |
104 'request.throw_errors': True, | 101 'request.throw_errors': True, |
105 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', | 102 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', |
106 'tools.decode.on': True, | 103 'tools.decode.on': True, |
107 'tools.trailing_slash.on': True, | 104 'tools.trailing_slash.on': True, |
108 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), | 105 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), |
109 }) | 106 }) |
110 | 107 |
111 # Initialize the application, and add EvalException for more helpful error messages | 108 cherrypy.quickstart(Root(data), '/', { |
112 app = cherrypy.Application(Root(data)) | |
113 app.wsgiapp.pipeline.append(('paste_exc', EvalException)) | |
114 cherrypy.quickstart(app, '/', { | |
115 '/media': { | 109 '/media': { |
116 'tools.staticdir.on': True, | 110 'tools.staticdir.on': True, |
117 'tools.staticdir.dir': 'static' | 111 'tools.staticdir.dir': 'static' |
118 } | 112 } |
119 }) | 113 }) |