Made it so the save_to_db also returns the ID of the document

This commit is contained in:
Tiago Ribeiro
2023-11-29 14:55:40 +00:00
parent 869e74f384
commit 4a0ae88fed
2 changed files with 14 additions and 10 deletions

20
app.py
View File

@@ -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:

View File

@@ -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)