annotate examples/tutorial/geddit/controller.py @ 625:abad7c2ebe15 trunk

GenshiTutorial: implemented AJAX commenting.
author cmlenz
date Fri, 31 Aug 2007 17:01:00 +0000
parents 6780f1b1b20d
children 3ed77fbfafa8
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
625
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
13 from geddit.lib import ajax, 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
622
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
23 @template.output('index.xml', method='xml')
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
24 def feed(self, id=None):
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
25 if id:
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
26 link = self.data.get(id)
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
27 if not link:
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
28 raise cherrypy.NotFound()
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
29 return template.render('info.xml', link=link)
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
30 else:
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
31 links = sorted(self.data.values(), key=operator.attrgetter('time'))
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
32 return template.render(links=links)
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
33
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
34 @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
35 @template.output('index.html')
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
36 def index(self):
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
37 links = sorted(self.data.values(), key=operator.attrgetter('time'))
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
38 return template.render(links=links)
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
39
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
40 @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
41 @template.output('info.html')
622
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
42 def info(self, id):
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
43 link = self.data.get(id)
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
44 if not link:
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
45 raise cherrypy.NotFound()
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
46 return template.render(link=link)
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
47
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
48 @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
49 @template.output('submit.html')
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
50 def submit(self, cancel=False, **data):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
51 if cherrypy.request.method == 'POST':
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
52 if cancel:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
53 raise cherrypy.HTTPRedirect('/')
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
54 form = LinkForm()
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
55 try:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
56 data = form.to_python(data)
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
57 link = Link(**data)
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
58 self.data[link.id] = link
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
59 raise cherrypy.HTTPRedirect('/')
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
60 except Invalid, e:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
61 errors = e.unpack_errors()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
62 else:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
63 errors = {}
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
64
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
65 return template.render(errors=errors) | HTMLFormFiller(data=data)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
66
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
67 @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
68 @template.output('comment.html')
622
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
69 def comment(self, id, cancel=False, **data):
6780f1b1b20d GenshiTutorial: add Atom feeds.
cmlenz
parents: 619
diff changeset
70 link = self.data.get(id)
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
71 if not link:
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
72 raise cherrypy.NotFound()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
73 if cherrypy.request.method == 'POST':
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
74 if cancel:
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
75 raise cherrypy.HTTPRedirect('/info/%s' % link.id)
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
76 form = CommentForm()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
77 try:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
78 data = form.to_python(data)
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
79 comment = link.add_comment(**data)
625
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
80 if not ajax.is_xhr():
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
81 raise cherrypy.HTTPRedirect('/info/%s' % link.id)
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
82 return template.render('_comment.html', comment=comment,
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
83 num=len(link.comments))
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
84 except Invalid, e:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
85 errors = e.unpack_errors()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
86 else:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
87 errors = {}
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
88
625
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
89 if ajax.is_xhr():
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
90 stream = template.render('_form.html', link=link, errors=errors)
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
91 else:
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
92 stream = template.render(link=link, comment=None, errors=errors)
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 622
diff changeset
93 return stream | HTMLFormFiller(data=data)
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
94
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
95
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
96 def main(filename):
236c351928a2 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
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
98 if os.path.exists(filename):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
99 fileobj = open(filename, 'rb')
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
100 try:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
101 data = pickle.load(fileobj)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
102 finally:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
103 fileobj.close()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
104 else:
618
b6706f9346ac Simplify the tutorial project: comments are now flat, not hierarchical.
cmlenz
parents: 616
diff changeset
105 data = {}
611
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 def _save_data():
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
108 # save data back to the pickle file
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
109 fileobj = open(filename, 'wb')
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
110 try:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
111 pickle.dump(data, fileobj)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
112 finally:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
113 fileobj.close()
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
114 cherrypy.engine.on_stop_engine_list.append(_save_data)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
115
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
116 # 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
117 # configuration file
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
118 cherrypy.config.update({
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
119 'request.throw_errors': True,
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
120 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
121 'tools.decode.on': True,
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
122 'tools.trailing_slash.on': True,
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
123 'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
124 })
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
125
619
756e7418e10c GenshiTutorial: various updates to sync with wiki page.
cmlenz
parents: 618
diff changeset
126 cherrypy.quickstart(Root(data), '/', {
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
127 '/media': {
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
128 'tools.staticdir.on': True,
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
129 'tools.staticdir.dir': 'static'
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
130 }
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
131 })
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
132
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
133 if __name__ == '__main__':
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
134 import formencode
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
135 formencode.api.set_stdtranslation(languages=['en'])
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
136 main(sys.argv[1])
Copyright (C) 2012-2017 Edgewall Software