Mercurial > genshi > mirror
annotate examples/tutorial/geddit/controller.py @ 620:725488d315f3 trunk
GenshiTutorial: remove obsolete template.
author | cmlenz |
---|---|
date | Thu, 30 Aug 2007 17:12:45 +0000 |
parents | 756e7418e10c |
children | 6780f1b1b20d |
rev | line source |
---|---|
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
1 #!/usr/bin/env python |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
2 |
618
b6706f9346ac
Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents:
616
diff
changeset
|
3 import operator |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
4 import os |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
5 import pickle |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
6 import sys |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
7 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
8 import cherrypy |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
9 from formencode import Invalid |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
10 from genshi.filters import HTMLFormFiller |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
11 |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
12 from geddit.form import LinkForm, CommentForm |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
13 from geddit.lib import template |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
14 from geddit.model import Link, Comment |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
15 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
16 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
17 class Root(object): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
18 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
19 def __init__(self, data): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
20 self.data = data |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
21 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
22 @cherrypy.expose |
615
06165fee45ab
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
23 @template.output('index.html') |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
24 def index(self): |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
25 links = sorted(self.data.values(), key=operator.attrgetter('time')) |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
26 return template.render(links=links) |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
27 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
28 @cherrypy.expose |
615
06165fee45ab
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
29 @template.output('info.html') |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
30 def info(self, code): |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
31 link = self.data.get(code) |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
32 if not link: |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
33 raise cherrypy.NotFound() |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
34 return template.render(link=link) |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
35 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
36 @cherrypy.expose |
615
06165fee45ab
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
37 @template.output('submit.html') |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
38 def submit(self, cancel=False, **data): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
39 if cherrypy.request.method == 'POST': |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
40 if cancel: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
41 raise cherrypy.HTTPRedirect('/') |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
42 form = LinkForm() |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
43 try: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
44 data = form.to_python(data) |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
45 link = Link(**data) |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
46 self.data[link.id] = link |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
47 raise cherrypy.HTTPRedirect('/') |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
48 except Invalid, e: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
49 errors = e.unpack_errors() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
50 else: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
51 errors = {} |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
52 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
53 return template.render(errors=errors) | HTMLFormFiller(data=data) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
54 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
55 @cherrypy.expose |
615
06165fee45ab
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
613
diff
changeset
|
56 @template.output('comment.html') |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
57 def comment(self, code, cancel=False, **data): |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
58 link = self.data.get(code) |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
59 if not link: |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
60 raise cherrypy.NotFound() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
61 if cherrypy.request.method == 'POST': |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
62 if cancel: |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
63 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
64 form = CommentForm() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
65 try: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
66 data = form.to_python(data) |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
67 comment = link.add_comment(**data) |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
68 raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
69 except Invalid, e: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
70 errors = e.unpack_errors() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
71 else: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
72 errors = {} |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
73 |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
74 return template.render(link=link, comment=None, |
616
8ada29c99c0d
GenshiTutorial: use form filler on comment/reply views.
cmlenz
parents:
615
diff
changeset
|
75 errors=errors) | HTMLFormFiller(data=data) |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
76 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
77 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
78 def main(filename): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
79 # load data from the pickle file, or initialize it to an empty list |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
80 if os.path.exists(filename): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
81 fileobj = open(filename, 'rb') |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
82 try: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
83 data = pickle.load(fileobj) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
84 finally: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
85 fileobj.close() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
86 else: |
618
b6706f9346ac
Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents:
616
diff
changeset
|
87 data = {} |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
88 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
89 def _save_data(): |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
90 # save data back to the pickle file |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
91 fileobj = open(filename, 'wb') |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
92 try: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
93 pickle.dump(data, fileobj) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
94 finally: |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
95 fileobj.close() |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
96 cherrypy.engine.on_stop_engine_list.append(_save_data) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
97 |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
98 # Some global configuration; note that this could be moved into a |
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
99 # configuration file |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
100 cherrypy.config.update({ |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
101 'request.throw_errors': True, |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
102 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
103 'tools.decode.on': True, |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
104 'tools.trailing_slash.on': True, |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
105 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)), |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
106 }) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
107 |
619
756e7418e10c
GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents:
618
diff
changeset
|
108 cherrypy.quickstart(Root(data), '/', { |
611
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
109 '/media': { |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
110 'tools.staticdir.on': True, |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
111 'tools.staticdir.dir': 'static' |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
112 } |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
113 }) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
114 |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
115 if __name__ == '__main__': |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
116 import formencode |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
117 formencode.api.set_stdtranslation(languages=['en']) |
236c351928a2
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
118 main(sys.argv[1]) |