Skip to content

Commit

Permalink
Merge pull request #141 from ia-toki/feature/generate-testing-quiz
Browse files Browse the repository at this point in the history
Add script to generate testing quiz
  • Loading branch information
gmochid authored Mar 18, 2024
2 parents 4a37a52 + aefe19d commit 99fd6b1
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions utils/scripts/generate_testing_quiz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Generate Testing Quiz
This script allows us to add a new (testing) quiz & specify the participant.
We can also add more participants in the existing quiz by filling the value of `quiz_doc_id`.
"""

import json
import os
from datetime import datetime
from firebase_admin import credentials, initialize_app, firestore


QUIZ_TEMPLATE = {
"title": None,
"created_at": str(datetime.now()),
"start_at": "2024-01-01 00:00:00",
"end_at": "2045-12-31 00:00:00",
"tasks": {'penegak': [], 'penggalang': [], 'siaga': [], 'sikecil': []},
"max_attempts": {'penegak': 1, 'penggalang': 1, 'siaga': 1, 'sikecil': 1},
"duration_minute": {'penegak': 24, 'penggalang': 24, 'siaga': 20, 'sikecil': 15},
"sponsors": {'penegak': None, 'penggalang': None, 'siaga': None, 'sikecil': None},
}

PARTICIPANT_TEMPLATE = {
"created_at": str(datetime.now()),
"user_uid": "",
"user_name": "",
"challenge_group": "penegak",
"quiz_id": None,
"quiz_title": None,
"quiz_start_at": None,
"quiz_end_at": None,
"quiz_max_attempts": 1,
"attempts": []
}


creds = credentials.Certificate(
json.loads(os.environ["FIREBASE_SECRET_KEY_JSON"])
)
initialize_app(creds)
firestore_client = firestore.client()


def add_new_testing_quiz_data(quiz_title, quiz_tasks):
quiz_data = QUIZ_TEMPLATE
quiz_data["title"] = quiz_title
quiz_data["tasks"]["penegak"] = quiz_tasks

col_ref = firestore_client.collection("weekly_quiz_list")
_, doc_ref = col_ref.add(quiz_data)
quiz_data["doc_id"] = doc_ref.id

return quiz_data


def register_participant(participants, quiz_data):
col_ref = firestore_client.collection("weekly_quiz_participation")
participant_data = PARTICIPANT_TEMPLATE
participant_data["quiz_title"] = quiz_data["title"]
participant_data["quiz_id"] = quiz_data["doc_id"]
participant_data["quiz_start_at"] = quiz_data["start_at"]
participant_data["quiz_end_at"] = quiz_data["end_at"]
print("Registered participant IDs: ")

for user_name, user_uid in participants:
participant_data["user_name"] = user_name
participant_data["user_uid"] = user_uid
_, doc_ref = col_ref.add(participant_data)
print(doc_ref.id)


if __name__ == "__main__":
quiz_doc_id = None
quiz_title = "Testing Latihan Bebras @now" # edit this
quiz_tasks = ["2016-BE-01", "2016-LT-02", "2016-JP-01"] # edit this

# list of participant in the format (user_name, user_uid)
participants = [
("User 1", "YHyEryTmogRIIaUNMHcOD72ZsaV"), # delete this
("User 2", "YeGQ3YBWXkRbDMfRu4z5cjrdeqV"), # delete this
]

# # ToDo: add functionality to also remove unused testing quiz
# prev_quiz_id = []

if quiz_doc_id is None:
quiz = add_new_testing_quiz_data(quiz_title, quiz_tasks)
print("Generated Quiz {}, ID: {}".format(quiz["title"], quiz["doc_id"]))
else:
col_ref = firestore_client.collection("weekly_quiz_list")
doc = col_ref.document(quiz_doc_id).get()
if doc.exists:
quiz = doc.to_dict()
quiz["doc_id"] = doc.id
print("Registered more participants in Quiz {}, ID: {}".format(quiz["title"], quiz["doc_id"]))
else:
raise Exception("Quiz not found!")

register_participant(participants, quiz)

0 comments on commit 99fd6b1

Please sign in to comment.