-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
183 lines (127 loc) · 5.8 KB
/
forms.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
# contains method to retrieve form data
from auth import get_service
# update the title of form
def update_title_of_form(formId, title="new form"):
service = get_service(api_name='forms',api_version='v1',)
update = {
"requests": [{
"updateFormInfo": {
"info": {
"title": title,
"documentTitle" : title
},
"updateMask": "*"
}
}]
}
return service.forms().batchUpdate(formId=formId, body=update).execute() # Update the form with a description
#get description of form, (:str, :dict)
def get_form_detail(formId):
service = get_service(api_name='forms',api_version='v1',)
result = service.forms().get(formId=formId).execute()
return result
# print(get_form())
#extract url of form, (formId:str, url:str)
def get_form_url(formId):
return get_form_detail(formId)['responderUri']
#extract and create image link , (:dict, :list)
def create_image_url(link):
if not link.count('='):
return ''
return 'https://drive.google.com/uc?id=' + link.split('=')[1]
# using thumbnail for low resolution images(like in committee page)
# reduce bandwidth usage and improve speed
def convert_link_to_public_thumbnail(link):
# print(link)
if not link.count('='):
return ''
return 'https://drive.google.com/thumbnail?id=' + link.split('=')[1] #use thumbnails, small size and load faster
#extract answers from text response in forms, (:dict, :list)
def get_text_answers(textAnswer):
result = []
for temp in textAnswer['textAnswers']['answers']:
# print(temp['value'])
result.append(temp['value'])
return result
#to create beautifully decorated json , (formType:enum, :list)
# define the fields of data expected in a specified order
def formMapper(formType = 'committee'):
# formtype = commitee, achievement, event
if formType == 'committee':
return ['position','name','fb','insta', 'twitter', 'linkedin', 'image']
elif formType == 'achievements':
return ['id', 'timestamp','title','image','desc','date', 'fb', 'youtube', 'medium', 'insta']
elif formType == 'events':
return ['title','formLink', 'image','desc','date', 'type', 'fb', 'youtube', 'medium', 'insta', 'status']
# extract id of each question to create a accurate mapping, (:str, :list)
def get_form_question_id(formId):
items = get_form_detail(formId)['items']
result = []
for temp in items:
result.append(temp['questionItem']['question']['questionId'])
return result
# extarct from data, ((formId:str, formType:enum), :dict)
# simply form name as a formType , only committee,achievements and events are defined for now
def retrieve_form_data(formId, formType='committee'):
service = get_service(api_name='forms',api_version='v1',)
formData = service.forms().responses().list(formId=formId).execute()
#check if there is any data or not, if not return empty dict
if not formData.get('responses'):
return {}
questionId = get_form_question_id(formId)
result = {}
responseMapper = formMapper(formType)
for temp in formData['responses']:
# print(temp)
indivisual = {}
i = 0
for id in questionId:
if dict.get(temp['answers'],id): #skip if no answer id found, so response with no data will not have key
# print(temp['answers'][id])
if temp['answers'][id].get('textAnswers'):
indivisual[responseMapper[i]] = get_text_answers(temp['answers'][id])
elif temp['answers'][id].get('fileUploadAnswers'):
indivisual[responseMapper[i]] = convert_link_to_public_thumbnail(temp['answers'][id])
i += 1
result[formType].append(indivisual)
return result
# ###############
# below code is the live use case of a project
# #############
def retrieve_form_data(formId, formType='committee'):
service = get_service(api_name='forms',api_version='v1',)
formData = service.forms().responses().list(formId=formId).execute()
#check if there is any data or not, if not return empty dict
if not formData.get('responses'):
return {}
questionId = get_form_question_id(formId)
result = {}
responseMapper = formMapper(formType) #to beautify the json
for temp in formData['responses']:
# print(temp)
indivisual = {}
i = 0
for id in questionId:
if dict.get(temp['answers'],id): #skip if no answer id found, so response with no data will not have key
# print(temp['answers'][id])
if temp['answers'][id].get('textAnswers'):
indivisual[responseMapper[i]] = get_text_answers(temp['answers'][id])
elif temp['answers'][id].get('fileUploadAnswers'):
indivisual[responseMapper[i]] = convert_link_to_public_thumbnail(temp['answers'][id])
i += 1
if formType == 'committee': ##construct a data of committee
if result.get(indivisual['position'][0]):
result[indivisual['position'][0]].append(indivisual)
else:
result[indivisual['position'][0]] = [indivisual]
elif formType == 'achievements': ##construct a data of achievements
if result.get('achievements'):
result['achievements'].append(indivisual)
else:
result['achievements'] = [indivisual]
elif formType == 'events': #construct a data of events
if result.get('events'):
result['events'].append(indivisual)
else:
result['events'] = [indivisual]
return result