generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_gform_items.py
144 lines (118 loc) · 3.7 KB
/
create_gform_items.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
from create_quiz import QUIZ_CATEGORIES
def create_gform_question(question_obj):
"""
Takes a question python object and returns the JSON
ready to use building the Google Form
Args:
question_obj: A Question object
"""
return {
"createItem": {
"item": {
"title": question_obj.get_question(),
"questionItem": {
"question": {
"required": True,
"grading": {
"pointValue": 1,
"correctAnswers": {
"answers":
[{"value": question_obj.get_correct_answer()}]
},
},
"choiceQuestion": {
"type": "RADIO",
"options": [
{"value":
question_obj.get_correct_answer()},
{"value":
question_obj.get_incorrect_answers()[0]},
{"value":
question_obj.get_incorrect_answers()[1]},
{"value":
question_obj.get_incorrect_answers()[2]}
],
"shuffle": True
}
}
},
},
"location": {
"index": 0
}
}
}
def create_gform_text_question(question_to_ask):
"""
Makes a Google Forms questions with a
text field input for response
Args:
question_to_ask: The Question to ask
"""
return {
"createItem": {
"item": {
"title": question_to_ask,
"questionItem": {
"question": {
"required": True,
"textQuestion": {
"paragraph": False
}
},
},
},
"location": {
"index": 0
}
}
}
def create_gform_page_break(round_num, category):
"""
Creates a page break for a Google Form
Args:
round_num: The number of the round
category: The index of the category of the round
"""
page_break = {
"createItem": {
"item": {
"title": f"Round {round_num}: {QUIZ_CATEGORIES[category]}",
"pageBreakItem": {}
},
"location": {
"index": 0
}
},
}
return page_break
def create_gform_round(round_obj):
"""
Takes a game round python object and returns a list
of "createItem" objects to send to the Google Forms
API
Args:
round_obj: A Round object
"""
nums_qs = round_obj.get_num_qs()
round_num = round_obj.get_round_num()
category = round_obj.get_category()
questions = round_obj.get_questions()
form_items = []
for question in questions:
form_items.insert(0, create_gform_question(question))
form_items.append(create_gform_page_break(round_num, category))
return form_items
def create_gform_game(game_obj):
"""
Takes a quiz game python object and returns a list
of "createItem" objects to send to the Google Forms
API
Args:
game_obj: A Quiz Game object
"""
form_items = []
rounds = game_obj.rounds
for round in rounds:
form_items.insert(0, create_gform_round(round))
return form_items