From 733499303f44fbfb3940f71d54554263aa45c30f Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 22 Nov 2021 21:13:33 +0100 Subject: [PATCH] fix creating languages #56 --- ki/handlers/update_profile.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index 06d107b..8fd0d9e 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -30,19 +30,25 @@ def update_languages(profile, languages_data): profile_language_ids = [] for language_data in languages_data: - language_id = language_data["language"]["id"] - language = Language.query.get(language_id) + if "id" in language_data["language"]: + language = Language.query.get(language_data["language"]["id"]) + else: + language_name = language_data["language"]["name"] + language = Language.query.filter(Language.name == language_name).first() + + if language is None: + language = Language(id=language_name, name=language_name) + db.session.add(language) profile_language = ProfileLanguage.query.filter(ProfileLanguage.profile == profile, - ProfileLanguage.language_id == language_id).first() + ProfileLanguage.language == language).first() if profile_language is None: profile_language = ProfileLanguage(profile=profile, language=language) db.session.add(profile_language) profile_language.level = language_data["level"] - - profile_language_ids.append(language_id) + profile_language_ids.append(language.id) ProfileLanguage.query.filter(ProfileLanguage.profile == profile, not_(ProfileLanguage.language_id.in_(profile_language_ids))).delete()