forked from nprapps/dailygraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic_templates.py
118 lines (87 loc) · 3.91 KB
/
graphic_templates.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
import imp
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 make_context, render_with_context
graphic_templates = Blueprint('graphic_templates', __name__)
@graphic_templates.route('/<slug>/')
@oauth.oauth_required
def _templates_detail(slug):
"""
Renders a parent.html index with child.html embedded as iframe.
"""
template_path = '%s/%s' % (app_config.TEMPLATES_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=template_path)
context['slug'] = slug
try:
graphic_config = imp.load_source('graphic_config', '%s/graphic_config.py' % template_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' % (template_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('parent.html', **context))
@graphic_templates.route('/<slug>/child.html')
@oauth.oauth_required
def _templates_child(slug):
"""
Renders a child.html for embedding.
"""
template_path = '%s/%s' % (app_config.TEMPLATES_PATH, slug)
# Fallback for legacy projects w/o child templates
if not os.path.exists('%s/child_template.html' % template_path):
with open('%s/child.html' % template_path) as f:
contents = f.read()
return contents
context = make_context(asset_depth=2, root_path=template_path)
context['slug'] = slug
config_path = '%s/graphic_config.py' % template_path
if not os.path.exists(config_path):
config_path = '%s/_base/graphic_config.py' % app_config.TEMPLATES_PATH
try:
graphic_config = imp.load_source('graphic_config', config_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' % (template_path, slug)
context['COPY'] = copytext.Copy(filename=copy_path)
except IOError:
pass
env = Environment(loader=FileSystemLoader([template_path, '%s/_base' % app_config.TEMPLATES_PATH]))
env.globals.update(render=render_with_context)
template = env.get_template('child_template.html')
return make_response(template.render(**context))
# Render graphic LESS files on-demand
@graphic_templates.route('/<slug>/css/<filename>.less')
def _templates_less(slug, filename):
"""
Compiles LESS for a graphic.
"""
template_path = '%s/%s' % (app_config.TEMPLATES_PATH, slug)
less_path = '%s/css/%s.less' % (template_path, filename)
if not os.path.exists(less_path):
less_path = '%s/_base/css/%s.less' % (app_config.TEMPLATES_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 from either graphic or base graphic paths
@graphic_templates.route('/<slug>/<path:path>')
def _static(slug, path):
template_path = '%s/%s' % (app_config.TEMPLATES_PATH, slug)
real_path = '%s/%s' % (template_path, path)
if not os.path.exists(real_path):
real_path = '%s/_base/%s' % (app_config.TEMPLATES_PATH, path)
if not os.path.exists(real_path):
abort(404)
with open('%s' % real_path) as f:
return f.read(), 200, { 'Content-Type': guess_type(real_path)[0] }