fix creating languages #56

This commit is contained in:
weeman 2021-11-22 21:13:33 +01:00
parent 111d4f08f4
commit 733499303f
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1

View File

@ -30,19 +30,25 @@ def update_languages(profile, languages_data):
profile_language_ids = [] profile_language_ids = []
for language_data in languages_data: for language_data in languages_data:
language_id = language_data["language"]["id"] if "id" in language_data["language"]:
language = Language.query.get(language_id) 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, profile_language = ProfileLanguage.query.filter(ProfileLanguage.profile == profile,
ProfileLanguage.language_id == language_id).first() ProfileLanguage.language == language).first()
if profile_language is None: if profile_language is None:
profile_language = ProfileLanguage(profile=profile, language=language) profile_language = ProfileLanguage(profile=profile, language=language)
db.session.add(profile_language) db.session.add(profile_language)
profile_language.level = language_data["level"] profile_language.level = language_data["level"]
profile_language_ids.append(language.id)
profile_language_ids.append(language_id)
ProfileLanguage.query.filter(ProfileLanguage.profile == profile, ProfileLanguage.query.filter(ProfileLanguage.profile == profile,
not_(ProfileLanguage.language_id.in_(profile_language_ids))).delete() not_(ProfileLanguage.language_id.in_(profile_language_ids))).delete()