This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
107 lines (85 loc) · 2.72 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
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
import os
import re
import importlib
def get_env():
return os.environ.get('DEPLOYMENT_ENVIRONMENT', 'dev')
settings = importlib.import_module('config.%s.settings' % get_env())
def valid_uuid(possible_uuid):
"""
Checks that a possible UUID4 string is a valid UUID4.
"""
regex = re.compile('^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I)
match = regex.match(possible_uuid)
return bool(match)
def clean_payload(payload):
"""
Serializes a payload from form strings to more useful Python types.
`payload` is a dictionary where both keys and values are exclusively strings.
* empty string becomes None
* applies a true / false test to possible true / false string values.
"""
output = {}
for k,v in payload.items():
# Takes the first value.
v = v[0]
# Serializes values
if v == u'':
v = None
if v.lower() in ['true', 'yes', 'y', '1']:
v = True
if v.lower() in ['false', 'no', 'n', '0']:
v = False
# Values not in the test pass through.
output[k] = v
return output
def get_page(request):
try:
page = int(request.args.get('page', '1'))
except TypeError:
page = 1
return page
def paginate(request, obj_count, page, context):
context['page'] = page
total_pages = int(obj_count) // int(settings.ITEMS_PER_PAGE)
remainder = int(obj_count) % int(settings.ITEMS_PER_PAGE)
if remainder > 0:
total_pages += 1
previous_page = page - 1
next_page = page + 1
has_next = True
has_previous = True
if previous_page < 1:
previous_page = 1
has_previous = False
if next_page > total_pages:
next_page = total_pages
has_next = False
last_item = page * settings.ITEMS_PER_PAGE
if not has_next:
last_item = obj_count
if not has_previous:
first_item = 1
else:
first_item = (settings.ITEMS_PER_PAGE * (page - 1)) + 1
context['pagination'] = {
'last_item': last_item,
'first_item': first_item,
'remainder': remainder,
'total': obj_count,
'has_next': has_next,
'has_previous': has_previous,
'page': page,
'total_pages': total_pages,
'previous_page_number': previous_page,
'next_page_number': next_page,
'pages': list(range(1,total_pages + 1))
}
return context
def build_context(request):
"""
We needed some static context for our pages and
you won't believe what happened next.
"""
context = {}
context['user_email'] = request.environ.get('HTTP_X_GOOG_AUTHENTICATED_USER_EMAIL', None) or 'test@test.dev'
return context