Mercurial > genshi > genshi-test
annotate examples/tutorial/geddit/controller.py @ 622:dc35e9882390
GenshiTutorial: add Atom feeds.
author | cmlenz |
---|---|
date | Thu, 30 Aug 2007 22:49:48 +0000 |
parents | 71d3edd302ae |
children | dba522b4c31d |
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 |
618
5a2059ce1f0b
Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents:
616
diff
changeset
|
3 import operator |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
4 import os |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
5 import pickle |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
6 import sys |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
7 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
8 import cherrypy |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
9 from formencode import Invalid |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
10 from genshi.filters import HTMLFormFiller |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
11 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
12 from geddit.form import LinkForm, CommentForm |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
13 from geddit.lib import template |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
14 from geddit.model import Link, Comment |
611
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 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
22 @cherrypy.expose |
622 | 23 @template.output('index.xml', method='xml') |
24 def feed(self, id=None): | |
25 if id: | |
26 link = self.data.get(id) | |
27 if not link: | |
28 raise cherrypy.NotFound() | |
29 return template.render('info.xml', link=link) | |
30 else: | |
31 links = sorted(self.data.values(), key=operator.attrgetter('time')) | |
32 return template.render(links=links) | |
33 | |
34 @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
|
35 @template.output('index.html') |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
36 def index(self): |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
37 links = sorted(self.data.values(), key=operator.attrgetter('time')) |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
38 return template.render(links=links) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
39 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
40 @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
|
41 @template.output('info.html') |
622 | 42 def info(self, id): |
43 link = self.data.get(id) | |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
44 if not link: |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
45 raise cherrypy.NotFound() |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
46 return template.render(link=link) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
47 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
48 @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
|
49 @template.output('submit.html') |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
50 def submit(self, cancel=False, **data): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
51 if cherrypy.request.method == 'POST': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
52 if cancel: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
53 raise cherrypy.HTTPRedirect('/') |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
54 form = LinkForm() |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
55 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
56 data = form.to_python(data) |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
57 link = Link(**data) |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
58 self.data[link.id] = link |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
59 raise cherrypy.HTTPRedirect('/') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
60 except Invalid, e: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
61 errors = e.unpack_errors() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
62 else: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
63 errors = {} |
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 return template.render(errors=errors) | HTMLFormFiller(data=data) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
66 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
67 @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
|
68 @template.output('comment.html') |
622 | 69 def comment(self, id, cancel=False, **data): |
70 link = self.data.get(id) | |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
71 if not link: |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
72 raise cherrypy.NotFound() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
73 if cherrypy.request.method == 'POST': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
74 if cancel: |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
75 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
76 form = CommentForm() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
77 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
78 data = form.to_python(data) |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
79 comment = link.add_comment(**data) |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
80 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
81 except Invalid, e: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
82 errors = e.unpack_errors() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
83 else: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
84 errors = {} |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
85 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
86 return template.render(link=link, comment=None, |
616
80a9c247ca80
GenshiTutorial: use form filler on comment/reply views.
cmlenz
parents:
615
diff
changeset
|
87 errors=errors) | HTMLFormFiller(data=data) |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
88 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
89 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
90 def main(filename): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
91 # 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
|
92 if os.path.exists(filename): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
93 fileobj = open(filename, 'rb') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
94 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
95 data = pickle.load(fileobj) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
96 finally: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
97 fileobj.close() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
98 else: |
618
5a2059ce1f0b
Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents:
616
diff
changeset
|
99 data = {} |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
100 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
101 def _save_data(): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
102 # save data back to the pickle file |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
103 fileobj = open(filename, 'wb') |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
104 try: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
105 pickle.dump(data, fileobj) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
106 finally: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
107 fileobj.close() |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
108 cherrypy.engine.on_stop_engine_list.append(_save_data) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
109 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
110 # Some global configuration; note that this could be moved into a |
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
111 # configuration file |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
112 cherrypy.config.update({ |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
113 'request.throw_errors': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
114 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
115 'tools.decode.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
116 'tools.trailing_slash.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
117 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
118 }) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
119 |
619
71d3edd302ae
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
120 cherrypy.quickstart(Root(data), '/', { |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
121 '/media': { |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
122 'tools.staticdir.on': True, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
123 'tools.staticdir.dir': 'static' |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
124 } |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
125 }) |
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 if __name__ == '__main__': |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
128 import formencode |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
129 formencode.api.set_stdtranslation(languages=['en']) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
130 main(sys.argv[1]) |