Firestore to Mongodb
This commit is contained in:
@@ -5,6 +5,7 @@ import string
|
||||
import uuid
|
||||
|
||||
import nltk
|
||||
from pymongo.database import Database
|
||||
from wonderwords import RandomWord
|
||||
|
||||
from helper.constants import *
|
||||
@@ -1210,7 +1211,7 @@ def gen_write_blanks_form_exercise_listening_monologue(text: str, quantity: int,
|
||||
}
|
||||
|
||||
|
||||
def gen_multiple_choice_level(quantity: int, start_id=1):
|
||||
def gen_multiple_choice_level(mongo_db: Database, quantity: int, start_id=1):
|
||||
gen_multiple_choice_for_text = "Generate " + str(
|
||||
quantity) + " multiple choice questions of 4 options for an english level exam, some easy questions, some intermediate " \
|
||||
"questions and some advanced questions. Ensure that the questions cover a range of topics such as " \
|
||||
@@ -1240,9 +1241,9 @@ def gen_multiple_choice_level(quantity: int, start_id=1):
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
|
||||
if len(question["questions"]) != quantity:
|
||||
return gen_multiple_choice_level(quantity, start_id)
|
||||
return gen_multiple_choice_level(mongo_db, quantity, start_id)
|
||||
else:
|
||||
all_exams = get_all("level")
|
||||
all_exams = get_all(mongo_db, "level")
|
||||
seen_keys = set()
|
||||
for i in range(len(question["questions"])):
|
||||
question["questions"][i], seen_keys = replace_exercise_if_exists(all_exams, question["questions"][i],
|
||||
@@ -1677,10 +1678,10 @@ def gen_blank_space_text_utas(quantity: int, start_id: int, size: int, topic=ran
|
||||
return question["question"]
|
||||
|
||||
|
||||
def gen_reading_passage_utas(start_id, sa_quantity: int, mc_quantity: int, topic=random.choice(mti_topics)):
|
||||
def gen_reading_passage_utas(mongo_db: Database, start_id, sa_quantity: int, mc_quantity: int, topic=random.choice(mti_topics)):
|
||||
passage = generate_reading_passage_1_text(topic)
|
||||
short_answer = gen_short_answer_utas(passage["text"], start_id, sa_quantity)
|
||||
mc_exercises = gen_text_multiple_choice_utas(passage["text"], start_id + sa_quantity, mc_quantity)
|
||||
mc_exercises = gen_text_multiple_choice_utas(mongo_db, passage["text"], start_id + sa_quantity, mc_quantity)
|
||||
return {
|
||||
"exercises": {
|
||||
"shortAnswer": short_answer,
|
||||
@@ -1719,7 +1720,7 @@ def gen_short_answer_utas(text: str, start_id: int, sa_quantity: int):
|
||||
GEN_QUESTION_TEMPERATURE)["questions"]
|
||||
|
||||
|
||||
def gen_text_multiple_choice_utas(text: str, start_id: int, mc_quantity: int):
|
||||
def gen_text_multiple_choice_utas(mongo_db: Database, text: str, start_id: int, mc_quantity: int):
|
||||
json_format = {
|
||||
"questions": [
|
||||
{
|
||||
@@ -1771,7 +1772,7 @@ def gen_text_multiple_choice_utas(text: str, start_id: int, mc_quantity: int):
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
|
||||
if len(question["questions"]) != mc_quantity:
|
||||
return gen_multiple_choice_level(mc_quantity, start_id)
|
||||
return gen_multiple_choice_level(mongo_db, mc_quantity, start_id)
|
||||
else:
|
||||
response = fix_exercise_ids(question, start_id)
|
||||
response["questions"] = randomize_mc_options_order(response["questions"])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
|
||||
from firebase_admin import firestore
|
||||
from google.cloud import storage
|
||||
from pymongo.database import Database
|
||||
|
||||
|
||||
def download_firebase_file(bucket_name, source_blob_name, destination_file_name):
|
||||
@@ -50,38 +50,16 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
|
||||
return None
|
||||
|
||||
|
||||
def save_to_db(collection: str, item):
|
||||
db = firestore.client()
|
||||
collection_ref = db.collection(collection)
|
||||
(update_time, document_ref) = collection_ref.add(item)
|
||||
def save_to_db_with_id(mongo_db: Database, collection: str, item, id: str):
|
||||
collection_ref = mongo_db[collection]
|
||||
|
||||
document_ref = collection_ref.insert_one({"id": id, **item})
|
||||
if document_ref:
|
||||
logging.info(f"Document added with ID: {document_ref.id}")
|
||||
return (True, document_ref.id)
|
||||
logging.info(f"Document added with ID: {document_ref.inserted_id}")
|
||||
return (True, document_ref.inserted_id)
|
||||
else:
|
||||
return (False, None)
|
||||
|
||||
|
||||
def save_to_db_with_id(collection: str, item, id: str):
|
||||
db = firestore.client()
|
||||
collection_ref = db.collection(collection)
|
||||
# Reference to the specific document with the desired ID
|
||||
document_ref = collection_ref.document(id)
|
||||
# Set the data to the document
|
||||
document_ref.set(item)
|
||||
if document_ref:
|
||||
logging.info(f"Document added with ID: {document_ref.id}")
|
||||
return (True, document_ref.id)
|
||||
else:
|
||||
return (False, None)
|
||||
|
||||
|
||||
def get_all(collection: str):
|
||||
db = firestore.client()
|
||||
collection_ref = db.collection(collection)
|
||||
|
||||
all_exercises = (
|
||||
collection_ref
|
||||
.get()
|
||||
)
|
||||
|
||||
return all_exercises
|
||||
def get_all(mongo_db: Database, collection: str):
|
||||
return list(mongo_db[collection].find())
|
||||
|
||||
Reference in New Issue
Block a user