-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to generate next weekly quiz
- Loading branch information
1 parent
af47dc4
commit b92013f
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
import os | ||
import random | ||
from datetime import datetime, timedelta | ||
from firebase_admin import credentials, initialize_app, firestore | ||
|
||
|
||
creds = credentials.Certificate( | ||
json.loads(os.environ["FIREBASE_SECRET_KEY_JSON"]) | ||
) | ||
initialize_app(creds) | ||
|
||
firestore_client = firestore.client() | ||
configuration_coll_ref = firestore_client.collection("configuration") | ||
problemset_coll_ref = firestore_client.collection("problem_set") | ||
weeklyquizlist_coll_ref = firestore_client.collection("weekly_quiz_list") | ||
|
||
config_global_variables = configuration_coll_ref.document("global_variables").get().to_dict() | ||
config_global_variables["weeklyquiz_number"] += 1 # increase weekly quiz number ID | ||
today = datetime.now() | ||
monday = ( | ||
today - timedelta(days=today.weekday()) | ||
).replace( | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
microsecond=0 | ||
) | ||
result = { | ||
"title": "LATIHAN BEBRAS MINGGUAN #" + config_global_variables["weeklyquiz_number"], | ||
"created_at": str(today), | ||
"start_at": str(monday + timedelta(days=7)), | ||
"end_at": str(monday + timedelta(days=14)), | ||
"problems": {}, | ||
"max_attempts": {}, | ||
"sponsors": {} | ||
} | ||
|
||
challengegroup_map = configuration_coll_ref.document("challenge_group").get().to_dict() | ||
for cg_group, cg_val in challengegroup_map.items(): | ||
problem_id_list = [] | ||
|
||
query_ref = problemset_coll_ref.where("challenge_group", "==", cg_group) | ||
for doc in query_ref.stream(): | ||
problem_id_list.append(doc.to_dict()["id"]) | ||
random.shuffle(problem_id_list) | ||
|
||
result["problems"][cg_group] = problem_id_list[:cg_val["weeklyquiz_problem_number"]] | ||
result["max_attempts"][cg_group] = cg_val["weeklyquiz_max_attempts"] | ||
result["sponsors"][cg_group] = None | ||
|
||
# for history data, generate random id & put it in the `result` for latest data | ||
_, doc_ref = weeklyquizlist_coll_ref.add(result) | ||
result["id"] = doc_ref.id | ||
|
||
# for latest data | ||
to_running = configuration_coll_ref.document("next_weekly_quiz").get().to_dict() | ||
configuration_coll_ref.document("next_weekly_quiz").set(result) | ||
configuration_coll_ref.document("running_weekly_quiz").set(to_running) | ||
|
||
# update global variables | ||
configuration_coll_ref.document("global_variables").set(config_global_variables) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
firebase-admin==6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
import os | ||
from firebase_admin import credentials, initialize_app, firestore | ||
from glob import glob | ||
|
||
creds = credentials.Certificate( | ||
json.loads(os.environ["FIREBASE_SECRET_KEY_JSON"]) | ||
) | ||
initialize_app(creds) | ||
|
||
firestore_client = firestore.client() | ||
col_ref = firestore_client.collection("problem_set") | ||
|
||
file_list = glob("data/question_bank/*/*/*.json") | ||
for file in file_list: | ||
with open(file, "r") as f: | ||
file_ref = file.replace(".json", "").split("/") | ||
name = "_".join(file_ref[-2:]) | ||
|
||
content = json.load(f) | ||
content["challenge_group"] = file_ref[-2] | ||
|
||
col_ref.document(name).set(content) |