forked from nprapps/dailygraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic.py
118 lines (88 loc) · 3.66 KB
/
graphic.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
#!/usr/bin/env python
from mimetypes import guess_type
import os
import subprocess
from flask import Blueprint, abort, make_response, render_template, render_template_string
from jinja2 import Environment, FileSystemLoader
import app_config
import copytext
import oauth
from render_utils import load_graphic_config, make_context, render_with_context, smarty_filter
graphic = Blueprint('graphic', __name__)
@graphic.route('/<slug>/')
@oauth.oauth_required
def _graphics_detail(slug):
"""
Renders a parent.html index with child.html embedded as iframe.
"""
from flask import request
graphic_path = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
# NOTE: Parent must load pym.js from same source as child to prevent version conflicts!
context = make_context(asset_depth=2, root_path=graphic_path)
context['slug'] = slug
context['var_name'] = slug.replace('-', '_')
template = 'parent.html'
try:
graphic_config = load_graphic_config(graphic_path)
context.update(graphic_config.__dict__)
if hasattr(graphic_config, 'COPY_GOOGLE_DOC_KEY') and graphic_config.COPY_GOOGLE_DOC_KEY:
copy_path = '%s/%s.xlsx' % (graphic_path, slug)
if request.args.get('refresh'):
oauth.get_document(graphic_config.COPY_GOOGLE_DOC_KEY, copy_path)
context['COPY'] = copytext.Copy(filename=copy_path)
except IOError:
pass
return make_response(render_template(template, **context))
@graphic.route('/<slug>/child.html')
@oauth.oauth_required
def _graphics_child(slug):
"""
Renders a child.html for embedding.
"""
graphic_path = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
print graphic_path
# Fallback for legacy projects w/o child templates
if not os.path.exists('%s/child_template.html' % graphic_path):
with open('%s/child.html' % graphic_path) as f:
contents = f.read()
return contents
context = make_context(asset_depth=2, root_path=graphic_path)
context['slug'] = slug
context['var_name'] = slug.replace('-', '_')
env = Environment(loader=FileSystemLoader(graphic_path))
try:
graphic_config = load_graphic_config(graphic_path)
context.update(graphic_config.__dict__)
if hasattr(graphic_config, 'JINJA_FILTER_FUNCTIONS'):
for func in graphic_config.JINJA_FILTER_FUNCTIONS:
env.filters[func.__name__] = func
if hasattr(graphic_config, 'COPY_GOOGLE_DOC_KEY') and graphic_config.COPY_GOOGLE_DOC_KEY:
copy_path = '%s/%s.xlsx' % (graphic_path, slug)
context['COPY'] = copytext.Copy(filename=copy_path)
except IOError:
pass
env.globals.update(render=render_with_context)
env.filters['smarty'] = smarty_filter
template = env.get_template('child_template.html')
return make_response(template.render(**context))
# Render graphic LESS files on-demand
@graphic.route('/<slug>/css/<filename>.less')
def _graphic_less(slug, filename):
"""
Compiles LESS for a graphic.
"""
graphic_path = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
less_path = '%s/css/%s.less' % (graphic_path, filename)
if not os.path.exists(less_path):
abort(404)
r = subprocess.check_output(['node_modules/less/bin/lessc', less_path])
return make_response(r, 200, { 'Content-Type': 'text/css' })
# Serve arbitrary static files on-demand
@graphic.route('/<slug>/<path:path>')
def _static(slug, path):
real_path = '%s/%s/%s' % (app_config.GRAPHICS_PATH, slug, path)
try:
with open(real_path) as f:
return f.read(), 200, { 'Content-Type': guess_type(real_path)[0] }
except IOError:
abort(404)