-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-utility.py
334 lines (264 loc) · 8.07 KB
/
admin-utility.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import json
from typing import Dict, List
import requests
base_url = "http://thriveapp.pythonanywhere.com/api/journeys/"
welcome_message = """
+-----------------------------+
| Welcome to Admin utility! |
| What do you want to do? |
+-----------------------------+
"""
quit_instruction = "Type \\q to quit at anytime."
main_menu = """
MAIN MENU
================================
0 approve a journey
1 approve a quest
2 draft a journey with quests
3 draft a quest alone
4 view submitted journeys
5 view submitted quests
6 edit journey
7 edit quest
================================
"""
approve_body = {
"approved": True
}
journey_string = """
ID {0}, {1} -------------------
Name: {2}
Description: {3}
Quests:
{4}
User name: {5}
Email: {6}
"""
quest_string = """
ID {0}, {1} ---------------
Name: {2}
Description: {3}
Survey Question: {4}
"""
edit_journey_prompt = """What do you want to change?
0 name
1 description
9 nothing else
"""
edit_quest_prompt = """What do you want to change?
0 name
1 description
2 survey question
3 order
9 nothing else
"""
def check_function(user_input: str):
if user_input.lower() == "\\q":
exit()
elif user_input.lower() == "\\m":
return True
def process_input(prompt: str):
user_input = input(prompt)
while check_function(user_input):
print("Invalid command \\m")
user_input = input(prompt)
return user_input
def approve_journey():
jid = input("Draft Journey ID: ")
if check_function(jid):
return
url = base_url + "submit-journeys/" + jid
body = approve_body
response = requests.patch(url, json=body)
if response.status_code == 202:
print("Successfully approved Journey ID " + jid + " and its quests!")
else:
print("Something's wrong")
def approve_quest():
qid = input("Draft Quest ID: ")
if check_function(qid):
return
url = base_url + "submit-quests/" + qid
body = approve_body
response = requests.patch(url, json=body)
if response.status_code == 202:
print("Successfully approved Quest ID " + qid + "!")
else:
print("Something's wrong")
def gather_quest_info() -> Dict:
quest = {}
quest["name"] = process_input("Quest Name: ")
quest["description"] = process_input("Quest Description: ")
quest["survey_question"] = process_input("Survey Question: \n"
"Type 'no' if don't want to include any\n")
if quest["survey_question"].lower() == "no":
quest.pop("survey_question")
return quest
def submit_journey():
req = {}
req["quests"] = []
req["user_name"] = process_input("username: ")
req["email"] = process_input("email: ")
req["name"] = process_input("Journey name: ")
req["description"] = process_input("Journey description: ")
while 1:
quest = gather_quest_info()
req["quests"].append(quest)
if process_input("More Quests? (y/n)").lower() == "n":
break
body = req
url = base_url + "submit-journeys"
response = requests.post(url, json=body)
jid = response.json()[0]["id"]
if response.status_code == 201:
print("Success!")
print(f"ID: {jid}")
else:
print("Something's wrong")
def submit_quest():
req = {}
req["quests"] = []
req["user_name"] = process_input("username: ")
req["email"] = process_input("email: ")
quest = gather_quest_info()
req["quests"].append(quest)
body = req
url = base_url + "submit-quests"
response = requests.post(url, json=body)
qid = response.json()[0]["id"]
if response.status_code == 201:
print("Success!")
print(f"ID: {qid}")
else:
print("Something's wrong")
def generate_quests_string(quests: List, mode: str) -> str:
if mode == "j":
sorted_quests = sorted(quests, key=lambda d: d['order'])
else:
sorted_quests = sorted(quests, key=lambda d: d['id'])
ret = ""
for quest in sorted_quests:
qid = quest["id"]
qname = quest["name"]
qdescription = quest["description"]
qsurvey_question = quest["survey_question"]
if qsurvey_question is None or qsurvey_question == "":
qsurvey_question = "No survey question"
if quest["approved"]:
qapproved = "LIVE"
else:
qapproved = "DRAFT"
qstring = quest_string.format(qid, qapproved, qname, qdescription, qsurvey_question)
ret += qstring
return ret
def get_submitted_journeys():
url = base_url + "submit-journeys"
response = requests.get(url)
if not response.ok:
print("Something's wrong")
return
journeys = response.json()
ret = ""
for journey in journeys:
jid = journey["id"]
jname = journey["name"]
jdescription = journey["description"]
user_name = journey["user_name"]
e_mail = journey["email"]
qstring = generate_quests_string(journey["submitted_quests"], "j")
if journey["approved"]:
japproved = "LIVE"
else:
japproved = "DRAFT"
ret += journey_string.format(jid, japproved, jname, jdescription, qstring, user_name, e_mail)
print("All submitted Journeys:")
print(ret)
def get_submitted_quests():
url = base_url + "submit-quests"
response = requests.get(url)
if not response.ok:
print("Something's wrong")
return
quests = response.json()
print("All Submitted Quests:")
print(generate_quests_string(quests, "q"))
def journey_change_options() -> Dict:
req = {}
while 1:
try:
choice = int(process_input(edit_journey_prompt))
if choice == 0:
req["name"] = process_input("Journey name: ")
elif choice == 1:
req["description"] = process_input("Journey description: ")
elif choice == 9:
break
except ValueError:
print("Invalid choice")
return req
def edit_journey():
jid = process_input("Draft Journey ID: ")
req = journey_change_options()
url = base_url + "submit-journeys/" + jid
response = requests.patch(url, json=req)
if not response.ok:
print("Something's wrong, did you change a journey that is already live?")
return
print("Changes Accepted!")
def quest_change_options() -> Dict:
req = {}
while 1:
try:
choice = int(process_input(edit_quest_prompt))
if choice == 0:
req["name"] = process_input("Quest name: ")
elif choice == 1:
req["description"] = process_input("Quest description: ")
elif choice == 2:
req["survey_question"] = process_input("Survey Question: ")
elif choice == 3:
req["order"] = process_input("Order: ")
print("Remember to change the order of the other quests in this journey!")
elif choice == 9:
break
except ValueError:
print("Invalid choice")
return req
def edit_quest():
qid = process_input("Draft Quest ID: ")
url = base_url + "submit-quests/" + qid
req = quest_change_options()
response = requests.patch(url, json=req)
if not response.ok:
print("Something's wrong, did you change a quest that is already live?")
return
print("Changes accepted!")
def call_choice(choice: int):
if choice == 0:
approve_journey()
elif choice == 1:
approve_quest()
elif choice == 2:
submit_journey()
elif choice == 3:
submit_quest()
elif choice == 4:
get_submitted_journeys()
elif choice == 5:
get_submitted_quests()
elif choice == 6:
edit_journey()
elif choice == 7:
edit_quest()
else:
print("invalid choice")
if __name__ == "__main__":
print(welcome_message)
print(quit_instruction)
while 1:
try:
choice = input(main_menu)
check_function(choice)
call_choice(int(choice))
except ValueError:
print("Invalid input")