/
utils.py
72 lines (61 loc) · 1.89 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
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
from datetime import date
from datetime import datetime
from functools import wraps, update_wrapper
from flask import make_response
from simplejson import JSONEncoder
from werkzeug.routing import BaseConverter
class CustomJSONEncoder(JSONEncoder):
"""
Normalizes date and date-time outputs when serializing JSON
in requests to the ISO format.
"""
def default(self, obj):
try:
if isinstance(obj, datetime) or isinstance(obj, date):
obj = obj.isoformat()
iterable = iter(obj)
except TypeError:
pass
else:
return "".join(list(iterable))
return JSONEncoder.default(self, obj)
class DateConverter(BaseConverter):
"""
Normalizes incoming values in a route definition marked
as `date` to follow the YYYY-MM-DD format that the database
expects
"""
def to_python(self, value):
return datetime.strptime(value, "%Y-%m-%d").date()
def to_url(self, value):
return value.isoformat()
def nocache(view):
"""
Adds response headers to prevent clients from caching
responses
:param view:
:return:
"""
@wraps(view)
def no_cache(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers['Last-Modified'] = datetime.now()
response.headers[
'Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
return update_wrapper(no_cache, view)
def check_date(date):
"""
Checks the validity of a string as a date string suitable
for the database.
:param date:
:return:
"""
try:
import datetime
datetime.datetime.strptime(date, '%Y-%m-%d')
except ValueError:
return False
return True