-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
201 lines (172 loc) · 6.57 KB
/
functions.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
import os
import re
import requests
from datetime import timedelta
# akvo-webform util
instance_base = 'https://api-auth0.akvo.org/flow/orgs/'
auth_domain = "https://akvofoundation.eu.auth0.com/oauth/token"
def reformat_duration(x):
return str(timedelta(seconds=x))
def camel_case_split(str):
matches = re.finditer(
'.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', str)
splitted = [m.group(0).lower().strip() for m in matches]
return " ".join(splitted)
def split_partnership_code(x, index=0):
if not x:
return ''
res = x.split('|')[index]
if index == 1 or ':' not in res:
return res
# get partnership code
res = res.split(':')[0]
return res
def split_reporting_period(x, index=0):
if not x:
return ''
return str(x.split('|')[index])
def fill_partnership_code(x, df, target_col):
# for now we use name column as example
repeat_index = x.get('repeat no')
instance = x.get('instance')
if (repeat_index == 1):
pass
find_partnership_code = df[df['instance'] == instance].iloc[0]
return find_partnership_code.get(target_col)
def find_excel_column_letter(renamed_columns):
start_index = 1 # it can start either at 0 or at 1
letter = ''
column_int = len(renamed_columns)
while column_int > 25 + start_index:
letter += chr(65 + int(
(len(renamed_columns) - start_index) / 26) - 1)
column_int = column_int - (
int((len(renamed_columns) - start_index) / 26)) * 26
letter += chr(65 - start_index + (int(column_int)))
return letter
def get_headers(token: str):
login = {
'client_id': 'S6Pm0WF4LHONRPRKjepPXZoX1muXm1JS',
'grant_type': 'refresh_token',
'refresh_token': token,
'scope': 'openid email'
}
req = requests.post(auth_domain, data=login)
if req.status_code != 200:
return False
return {
'Content-Type': 'application/json',
'Accept': 'application/vnd.akvo.flow.v2+json',
'Authorization': 'Bearer {}'.format(req.json().get('id_token'))
}
def get_data(uri, auth):
req = requests.get(uri, headers=auth).json()
if req.get('error'):
print(f"Error 403: {req.get('error')}")
raise Exception(req.get('error'))
return req
def fetch_all(url, headers, formInstances=[]):
data = get_data(url, headers)
next_url = data.get('nextPageUrl')
data = data.get('formInstances')
for d in data:
formInstances.append(d)
if next_url:
fetch_all(next_url, headers, formInstances)
return formInstances
def handle_list(data, target):
response = []
for value in data:
if value.get("code"):
response.append("{}:{}".format(value.get("code"),
value.get(target)))
else:
response.append(value.get(target))
return "|".join(response)
def data_handler(data, qType):
if data:
if qType in [
'FREE_TEXT', 'NUMBER', 'BARCODE', 'DATE', 'GEOSHAPE', 'SCAN',
'CADDISFLY'
]:
return data
if qType == 'OPTION':
return handle_list(data, "text")
if qType == 'CASCADE':
return handle_list(data, "name")
if qType == ['PHOTO', 'VIDEO']:
return data.get('filename')
if qType == 'VIDEO':
return data.get('filename')
if qType == 'GEO':
return {'lat': data.get('lat'), 'long': data.get('long')}
if qType == 'SIGNATURE':
return data.get("name")
return None
def get_page(instance: str, survey_id: int, form_id: int, token: str):
headers = get_headers(token)
instance_uri = '{}{}'.format(instance_base, instance)
form_instance_url = '{}/form_instances?survey_id={}&form_id={}'.format(
instance_uri, survey_id, form_id)
collections = fetch_all(form_instance_url, headers)
form_definition = get_data('{}/surveys/{}'.format(instance_uri, survey_id),
headers)
form_definition = form_definition.get('forms')
form_definition = list(
filter(lambda x: int(x['id']) == form_id,
form_definition))[0].get('questionGroups')
results = []
for col in collections:
dt = {}
dt_repeatable = {}
for c in col:
if c != 'responses':
meta = camel_case_split(c)
dt.update({meta: col[c]})
else:
for g in form_definition:
for q in g['questions']:
try:
answers = col[c][g.get('id')]
for i, a in enumerate(answers):
if i > 0:
dt_repeatable.update({'repeat no': i + 1})
for c in col:
if c != 'responses':
meta = camel_case_split(c)
dt_repeatable.update({
meta: col[c]})
d = data_handler(a.get(q['id']), q['type'])
# n = "{}|{}".format(q['id'], q['name'])
n = q['name'].lower().strip()
dt_repeatable.update({n: d})
else:
dt.update({'repeat no': i + 1})
d = data_handler(
answers[0].get(q['id']), q['type'])
# n = "{}|{}".format(q['id'], q['name'])
n = q['name'].lower().strip()
dt.update({n: d})
except(TypeError):
n = q['name'].lower().strip()
dt.update({'repeat no': 1, n: ''})
results.append(dt)
if dt_repeatable:
results.append(dt_repeatable)
return results
def get_refresh_token():
data = {
"client_id": os.environ['the_client_id'],
"username": os.environ['the_email'],
"password": os.environ['the_pwd'],
"grant_type": "password",
"scope": "offline_access"
}
req = requests.post(auth_domain, data=data)
refresh_token = None
if req and req.status_code == 200:
refresh_token = req.json().get('refresh_token')
f = open('token.txt', 'w')
f.write(refresh_token)
f.close()
return refresh_token