-
Notifications
You must be signed in to change notification settings - Fork 11
/
web.py
77 lines (66 loc) · 1.89 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import threading
import operator, os, sys
import cherrypy
import template
from config import Config
from model import Item
from updater import ItemUpdater
from form import ItemForm
from formencode import Invalid
from genshi.input import HTML
from genshi.filters import HTMLFormFiller, HTMLSanitizer
from ebay import ItemNotFoundException
class Root(object):
def __init__(self, updater):
self.updater = updater
@cherrypy.expose
@template.output("index.html")
def index(self):
return template.render(items=Config.items.values())
@cherrypy.expose
def delete(self, id=None, *args, **kw):
id = int(id)
if id in Config.items:
del Config.items[id]
raise cherrypy.HTTPRedirect('/')
@cherrypy.expose
@template.output('submit.html')
def submit(self, cancel=False, **data):
if cherrypy.request.method == 'POST':
if cancel:
raise cherrypy.HTTPRedirect('/')
form = ItemForm()
try:
data = form.to_python(data)
item = Item(**data)
self.updater.updateItem(item)
Config.items[item.id] = item
raise cherrypy.HTTPRedirect('/')
except Invalid, e:
errors = e.unpack_errors()
except ItemNotFoundException, e:
errors = { "id" : e }
else:
errors = {}
return template.render(errors=errors) | HTMLFormFiller(data=data)
class WebServer(threading.Thread):
def __init__(self, cli, updater):
threading.Thread.__init__(self)
self.cli = cli
self.updater = updater
def stop(self):
cherrypy.engine.exit()
def subscribe(self, callback):
if hasattr(cherrypy.engine, 'subscribe'):
cherrypy.engine.subscribe('stop', callback)
else:
cherrypy.engine.on_stop_engine_list.append(callback)
def run(self):
cherrypy.config.update({ 'global': {
'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
'log.screen': False,
'engine.SIGHUP': None,
'engine.SIGTERM': None
}
})
cherrypy.quickstart(Root(self.updater), '/', "cherrypy.conf")