forked from anandology/electionspot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
43 lines (37 loc) · 1.15 KB
/
utils.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
import web
import simplejson
import re
import config
def json_processor(handler):
if web.ctx.path.endswith('.json'):
web.ctx.path = web.ctx.path[:-len(".json")]
web.ctx.method = "GET_json"
web.header('Content-Type', 'application/json')
d = handler()
return simplejson.dumps(d)
else:
return handler()
cache = {}
re_accepts_gzip = re.compile(r'\bgzip\b')
def cache_processor(handler):
"""cache and gzip processor.
Inspired by django.middleware.gzip.GZipMiddleware
"""
ae = web.ctx.env.get('HTTP_ACCEPT_ENCODING', '')
if config.cache and web.ctx.method in ["GET", "GET_json"] and re_accepts_gzip.search(ae):
if web.ctx.path not in cache:
data = handler()
cache[web.ctx.fullpath] = compress(web.safestr(data))
web.expires(3600) # one hour
web.header('Content-Encoding', 'gzip')
return cache[web.ctx.fullpath]
else:
return handler()
def compress(data):
from cStringIO import StringIO
import gzip
f = StringIO()
gz = gzip.GzipFile(fileobj=f, mode='wb')
gz.write(data)
gz.flush()
return f.getvalue()