forked from nprapps/dailygraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphic_templates.py
135 lines (100 loc) · 4.56 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import imp
from mimetypes import guess_type
import os
import subprocess
import sys
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_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.
"""
from flask import request
template_path = '%s/%s' % (app_config.TEMPLATES_PATH, slug)
base_template_path = '%s/%s' % (app_config.TEMPLATES_PATH, '_base')
# 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 = load_graphic_config(template_path, [base_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)
base_template_path = '%s/%s' % (app_config.TEMPLATES_PATH, '_base')
# 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
env = Environment(loader=FileSystemLoader([template_path, '%s/_base' % app_config.TEMPLATES_PATH]))
try:
graphic_config = load_graphic_config(template_path, [base_template_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' % (template_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_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)
base_less_path = '%s/_base/css/base.less' % app_config.TEMPLATES_PATH
temp_base_less_path = '%s/css/base.less' % template_path
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)
if os.path.exists(temp_base_less_path):
os.remove(temp_base_less_path)
# Temp symlink base.less so it can be included by less compiler
os.symlink(base_less_path, temp_base_less_path)
r = subprocess.check_output(['node_modules/less/bin/lessc', less_path])
# Remove temporary symlink
os.remove(temp_base_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] }