-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.py
232 lines (195 loc) · 8.02 KB
/
controllers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import helpers as e # exceptions
import helpers as h
from templates import template_wrapper, TemplateArgs as targs, render
import helpers as e
from helpers import redirect, save_form_data, create_thumbnail, admin
import cherrypy
import models as m
from urllib2 import urlopen
import os.path
class Show:
@cherrypy.expose
def index(self):
redirect('/show/list')
@cherrypy.expose
@template_wrapper('/shows/list.html')
def list(self):
shows = m.Show.query.all()
return targs(shows=shows)
@cherrypy.expose
@template_wrapper('/shows/single.html')
def default(self,id):
show = m.Show.get(id)
if not show:
raise e.Validation('Could not find show')
return targs(show=show)
@cherrypy.expose
@template_wrapper('/shows/edit.html')
def update(self,show_id=None,name=None,description=None,
image=None,image_url=None,action=None):
cherrypy.log('show_is: %s'%show_id)
show = m.Show.get(show_id)
if not show:
raise e.Validation('Show not found')
if action:
if not name:
raise e.Validation('Name must be defined')
show.name = name
show.description = description
if image is not None and len(image.filename):
cherrypy.log('has image: %s' % image.filename)
image_path = save_form_data(image,
cherrypy.config.get('image_root'),
'character_')
show.picture_path = image_path
elif image_url:
cherrypy.log('has image url')
try:
image_path = _save_form_data(urlopen(image_url).read(),
cherrypy.config.get('image_root'),
image_url.rsplit('.',1)[-1],
'character_')
show.picture_path = image_path
except:
cherrypy.log('bad download')
image_path = None
m.session.commit()
redirect('/show/%s' % show.id)
return targs(show=show)
@cherrypy.expose
@template_wrapper('/shows/create.html')
def create(self,name=None,description=None,image=None,
image_url=None,action=None):
if action:
if not name:
raise e.Validation('Name must be defined')
if image is not None and len(image.filename):
cherrypy.log('has image: %s' % image.filename)
image_path = save_form_data(image,
cherrypy.config.get('image_root'),
'character_')
elif image_url:
cherrypy.log('has image url')
try:
image_path = _save_form_data(urlopen(image_url).read(),
cherrypy.config.get('image_root'),
image_url.rsplit('.',1)[-1],
'character_')
except:
cherrypy.log('bad download')
image_path = None
show = m.Show(name=name,
description=description,
picture_path=os.path.basename(image_path))
m.session.add(show)
m.session.commit()
redirect('/show/%s'%show.id)
class Character:
@cherrypy.expose
def index(self):
redirect('/character/list')
@cherrypy.expose
@template_wrapper('/characters/list.html')
def list(self):
shows = m.Show.query.all()
return targs(shows=shows)
@cherrypy.expose
@template_wrapper('/characters/single.html')
def default(self,id):
character = m.Character.get(id)
if not character:
raise e.Validation('Could not find character')
return targs(character=character)
@cherrypy.expose
def vote(self,id,direction):
character = m.Character.get(id)
if not character:
raise e.Validation('Could not find character')
if direction.lower() == 'liberal':
character.liberal_vote_count += 1
elif direction.lower() == 'conservative':
character.conservative_vote_count += 1
else:
raise e.Validation('What is %s?' % direction)
character.total_vote_count += 1
m.session.commit()
redirect('/character/%s' % character.id)
@cherrypy.expose
def graph(self,id,**kwargs):
cherrypy.log('graph: %s' % id)
character = m.Character.get(id)
if not character:
raise e.Validation('Could not find character')
src = render('/characters/graph.html',character=character).strip()
return redirect(src)
@cherrypy.expose
@template_wrapper('/characters/create.html')
def create(self,name=None,show_id=None,description=None,
image=None,image_url=None,action=None):
if action:
if not name:
raise e.Validation('Name required')
if not show_id:
raise e.Validation('Show required')
if not description:
raise e.Validation('Description required')
show = m.Show.get(show_id)
if not show:
raise e.Validation('Show not found')
if image is not None and len(image.filename):
cherrypy.log('has image: %s' % image.filename)
image_path = save_form_data(image,
cherrypy.config.get('image_root'),
'character_')
elif image_url:
cherrypy.log('has image url')
try:
image_path = _save_form_data(urlopen(image_url).read(),
cherrypy.config.get('image_root'),
image_url.rsplit('.',1)[-1],
'character_')
except:
cherrypy.log('bad download')
image_path = None
else:
image_path = None
cherrypy.log("image_path: %s" % image_path)
character = m.Character(name=name,
show=show,
description=description,
picture_path=os.path.basename(image_path))
m.session.add(character)
m.session.commit()
redirect('/character/%s' % character.id)
return targs(shows=m.Show.query.all())
class Thumbnail:
@cherrypy.expose
def default(self,img,size=200):
cherrypy.log('thumbnail: %s' % img)
# get the path
image_root = cherrypy.config.get('image_root')
thumbnail_root = cherrypy.config.get('thumbnail_root')
path = create_thumbnail(os.path.join(image_root,img),
thumbnail_root,
size)
path = os.path.abspath(path)
# check and see if the thumbnail already exists
cherrypy.log('thumbnail path: %s' % path)
return cherrypy.lib.static.serve_file(path)
class Root:
show = Show()
character = Character()
thumbnail = Thumbnail()
@cherrypy.expose
@template_wrapper('/index.html')
def index(self):
top_characters = m.Character.query. \
order_by(
m.Character.total_vote_count.desc()). \
limit(10).distinct().all()
top_shows = m.Show.query.join('characters'). \
order_by(
m.Character.total_vote_count.desc()). \
limit(10).distinct().all()
return targs(top_characters=top_characters,
top_shows=top_shows)