From 869e74f384ac65ec15bfc4d424bd4294154a03e4 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Wed, 29 Nov 2023 14:21:11 +0000 Subject: [PATCH 1/4] app.py edited online with Bitbucket --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index b0896eb..6c17910 100644 --- a/app.py +++ b/app.py @@ -168,7 +168,7 @@ def save_listening(): text_to_speech(part["text"], sound_file_path) file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) template["parts"][i]["audio"]["source"] = file_url - template["parts"][i]["exercises"].append(part["exercises"]) + template["parts"][i]["exercises"] = part["exercises"] if save_to_db("listening", template): return template else: From 4a0ae88fedf3474c1fa162f4af31e2493e52efe2 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Wed, 29 Nov 2023 14:55:40 +0000 Subject: [PATCH 2/4] Made it so the save_to_db also returns the ID of the document --- app.py | 20 ++++++++++++-------- helper/firebase_helper.py | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 6c17910..1306d1d 100644 --- a/app.py +++ b/app.py @@ -169,8 +169,9 @@ def save_listening(): file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) template["parts"][i]["audio"]["source"] = file_url template["parts"][i]["exercises"] = part["exercises"] - if save_to_db("listening", template): - return template + (result, id) = save_to_db("listening", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save question: " + parts) except Exception as e: @@ -279,8 +280,9 @@ def save_writing_task(): template = getWritingTemplate() for i, exercise in enumerate(exercises, start=0): template["exercises"][i]["prompt"] = exercise - if save_to_db("writing", template): - return template + (result, id) = save_to_db("writing", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save writing: " + template) except Exception as e: @@ -506,8 +508,9 @@ def save_speaking(): template["exercises"][2]["prompts"] = sp3_questions template["exercises"][2]["title"] = exercises[2]["topic"] - if save_to_db("speaking", template): - return template + (result, id) = save_to_db("speaking", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save speaking: " + template) except Exception as e: @@ -605,8 +608,9 @@ def save_reading_passage(): parts = data.get('parts') template = getReadingTemplate() template["parts"] = parts - if save_to_db("reading", template): - return template + (result, id) = save_to_db("reading", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save reading: " + template) except Exception as e: diff --git a/helper/firebase_helper.py b/helper/firebase_helper.py index 4b48c22..05fbd89 100644 --- a/helper/firebase_helper.py +++ b/helper/firebase_helper.py @@ -54,9 +54,9 @@ def save_to_db(collection: str, item): document_ref = collection_ref.add(item) if document_ref: print(f"Document added with ID: {document_ref}") - return True + return (True, document_ref) else: - return False + return (False, None) From 760fe274119948b6ddb116b73375f0965dd209b1 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Wed, 29 Nov 2023 15:23:30 +0000 Subject: [PATCH 3/4] Revert "Made it so the save_to_db also returns the ID of the document" This reverts commit 4a0ae88fedf3474c1fa162f4af31e2493e52efe2. --- app.py | 20 ++++++++------------ helper/firebase_helper.py | 6 +++--- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 1306d1d..6c17910 100644 --- a/app.py +++ b/app.py @@ -169,9 +169,8 @@ def save_listening(): file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) template["parts"][i]["audio"]["source"] = file_url template["parts"][i]["exercises"] = part["exercises"] - (result, id) = save_to_db("listening", template) - if result: - return {**template, "id": id} + if save_to_db("listening", template): + return template else: raise Exception("Failed to save question: " + parts) except Exception as e: @@ -280,9 +279,8 @@ def save_writing_task(): template = getWritingTemplate() for i, exercise in enumerate(exercises, start=0): template["exercises"][i]["prompt"] = exercise - (result, id) = save_to_db("writing", template) - if result: - return {**template, "id": id} + if save_to_db("writing", template): + return template else: raise Exception("Failed to save writing: " + template) except Exception as e: @@ -508,9 +506,8 @@ def save_speaking(): template["exercises"][2]["prompts"] = sp3_questions template["exercises"][2]["title"] = exercises[2]["topic"] - (result, id) = save_to_db("speaking", template) - if result: - return {**template, "id": id} + if save_to_db("speaking", template): + return template else: raise Exception("Failed to save speaking: " + template) except Exception as e: @@ -608,9 +605,8 @@ def save_reading_passage(): parts = data.get('parts') template = getReadingTemplate() template["parts"] = parts - (result, id) = save_to_db("reading", template) - if result: - return {**template, "id": id} + if save_to_db("reading", template): + return template else: raise Exception("Failed to save reading: " + template) except Exception as e: diff --git a/helper/firebase_helper.py b/helper/firebase_helper.py index 05fbd89..d169167 100644 --- a/helper/firebase_helper.py +++ b/helper/firebase_helper.py @@ -51,10 +51,10 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file def save_to_db(collection: str, item): db = firestore.client() collection_ref = db.collection(collection) - document_ref = collection_ref.add(item) + (update_time, document_ref) = collection_ref.add(item) if document_ref: - print(f"Document added with ID: {document_ref}") - return (True, document_ref) + print(f"Document added with ID: {document_ref.id}") + return (True, document_ref.id) else: return (False, None) From 34154b1e5fcec5354a80d1d1e3009ee90521ba0f Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Wed, 29 Nov 2023 15:44:39 +0000 Subject: [PATCH 4/4] Another try --- app.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 6c17910..88a01ff 100644 --- a/app.py +++ b/app.py @@ -169,8 +169,10 @@ def save_listening(): file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) template["parts"][i]["audio"]["source"] = file_url template["parts"][i]["exercises"] = part["exercises"] - if save_to_db("listening", template): - return template + + (result, id) = save_to_db("listening", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save question: " + parts) except Exception as e: @@ -279,8 +281,10 @@ def save_writing_task(): template = getWritingTemplate() for i, exercise in enumerate(exercises, start=0): template["exercises"][i]["prompt"] = exercise - if save_to_db("writing", template): - return template + + (result, id) = save_to_db("writing", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save writing: " + template) except Exception as e: @@ -505,9 +509,10 @@ def save_speaking(): print("Failed to create video for part 3 question: " + question) template["exercises"][2]["prompts"] = sp3_questions template["exercises"][2]["title"] = exercises[2]["topic"] - - if save_to_db("speaking", template): - return template + + (result, id) = save_to_db("speaking", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save speaking: " + template) except Exception as e: @@ -605,8 +610,10 @@ def save_reading_passage(): parts = data.get('parts') template = getReadingTemplate() template["parts"] = parts - if save_to_db("reading", template): - return template + + (result, id) = save_to_db("reading", template) + if result: + return {**template, "id": id} else: raise Exception("Failed to save reading: " + template) except Exception as e: