comparison examples/tutorial/geddit/controller.py @ 628:9822dfff0fd5 trunk

GenshiTutorial: tweaks to sync with code on wiki page.
author cmlenz
date Mon, 03 Sep 2007 19:59:03 +0000
parents 3ed77fbfafa8
children e990fa05d80b
comparison
equal deleted inserted replaced
627:3ed77fbfafa8 628:9822dfff0fd5
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import operator 3 import operator, os, pickle, sys
4 import os
5 import pickle
6 import sys
7 4
8 import cherrypy 5 import cherrypy
9 from formencode import Invalid 6 from formencode import Invalid
10 from genshi.filters import HTMLFormFiller 7 from genshi.filters import HTMLFormFiller
11 8
16 13
17 class Root(object): 14 class Root(object):
18 15
19 def __init__(self, data): 16 def __init__(self, data):
20 self.data = data 17 self.data = data
21
22 @cherrypy.expose
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 18
34 @cherrypy.expose 19 @cherrypy.expose
35 @template.output('index.html') 20 @template.output('index.html')
36 def index(self): 21 def index(self):
37 links = sorted(self.data.values(), key=operator.attrgetter('time')) 22 links = sorted(self.data.values(), key=operator.attrgetter('time'))
90 stream = template.render('_form.html', link=link, errors=errors) 75 stream = template.render('_form.html', link=link, errors=errors)
91 else: 76 else:
92 stream = template.render(link=link, comment=None, errors=errors) 77 stream = template.render(link=link, comment=None, errors=errors)
93 return stream | HTMLFormFiller(data=data) 78 return stream | HTMLFormFiller(data=data)
94 79
80 @cherrypy.expose
81 @template.output('index.xml', method='xml')
82 def feed(self, id=None):
83 if id:
84 link = self.data.get(id)
85 if not link:
86 raise cherrypy.NotFound()
87 return template.render('info.xml', link=link)
88 else:
89 links = sorted(self.data.values(), key=operator.attrgetter('time'))
90 return template.render(links=links)
91
95 92
96 def main(filename): 93 def main(filename):
97 # load data from the pickle file, or initialize it to an empty list 94 # load data from the pickle file, or initialize it to an empty list
98 if os.path.exists(filename): 95 if os.path.exists(filename):
99 fileobj = open(filename, 'rb') 96 fileobj = open(filename, 'rb')
111 pickle.dump(data, fileobj) 108 pickle.dump(data, fileobj)
112 finally: 109 finally:
113 fileobj.close() 110 fileobj.close()
114 cherrypy.engine.on_stop_engine_list.append(_save_data) 111 cherrypy.engine.on_stop_engine_list.append(_save_data)
115 112
116 # Some global configuration; note that this could be moved into a 113 # Some global configuration; note that this could be moved into a
117 # configuration file 114 # configuration file
118 cherrypy.config.update({ 115 cherrypy.config.update({
119 'request.throw_errors': True, 116 'request.throw_errors': True,
120 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', 117 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
121 'tools.decode.on': True, 118 'tools.decode.on': True,
Copyright (C) 2012-2017 Edgewall Software