This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·319 lines (239 loc) · 10.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
import json
from mimetypes import guess_type
import urllib
import envoy
from flask import Flask, Markup, abort, render_template, redirect, Response
import app_config
from models import Joke, Episode, EpisodeJoke, JokeConnection
from render_utils import flatten_app_config, make_context
app = Flask(app_config.PROJECT_NAME)
def _all_seasons():
output = []
SEASONS = [1, 2, 3]
if app_config.IMPORT_NEW_SEASON is True:
SEASONS.append(4)
for season in SEASONS:
season_dict = {}
season_dict['season'] = season
season_dict['episodes'] = []
for episode in Episode.select().where(Episode.season == season):
season_dict['episodes'].append({
'url': 'episode-%s.html' % episode.code,
'text': '%s: %s' % (episode.episode, episode.title),
'episode': episode.episode,
'code': episode.code
})
season_dict['episodes'] = sorted(season_dict['episodes'], key=lambda episode: episode['episode'])
output.append(season_dict)
return output
@app.route('/episode-<episode_code>.html')
def _episode_detail(episode_code):
context = make_context()
context['episode'] = Episode.get(Episode.code == episode_code)
context['jokes'] = {}
context['joke_count'] = 0
for joke in EpisodeJoke.select().where(EpisodeJoke.episode == context['episode']):
group = joke.joke.primary_character
if group not in app_config.PRIMARY_CHARACTER_LIST:
group = 'Miscellaneous'
if group not in context['jokes']:
context['jokes'][group] = []
context['jokes'][group].append(joke)
context['joke_count'] += 1
context['seasons'] = _all_seasons()
context['group_order'] = [g for g in app_config.PRIMARY_CHARACTER_LIST if g in context['jokes']]
try:
context['next'] = Episode.get(number=context['episode'].number + 1)
except Episode.DoesNotExist:
context['next'] = None
try:
context['prev'] = Episode.get(number=context['episode'].number - 1)
except Episode.DoesNotExist:
context['prev'] = None
return render_template('episode_detail.html', **context)
@app.route('/joke-<joke_code>.html')
def _joke_detail(joke_code):
context = make_context()
context['joke'] = Joke.get(Joke.code == int(joke_code))
context['episodejokes'] = EpisodeJoke.select().where(EpisodeJoke.joke == context['joke'])
context['episodejokes'] = sorted(context['episodejokes'], key=lambda ej: ej.episode.code)
context['seasons'] = _all_seasons()
with open('www/live-data/jokes.json') as f:
data = json.load(f)
group_order = data['group_order']
joke_data = data['jokes']
connections = data['connections']
connected_joke_codes = [int(joke_code)]
def filter_connections(c):
if c['joke1_code'] == int(joke_code) or c['joke2_code'] == int(joke_code):
connected_joke_codes.append(c['joke1_code'])
connected_joke_codes.append(c['joke2_code'])
return True
return False
connections = filter(filter_connections, connections)
def filter_jokes(c):
return c['code'] in connected_joke_codes
for group, jokes in joke_data.items():
joke_data[group] = filter(filter_jokes, jokes)
if len(joke_data[group]) == 0:
del joke_data[group]
group_order.remove(group)
context['group_order'] = Markup(json.dumps(group_order))
context['joke_data'] = Markup(json.dumps(joke_data))
context['connection_data'] = Markup(json.dumps(connections))
context['episodes'] = Markup(json.dumps(data['episodes']))
group = context['joke'].primary_character
if group not in app_config.PRIMARY_CHARACTER_LIST:
group = 'Miscellaneous'
context['group'] = group
return render_template('joke_detail.html', **context)
@app.route('/')
def index():
context = make_context()
context['jokes'] = []
for joke in Joke.select():
context['jokes'].append(joke)
context['jokes'] = sorted(context['jokes'], key=lambda joke: joke.code)
context['seasons'] = _all_seasons()
with open('www/live-data/jokes.json') as f:
data = json.load(f)
context['group_order'] = Markup(json.dumps(data['group_order']))
context['joke_data'] = Markup(json.dumps(data['jokes']))
context['connection_data'] = Markup(json.dumps(data['connections']))
context['episodes'] = Markup(json.dumps(data['episodes']))
return render_template('viz.html', **context)
@app.route('/admin/episodes/<episode_code>/jokeconnection/<joke_connection_id>/delete/', methods=['DELETE'])
def _admin_jokeconnection_delete(episode_code, joke_connection_id):
from flask import request
if request.method == 'DELETE':
JokeConnection.delete().where(JokeConnection.id == int(joke_connection_id)).execute()
return joke_connection_id
@app.route('/admin/episodes/<episode_code>/episodejoke/<episode_joke_id>/delete/', methods=['DELETE'])
def _admin_episodejokes_delete(episode_code, episode_joke_id):
from flask import request
if request.method == 'DELETE':
EpisodeJoke.delete().where(EpisodeJoke.id == int(episode_joke_id)).execute()
return episode_joke_id
@app.route('/admin/episodes/<episode_code>/episodejoke/', methods=['PUT', 'POST'])
def _admin_episodejokes(episode_code):
from flask import request
details = request.form.get('details', None)
if request.method == 'POST':
episode_joke_id = request.form.get('episode_joke_id', None)
ej = EpisodeJoke.get(id=int(episode_joke_id))
ej.details = details
ej.save()
return '%s' % ej.id
if request.method == 'PUT':
joke_code = request.form.get('joke_code', None)
joke_type = request.form.get('type', None)
joke = Joke.get(code=int(joke_code))
episode = Episode.get(code=episode_code)
code = 's%se%sj%s' % (
str(episode.season).zfill(2),
str(episode.episode).zfill(2),
joke.code
)
context = {}
context['ej'] = EpisodeJoke(joke=joke, episode=episode, joke_type=joke_type, details=details, code=code)
context['ej'].save()
return render_template('_episodejoke_form_row.html', **context)
@app.route('/admin/episodes/<episode_code>/jokeconnection/', methods=['PUT'])
def _admin_jokeconnections(episode_code):
from flask import request
if request.method == 'POST':
pass
if request.method == 'PUT':
payload = {}
ej = EpisodeJoke.get(id=int(request.form.get('episode_joke_id')))
payload['joke1'] = ej.joke
payload['joke2'] = Joke.get(code=int(request.form.get('joke_code')))
payload['episode'] = ej.episode
j = JokeConnection(**payload)
j.save()
return("""
<br/>
<a class="related kill-related" href="#" data-jc-id="%s">×</a>
<a class="related" href="#joke-%s">%s →</a>""" % (j.id, j.joke2.code, j.joke2.text))
@app.route('/admin/episodes/')
def _admin_episodes_nocode():
return redirect('/admin/episodes/s04e01/')
@app.route('/admin/episodes/<episode_code>/', methods=['GET', 'PUT'])
def _admin_episodes(episode_code):
from flask import request
if request.method == 'GET':
context = {}
context['episode'] = Episode.get(code=episode_code)
context['episodejokes'] = EpisodeJoke.select().join(Episode).where(Episode.code == episode_code)
context['jokes'] = Joke.select().order_by(Joke.primary_character)
context['seasons'] = _all_seasons()
try:
context['next'] = Episode.get(number=context['episode'].number + 1)
except Episode.DoesNotExist:
context['next'] = None
try:
context['prev'] = Episode.get(number=context['episode'].number - 1)
except Episode.DoesNotExist:
context['prev'] = None
return render_template('admin_episode_detail.html', **context)
if request.method == 'PUT':
e = Episode.get(code=episode_code)
e.blurb = request.form.get('blurb', None)
e.save()
return '%s' % e.id
@app.route('/admin/output/')
def _admin_output():
output = {}
output['joke_main'] = ''
output['joke_details'] = ''
output['joke_connections'] = ''
for joke in Joke.select():
for episode in Episode.select().where(Episode.season == 4).order_by(Episode.number):
try:
ej = EpisodeJoke.get(episode=episode, joke=joke)
output['joke_main'] += '%s\t' % ej.joke_type
output['joke_details'] += '\'%s\t' % ej.details
if ej.connections():
output['joke_connections'] += '\'%s\t' % ej.connections()[0]['text']
else:
output['joke_connections'] += '\t'
except EpisodeJoke.DoesNotExist:
output['joke_main'] += '\t'
output['joke_details'] += '\t'
output['joke_connections'] += '\t'
output['joke_main'] += '\n'
output['joke_details'] += '\n'
output['joke_connections'] += '\n'
return render_template('_output.html', **output)
# Render LESS files on-demand
@app.route('/less/<string:filename>')
def _less(filename):
try:
with open('less/%s' % filename) as f:
less = f.read()
except IOError:
abort(404)
r = envoy.run('%s/lessc -' % app_config.APPS_NODE_PATH, data=less)
return r.std_out, 200, {'Content-Type': 'text/css'}
# Render JST templates on-demand
@app.route('/js/templates.js')
def _templates_js():
r = envoy.run('%s/jst --template underscore jst' % app_config.APPS_NODE_PATH)
return r.std_out, 200, {'Content-Type': 'application/javascript'}
# Render application configuration
@app.route('/js/app_config.js')
def _app_config_js():
config = flatten_app_config()
js = 'window.APP_CONFIG = ' + json.dumps(config)
return js, 200, {'Content-Type': 'application/javascript'}
# Server arbitrary static files on-demand
@app.route('/<path:path>')
def _static(path):
try:
with open('www/%s' % path) as f:
return f.read(), 200, {'Content-Type': guess_type(path)[0]}
except IOError:
abort(404)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=app_config.DEBUG)