forked from nprapps/playgrounds2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·337 lines (266 loc) · 10.8 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
import cgi
from datetime import date
import datetime
import json
from mimetypes import guess_type
import re
from sets import Set
import urllib
import envoy
from flask import Flask, Markup, abort, render_template, url_for
from jinja2 import Template
import requests
import app_config
import copytext
from models import Playground, Revision, display_field_name, get_active_playgrounds
from render_utils import flatten_app_config, make_context
import argparse
app = Flask(app_config.PROJECT_NAME)
@app.route('/email/<revision_group>/')
def _prepare_email(revision_group):
revisions = Revision.select().where(Revision.revision_group == int(revision_group))
context = {}
context['base_url'] = '%s/playground/' % app_config.S3_BASE_URL
context['total_revisions'] = revisions.count()
context['deletes'] = {}
context['deletes']['playgrounds'] = []
context['deletes']['total_revisions'] = 0
context['inserts'] = {}
context['inserts']['playgrounds'] = []
context['inserts']['total_revisions'] = 0
context['updates'] = {}
context['updates']['playgrounds'] = []
context['updates']['total_revisions'] = 0
inserts = revisions.where(Revision.action == 'insert')
if inserts.count() > 0:
context['inserts']['total_revisions'] = inserts.count()
for revision in inserts:
p = Playground.get(slug=revision.playground.slug)
playground_dict = p.__dict__['_data']
playground_dict['display_name'] = p.display_name
playground_dict['site_url'] = '%s/playground/%s.html' % (app_config.S3_BASE_URL, revision.playground.slug)
playground_dict['revision_group'] = int(revision_group)
playground_dict['headers'] = revision.get_headers()
playground_dict['feature_count'] = int(p.feature_count)
nearby = p.nearby(3)
playground_dict['nearby'] = []
for n in nearby:
if n.distance < 0.5:
playground_dict['nearby'].append(n)
context['inserts']['playgrounds'].append(playground_dict)
context['inserts']['playgrounds'] = sorted(context['inserts']['playgrounds'], key=lambda p: p['name'])
deletes = revisions.where(Revision.action == 'delete-request')
if deletes.count() > 0:
context['deletes']['total_revisions'] = deletes.count()
for revision in deletes:
p = Playground.get(slug=revision.playground.slug)
playground_dict = playground_dict = p.__dict__['_data']
playground_dict['display_name'] = p.display_name
playground_dict['site_url'] = '%s/playground/%s.html' % (app_config.S3_BASE_URL, revision.playground.slug)
playground_dict['delete_url'] = '%s/delete-playground/%s/' % (app_config.SERVER_BASE_URL, revision.playground.slug)
playground_dict['revision_group'] = int(revision_group)
for item in json.loads(revision.log):
if item.get('field', None) == "reason":
playground_dict['text'] = cgi.escape(item.get('to'))
playground_dict['headers'] = revision.get_headers()
context['deletes']['playgrounds'].append(playground_dict)
context['deletes']['playgrounds'] = sorted(context['deletes']['playgrounds'], key=lambda p: p['name'])
updates = revisions.where(Revision.action == 'update')
if updates.count() > 0:
context['updates']['total_revisions'] = updates.count()
updated_playgrounds = Set([])
for revision in updates:
updated_playgrounds.add(revision.playground.slug)
for playground_slug in updated_playgrounds:
p = Playground.get(slug=playground_slug)
playground_dict = p.__dict__['_data']
playground_dict['display_name'] = p.display_name
playground_dict['site_url'] = '%s/playground/%s.html' % (app_config.S3_BASE_URL, playground_slug)
playground_dict['revisions'] = []
for revision in updates:
if revision.playground.id == p.id:
revision_dict = {}
revision_dict['revision_group'] = revision_group
revision_dict['fields'] = revision.get_log()
revision_dict['headers'] = revision.get_headers()
playground_dict['revisions'].append(revision_dict)
context['updates']['playgrounds'].append(playground_dict)
context['updates']['playgrounds'] = sorted(context['updates']['playgrounds'], key=lambda p: p['name'])
with open('templates/_email.html', 'rb') as read_template:
payload = Template(read_template.read())
return payload.render(**context)
@app.route('/email/')
def _list_revision_groups():
context = {}
context['revision_groups'] = Set([])
revisions = Revision.select()
for revision in revisions:
revision_date = datetime.datetime.fromtimestamp(revision.revision_group)
context['revision_groups'].add((revision_date, revision.revision_group))
context['revision_groups'] = sorted(context['revision_groups'], key=lambda g: g[0], reverse=True)
return render_template('_email_list.html', **context)
def intcomma(value):
"""
Converts an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
"""
value = str(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', value)
if value == new:
return new
else:
return intcomma(new)
@app.route('/')
def index():
"""
Playgrounds index page.
"""
context = make_context()
context['playground_count'] = intcomma(Playground.select().count())
return render_template('index.html', **context)
@app.route('/search.html')
def search():
"""
Search results page.
"""
context = make_context()
return render_template('search.html', **context)
@app.route('/add-search.html')
def add_search():
"""
Pre-add search page
"""
context = make_context()
return render_template('add_search.html', **context)
@app.route('/create.html')
def playground_create():
context = make_context()
context['features'] = Playground.features_form()
return render_template('create.html', **context)
@app.route('/sitemap.xml')
def sitemap():
"""
Renders a sitemap.
"""
context = make_context()
context['pages'] = []
now = date.today().isoformat()
context['pages'].append(('/', now))
for playground in get_active_playgrounds():
context['pages'].append((url_for('_playground', playground_slug=playground.slug), now))
sitemap = render_template('sitemap.xml', **context)
return (sitemap, 200, { 'content-type': 'application/xml' })
@app.route('/widget.html')
def widget():
"""
Embeddable widget example page.
"""
return render_template('widget.html', **make_context())
@app.route('/test_widget.html')
def test_widget():
"""
Example page displaying widget at different embed sizes.
"""
return render_template('test_widget.html', **make_context())
@app.route('/js/embed-widget.js')
def embed_widget():
"""
Javascript to embed the widget
"""
return render_template('embed_widget.js', **make_context())
@app.route('/playground/<string:playground_slug>.html')
def _playground(playground_slug):
"""
Playground detail page.
"""
from flask import request
context = make_context()
context['playground'] = Playground.get(slug=playground_slug)
context['fields'] = context['playground'].update_form()
context['features'] = context['playground'].update_features_form()
context['revisions'] = Revision.select()\
.where(Revision.playground == context['playground'].id)\
.where((Revision.action == 'insert') | (Revision.action == 'update'))\
.order_by(Revision.timestamp.desc())
context['display_field_name'] = display_field_name
context['path'] = request.path
return render_template('playground.html', **context)
@app.route('/cloudsearch/<path:path>')
def _cloudsearch_proxy(path):
from flask import request
args = {}
# Convert immutable MultiDict to dict
for k, v in request.args.items():
args[k] = v[0] if isinstance(v, list) else v
if 'callback' in request.args:
callback = request.args['callback']
del args['callback']
else:
callback = None
url = 'http://%s/%s?%s' % (app_config.CLOUD_SEARCH_DOMAIN, path, urllib.urlencode(args))
response = requests.get(url)
if response.status_code == 507:
return ('%s({ "error": "507" });' % callback, 200, response.headers);
output = response.text
if callback:
output = '%s(%s);' % (callback, output)
return (output, response.status_code, response.headers)
@app.route('/test/test.html')
def test_dir():
return render_template('index.html', **make_context())
# 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():
"""
This includes both client-side config and some COPY vars we need in JS.
"""
config = flatten_app_config()
js = 'window.APP_CONFIG = ' + json.dumps(config) + ';'
features = {}
data = copytext.Copy(app_config.COPY_PATH)
for feature in data['feature_list']:
features[feature['key']] = dict(zip(feature._columns, feature._row))
features = 'window.FEATURES = ' + json.dumps(features) + ';'
return '\n'.join([js, features]), 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)
@app.template_filter('urlencode')
def urlencode_filter(s):
"""
Filter to urlencode strings.
"""
if type(s) == 'Markup':
s = s.unescape()
s = s.encode('utf8')
s = urllib.quote_plus(s)
return Markup(s)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port')
args = parser.parse_args()
server_port = 8000
if args.port:
server_port = int(args.port)
app.run(host='0.0.0.0', port=server_port, debug=app_config.DEBUG)