This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·116 lines (94 loc) · 3.03 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
#!/usr/bin/env python
import app_config
import json
import os
import re
import static
from fabfile.text import update as update_google_doc
from flask import Flask, Markup, make_response, render_template
from jinja2 import contextfunction, Environment, FileSystemLoader
from render_utils import make_context, smarty_filter, urlencode_filter, markdown_filter
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
app.debug = app_config.DEBUG
app.add_template_filter(smarty_filter, name='smarty')
app.add_template_filter(urlencode_filter, name='urlencode')
app.add_template_filter(markdown_filter, name='markdown')
# Example application views
@app.route('/')
def index():
"""
Example view demonstrating rendering a simple HTML page.
"""
# update_google_doc()
context = make_context()
with open('data/featured.json') as f:
context['featured'] = json.load(f)
return make_response(render_template('index.html', **context))
@app.route('/comments/')
def comments():
"""
Full-page comments view.
"""
return make_response(render_template('comments.html', **make_context()))
@app.route('/widget.html')
def widget():
"""
Embeddable widget example page.
"""
return make_response(render_template('widget.html', **make_context()))
@contextfunction
def render_file(context, path):
"""
Render a file with the current context
"""
env = Environment(loader=FileSystemLoader('www/assets'))
template = env.get_template(path)
return template.render(**context)
@contextfunction
def photos(context, group):
"""
Render one or more photos defined in the spreadsheet.
"""
photoset = [photo for photo in context['COPY']['photos'] if photo['group'] == group]
whitespace_regex = re.compile(r'\s+') # Remove whitespace for Markdown embedding
fragments = []
for row in photoset:
template = '%s.html' % row['template']
output = render_template(template, row=row)
output = whitespace_regex.sub(' ', output)
fragments.append(output)
return Markup("".join(fragments))
def note(text):
"""
Return a note.
"""
return render_template('_note.html', note=text)
@app.context_processor
def context_processor():
"""
Add our app's functions
"""
context = {}
context['render_file'] = render_file
context['photos'] = photos
context['note'] = note
return context
app.register_blueprint(static.static)
# Enable Werkzeug debug pages
if app_config.DEBUG:
wsgi_app = DebuggedApplication(app, evalex=False)
else:
wsgi_app = app
# Catch attempts to run the app directly
if __name__ == '__main__':
if not os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
print 'This command has been deprecated! Please run "fab app" instead!'
import argparse
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)