forked from kjam/cherrypy-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
100 lines (76 loc) · 3.02 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import cherrypy
from jinja2 import Environment,FileSystemLoader
from db_tools import get_value,set_value,del_value
from views import get_global_links,get_polls,get_poll,\
add_poll,edit_poll,cast_vote
import os.path
conf = os.path.join(os.path.dirname(__file__), 'app.conf')
env = Environment(loader=FileSystemLoader('templates'))
class PollViews:
@cherrypy.expose
def index(self):
data_dict = {
'title': 'Poll List',
'links': get_global_links(),
'polls': get_polls(),
}
templ = env.get_template('polls.html')
return templ.render(data_dict)
@cherrypy.expose
def poll(self,key,choice=None):
method = cherrypy.request.method.upper()
poll = False
data_dict = {
'title': 'Poll',
'links': get_global_links(),
}
if method == 'POST':
data_dict['poll'] = cast_vote(key,choice)
data_dict['success'] = True
else:
data_dict['poll'] = get_poll(key)
templ = env.get_template('poll.html')
return templ.render(data_dict)
@cherrypy.expose
def add(self,**kwargs):
method = cherrypy.request.method.upper()
poll = False
data_dict = {
'title': 'Add a poll',
'links': get_global_links(),
}
if method == 'POST':
add_poll(**kwargs)
data_dict['success'] = True
templ = env.get_template('add_poll.html')
return templ.render(data_dict)
@cherrypy.expose
def edit(self,key,**kwargs):
method = cherrypy.request.method.upper()
poll = False
data_dict = {
'title': 'Edit your poll',
'links': get_global_links(),
}
if method == 'POST':
poll = edit_poll(**kwargs)
data_dict['poll'] = poll
data_dict['success'] = True
else:
data_dict['poll'] = get_poll(key)
templ = env.get_template('edit_poll.html')
return templ.render(data_dict)
class PollApp:
polls = PollViews()
@cherrypy.expose
def index(self):
data_dict = {
'title': 'Welcome to the poll application!',
'links': get_global_links(),
}
templ = env.get_template('index.html')
return templ.render(data_dict)
if __name__ == '__main__':
cherrypy.quickstart(PollApp(), config=conf)
else:
cherrypy.tree.mount(PollApp(), config=conf)