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 5
/
app.py
executable file
·151 lines (114 loc) · 4.01 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
"""
Example application views.
Note that `render_template` is wrapped with `make_response` in all application
routes. While not necessary for most Flask apps, it is required in the
App Template for static publishing.
"""
import app_config
import json
import oauth
import static
import re
import string
from PIL import Image
from flask import Flask, make_response, render_template
from render_utils import make_context, smarty_filter, urlencode_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.route('/')
@oauth.oauth_required
def index():
"""
Example view demonstrating rendering a simple HTML page.
"""
context = make_context()
with open('data/featured.json') as f:
context['featured'] = json.load(f)
# Read the books JSON into the page.
with open('www/static-data/books.json', 'rb') as readfile:
context['books_js'] = readfile.read()
return make_response(render_template('index.html', **context))
@app.route('/share/<slug>.html')
def share(slug):
featured_book = None
context = make_context()
with open('www/static-data/books.json', 'rb') as f:
books = json.load(f)
for book in books:
if book.get('slug') == slug:
featured_book = book
break
if not featured_book:
return 404
featured_book['thumb'] = "%sassets/cover/%s.jpg" % (context['SHARE_URL'], featured_book['slug'])
try:
book_image = Image.open('www/assets/cover/%s.jpg' % featured_book['slug'])
width, height = book_image.size
context['thumb_width'] = width
context['thumb_height'] = height
except IOError:
context['thumb_width'] = None
context['thumb_height'] = None
context['twitter_handle'] = 'nprbooks'
context['book'] = featured_book
return make_response(render_template('share.html', **context))
@app.route('/tag_share/<slug>.html')
def tag_share(slug):
featured_tag = None
context = make_context()
tags = context['COPY']['tags']
for tag in tags:
if tag['key'] == slug:
featured_tag = tag
break
if not featured_tag:
return 404
context['tag_thumb'] = "%sassets/tag/%s.jpg" % (context['SHARE_URL'],
featured_tag['img'])
try:
book_image = Image.open('www/assets/tag/%s.jpg' % featured_tag['img'])
width, height = book_image.size
context['thumb_width'] = width
context['thumb_height'] = height
except IOError:
context['thumb_width'] = None
context['thumb_height'] = None
context['twitter_handle'] = 'nprbooks'
context['tag'] = featured_tag
return make_response(render_template('tag_share.html', **context))
@app.route('/seamus')
def seamus():
"""
Preview for Seamus page
"""
context = make_context()
# Read the books JSON into the page.
with open('www/static-data/books.json', 'rb') as readfile:
books_data = json.load(readfile)
books = sorted(books_data, key=lambda k: k['title'])
# Harvest long tag names
for book in books:
tag_list = []
for tag in book['tags']:
tag_list.append(context['COPY']['tags'][tag]['value'])
book['tag_list'] = tag_list
context['books'] = books
return render_template('seamus-preview.html', **context)
@app.route('/coming-soon.html')
def coming_soon():
context = make_context()
return make_response(render_template('coming-soon.html', **context))
app.register_blueprint(static.static)
app.register_blueprint(oauth.oauth)
# 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__':
print 'This command has been removed! Please run "fab app" instead!'