From 352c6e54372108192e1d1f7786225fbb03b1dc38 Mon Sep 17 00:00:00 2001 From: sondrefj Date: Tue, 16 Jul 2024 10:07:18 +0200 Subject: [PATCH] feat: add translation from norwegian label to english label --- .gitignore | 1 + src/webapp/api.py | 5 +++++ src/webapp/models.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/.gitignore b/.gitignore index bff0532..e7c4eb3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ venv config.json src/config.json .idea +.venv diff --git a/src/webapp/api.py b/src/webapp/api.py index b61b024..9e8999a 100644 --- a/src/webapp/api.py +++ b/src/webapp/api.py @@ -203,7 +203,12 @@ def get_example_drawings(json_data): data = json.loads(json_data) game_id = data["game_id"] number_of_images = data["number_of_images"] + label = data["label"] + lang = data["lang"] + if (lang == "NO"): + label = models.to_english(label) + example_drawings = storage.get_n_random_images_from_label( number_of_images, label) emit("getExampleDrawings", json.dumps(example_drawings), room=game_id) diff --git a/src/webapp/models.py b/src/webapp/models.py index 0e41a02..c631a22 100644 --- a/src/webapp/models.py +++ b/src/webapp/models.py @@ -480,6 +480,20 @@ def to_norwegian(english_label): ) +def to_english(norwegian_label): + """ + Reads the labels table and return the english translation of the norwegian label + """ + try: + query = Labels.query.filter(Labels.norwegian == norwegian_label)[0] + return str(query.english) + + except AttributeError as e: + raise AttributeError( + "Could not find translation in Labels table: " + str(e) + ) + + def seed_labels(app, filepath): """ Function for updating labels in database.