-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
executable file
·117 lines (92 loc) · 3.18 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
#!/usr/bin/env python
import app_config
import argparse
import copytext
import csv
import imp
import json
import oauth
import os
import static
import static_post
import static_theme
from flask import Blueprint, Flask, make_response, render_template, render_template_string
from glob import glob
from render_utils import make_context, smarty_filter, urlencode_filter, number_filter, CSSIncluder, JavascriptIncluder
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
app.debug = app_config.DEBUG
app.jinja_env.filters['smarty'] = smarty_filter
app.jinja_env.filters['urlencode'] = urlencode_filter
app.jinja_env.filters['format_number'] = number_filter
posts = Blueprint('posts', __name__, template_folder='posts/')
# Example application views
@app.route('/')
@oauth.oauth_required
def _posts_list():
"""
Renders a list of all posts for local testing.
"""
context = make_context()
context['posts'] = []
posts = glob('%s/*' % app_config.POST_PATH)
for post in posts:
name = post.split('%s/' % app_config.POST_PATH)[1]
context['posts'].append(name)
context['posts_count'] = len(context['posts'])
return make_response(render_template('post_list.html', **context))
@app.route('/sitemap.xml')
def _sitemap():
"""
Render a simple sitemap for look content.
"""
context = make_context()
with open('data/sitemap.csv') as f:
posts = csv.reader(f)
posts.next()
context['posts'] = list(posts)
return make_response(render_template('sitemap.xml', **context))
@posts.route('/posts/<slug>/')
@oauth.oauth_required
def _post(slug):
"""
Renders a post without the tumblr wrapper.
"""
post_path = '%s/%s' % (app_config.POST_PATH, slug)
context = make_context()
context['slug'] = slug
context['COPY'] = copytext.Copy(filename='data/%s.xlsx' % slug)
context['JS'] = JavascriptIncluder(asset_depth=2, static_path=post_path)
context['CSS'] = CSSIncluder(asset_depth=2, static_path=post_path)
try:
post_config = imp.load_source('post_config', '%s/post_config.py' % post_path)
context.update(post_config.__dict__)
except IOError:
pass
if os.path.exists('%s/featured.json' % post_path):
with open('%s/featured.json' % post_path) as f:
context['featured'] = json.load(f)
with open('%s/templates/index.html' % post_path) as f:
template = f.read().decode('utf-8')
return make_response(render_template_string(template, **context))
app.register_blueprint(static.static)
app.register_blueprint(posts)
app.register_blueprint(oauth.oauth)
app.register_blueprint(static_post.post)
app.register_blueprint(static_theme.theme)
# Enable Werkzeug debug pages
if app_config.DEBUG:
wsgi_app = DebuggedApplication(app, evalex=False)
else:
wsgi_app = app
# Boilerplate
if __name__ == '__main__':
print 'This command has been removed! 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)