Mercurial > genshi > genshi-test
annotate examples/tutorial/geddit/controller.py @ 690:cea2e0d9ba28 experimental-match-fastpaths
more code/comment clean up - make sure to retain match order
author | aflett |
---|---|
date | Sat, 15 Mar 2008 05:42:29 +0000 |
parents | 19e8a05adc86 |
children |
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 |
628
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
3 import operator, os, pickle, sys |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
4 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
5 import cherrypy |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
6 from formencode import Invalid |
631 | 7 from genshi.input import HTML |
8 from genshi.filters import HTMLFormFiller, HTMLSanitizer | |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
9 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
10 from geddit.form import LinkForm, CommentForm |
625 | 11 from geddit.lib import ajax, template |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
12 from geddit.model import Link, Comment |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
13 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
14 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
15 class Root(object): |
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 def __init__(self, data): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
18 self.data = data |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
19 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
20 @cherrypy.expose |
615
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
21 @template.output('index.html') |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
22 def index(self): |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
23 links = sorted(self.data.values(), key=operator.attrgetter('time')) |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
24 return template.render(links=links) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
25 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
26 @cherrypy.expose |
615
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
27 @template.output('submit.html') |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
28 def submit(self, cancel=False, **data): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
29 if cherrypy.request.method == 'POST': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
30 if cancel: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
31 raise cherrypy.HTTPRedirect('/') |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
32 form = LinkForm() |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
33 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
34 data = form.to_python(data) |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
35 link = Link(**data) |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
36 self.data[link.id] = link |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
37 raise cherrypy.HTTPRedirect('/') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
38 except Invalid, e: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
39 errors = e.unpack_errors() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
40 else: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
41 errors = {} |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
42 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
43 return template.render(errors=errors) | HTMLFormFiller(data=data) |
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 |
627
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
46 @template.output('info.html') |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
47 def info(self, id): |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
48 link = self.data.get(id) |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
49 if not link: |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
50 raise cherrypy.NotFound() |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
51 return template.render(link=link) |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
52 |
2f4dc32a13e7
GenshiTutorial: minor tweaks to sync with Wiki page.
cmlenz
parents:
625
diff
changeset
|
53 @cherrypy.expose |
615
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
54 @template.output('comment.html') |
622 | 55 def comment(self, id, cancel=False, **data): |
56 link = self.data.get(id) | |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
57 if not link: |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
58 raise cherrypy.NotFound() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
59 if cherrypy.request.method == 'POST': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
60 if cancel: |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
61 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
62 form = CommentForm() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
63 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
64 data = form.to_python(data) |
631 | 65 markup = HTML(data['content']) | HTMLSanitizer() |
66 data['content'] = markup.render('xhtml') | |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
67 comment = link.add_comment(**data) |
625 | 68 if not ajax.is_xhr(): |
69 raise cherrypy.HTTPRedirect('/info/%s' % link.id) | |
70 return template.render('_comment.html', comment=comment, | |
71 num=len(link.comments)) | |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
72 except Invalid, e: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
73 errors = e.unpack_errors() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
74 else: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
75 errors = {} |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
76 |
625 | 77 if ajax.is_xhr(): |
78 stream = template.render('_form.html', link=link, errors=errors) | |
79 else: | |
80 stream = template.render(link=link, comment=None, errors=errors) | |
81 return stream | HTMLFormFiller(data=data) | |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
82 |
628
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
83 @cherrypy.expose |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
84 @template.output('index.xml', method='xml') |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
85 def feed(self, id=None): |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
86 if id: |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
87 link = self.data.get(id) |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
88 if not link: |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
89 raise cherrypy.NotFound() |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
90 return template.render('info.xml', link=link) |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
91 else: |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
92 links = sorted(self.data.values(), key=operator.attrgetter('time')) |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
93 return template.render(links=links) |
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
94 |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
95 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
96 def main(filename): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
97 # 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
|
98 if os.path.exists(filename): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
99 fileobj = open(filename, 'rb') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
100 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
101 data = pickle.load(fileobj) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
102 finally: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
103 fileobj.close() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
104 else: |
618
5a2059ce1f0b
Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents:
616
diff
changeset
|
105 data = {} |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
106 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
107 def _save_data(): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
108 # save data back to the pickle file |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
109 fileobj = open(filename, 'wb') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
110 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
111 pickle.dump(data, fileobj) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
112 finally: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
113 fileobj.close() |
664
19e8a05adc86
Fix compatibility of tutorial code with CherryPy 3.1. Closes #159.
cmlenz
parents:
632
diff
changeset
|
114 if hasattr(cherrypy.engine, 'subscribe'): # CherryPy >= 3.1 |
19e8a05adc86
Fix compatibility of tutorial code with CherryPy 3.1. Closes #159.
cmlenz
parents:
632
diff
changeset
|
115 cherrypy.engine.subscribe('stop', _save_data) |
19e8a05adc86
Fix compatibility of tutorial code with CherryPy 3.1. Closes #159.
cmlenz
parents:
632
diff
changeset
|
116 else: |
19e8a05adc86
Fix compatibility of tutorial code with CherryPy 3.1. Closes #159.
cmlenz
parents:
632
diff
changeset
|
117 cherrypy.engine.on_stop_engine_list.append(_save_data) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
118 |
628
693a7212b348
GenshiTutorial: tweaks to sync with code on wiki page.
cmlenz
parents:
627
diff
changeset
|
119 # Some global configuration; note that this could be moved into a |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
120 # configuration file |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
121 cherrypy.config.update({ |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
122 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
123 'tools.decode.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
124 'tools.trailing_slash.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
125 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
126 }) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
127 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
128 cherrypy.quickstart(Root(data), '/', { |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
129 '/media': { |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
130 'tools.staticdir.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
131 'tools.staticdir.dir': 'static' |
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 }) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
134 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
135 if __name__ == '__main__': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
136 import formencode |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
137 formencode.api.set_stdtranslation(languages=['en']) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
138 main(sys.argv[1]) |