-
Notifications
You must be signed in to change notification settings - Fork 8
/
web.py
296 lines (204 loc) · 6.71 KB
/
web.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from functools import wraps
import datetime
import json
import os
import re
import urlparse
from flask import Flask, flash, g, redirect, request, render_template, Response
from flask.ext.babel import Babel
import postmark
import pymongo
import requests
LANGUAGES = ('en', 'es')
EMPTY_BLOCK = """<br><br>"""
POSTMARK_KEY = os.environ.get('POSTMARK_KEY', '')
RECAPTCHA_PUBLIC = os.environ.get('RECAPTCHA_PUBLIC', '')
RECAPTCHA_PRIVATE = os.environ.get('RECAPTCHA_PRIVATE', '')
app = Flask(__name__)
app.secret_key = os.environ.get('FLASK_SECRETKEY', '1234567890')
babel = Babel(app)
class CrappyCache(dict):
MINUTE = 60
HOUR = 60 * 60
DAY = 60 * 60 * 24
def __init__(self, *args, **kwargs):
self.expiration = kwargs.pop("expiration", None)
super(CrappyCache, self).__init__(*args, **kwargs)
def __setitem__(self, key, value):
now = datetime.datetime.utcnow()
super(CrappyCache, self).__setitem__(key, (now, value))
def __getitem__(self, key):
if key in self:
(then, val) = super(CrappyCache, self).__getitem__(key)
if self.expiration is None:
return val
now = datetime.datetime.utcnow()
delta = now - then
if delta.seconds < self.expiration:
return val
del self[key]
SCARY_CACHE = CrappyCache(expiration=CrappyCache.MINUTE * 5)
#
# authentication stuff
#
def check_auth(username, password):
return username == 'admin' and password == os.environ.get('ADMIN_PASSWORD', '')
def authenticate():
msg = "This site is not yet available to the public. Please login."
return Response(msg, 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
#
# locale and babel goodness
#
@babel.localeselector
def get_locale():
if 'lang' in request.args:
lang = request.args['lang']
if lang in LANGUAGES:
return lang
return request.accept_languages.best_match(LANGUAGES)
#
# template filters
#
@app.template_filter()
def slugify(value):
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '-', value)
#
# request lifecycle
#
@app.before_request
def before_request():
mongo_uri = os.environ.get('MONGOHQ_URL')
if mongo_uri:
conn = pymongo.Connection(mongo_uri)
g.db = conn[os.environ.get('MONGOHQ_DB')]
else:
conn = pymongo.Connection()
g.db = conn['openingparliament']
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.connection.disconnect()
@app.context_processor
def inject_content():
doc = g.db.blocks.find_one({'path': request.path})
return {'content': doc.get('content') or EMPTY_BLOCK if doc else EMPTY_BLOCK}
@app.context_processor
def inject_admin():
return {'admin': True if request.authorization else False}
#
# the good, meaty url handlers
#
@app.route('/')
def index():
return render_template('index.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
# verify captcha
captcha_url = "http://www.google.com/recaptcha/api/verify"
params = {
"privatekey": RECAPTCHA_PRIVATE,
"remoteip": "",
"challenge": request.form['recaptcha_challenge_field'],
"response": request.form['recaptcha_response_field'],
}
response = requests.post(captcha_url, params=params)
print response.text
if not response.text.startswith("true"):
context = {"form": request.form}
flash('Sorry, your captcha was incorrect. Please try again.')
return render_template('contact.html', **context)
# send email
msg = "%s <%s>\n" % (request.form['name'], request.form['email'])
if request.form['organization']:
msg += "%s\n" % request.form['organization']
msg += "\n%s\n" % request.form['message']
kwargs = {
'api_key': POSTMARK_KEY,
'sender': '[email protected]',
'reply_to': '%s' % request.form['email'],
'bcc': '[email protected]',
'subject': '[OpeningParliament.org] contact: %s <%s>' % (request.form['name'], request.form['email']),
'text_body': msg,
}
postmark.PMMail(**kwargs).send()
flash('Your message has been sent. Thank you for contacting us!')
return redirect('/contact')
return render_template('contact.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/casestudies')
def casestudies():
return render_template('casestudies.html')
@app.route('/declaration')
def declaration():
return render_template('declaration.html')
@app.route('/glow')
def glow():
# return render_template('glow.html')
return redirect('http://openparl2014.org/')
@app.route('/networking')
def networking():
return render_template('networking.html')
@app.route('/organizations')
def organizations():
return render_template('organizations.html')
@app.route('/press')
def press():
# return render_template('press.html')
return redirect('/')
@app.route('/export')
def export():
docs = g.db.blocks.find()
content = {
'pages': [{'path': d['path'], 'content': d['content']} for d in docs],
}
return Response(json.dumps(content), content_type='application/json')
@app.route('/login')
@requires_auth
def login():
return redirect('/')
@app.route('/save', methods=['POST'])
@requires_auth
def save():
content = request.form.get('content', '').strip()
path = request.form.get('path')
if not path:
referrer = request.environ.get('HTTP_REFERER')
path = urlparse.urlparse(referrer).path
doc = {
'path': path,
'content': content,
}
g.db.blocks.update({'path': path}, {"$set": doc}, upsert=True)
return content
#
# scary RSS proxy method
#
@app.route('/rss')
def rss():
url = "http://blog.openingparliament.org/rss"
if url in SCARY_CACHE:
doc = SCARY_CACHE[url]
else:
resp = requests.get(url)
doc = resp.text
SCARY_CACHE[url] = doc
return Response(doc, content_type="text/xml")
#
# the "magic" as they call it
#
if __name__ == '__main__':
DEBUG = True
app.run(debug=DEBUG, port=8000)