From 5113f6995e07061d98d123c41ff9056562e4b644 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 27 Jun 2021 13:51:07 +0200 Subject: [PATCH] implement languages update --- ki/handlers/update_profile.py | 25 ++++++++++++++++++++- ki/test/test_profile_endpoint.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index 3bfebcd..d69ea5d 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -1,7 +1,7 @@ from flask import make_response, request from sqlalchemy import not_ -from ki.models import Address, Contact, ContactType, User, Profile, ProfileSkill, Skill +from ki.models import Address, Contact, ContactType, Language, User, Profile, ProfileLanguage, ProfileSkill, Skill from app import db @@ -21,6 +21,28 @@ def update_address(profile, address_data): address.country = address_data.get("country", "") +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) + + profile_language = ProfileLanguage.query.filter(ProfileLanguage.profile == profile, + ProfileLanguage.language_id == language_id).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) + + ProfileLanguage.query.filter(ProfileLanguage.profile == profile, + not_(ProfileLanguage.language_id.in_(profile_language_ids))).delete() + + def update_skills(profile, skills_data): profile_skill_ids = [] @@ -89,6 +111,7 @@ def update_profile(user_id: int): update_address(profile, request.json.get("address", {})) update_contacts(profile, request.json.get("contacts", {})) update_skills(profile, request.json.get("skills", {})) + update_languages(profile, request.json.get("languages", {})) db.session.commit() diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 6a40bdf..f58fda7 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -71,6 +71,20 @@ class TestProfileEndpoint(unittest.TestCase): "name": "Tschunkproduktion" }, "level": 5 + }], + "languages": [{ + "id": 1, + "language": { + "id": "de", + "name": "Deutsch" + }, + "level": 4 + }, { + "language": { + "id": "es", + "name": "Spanisch" + }, + "level": 2 }] } response = self.client.post("/users/1/profile", @@ -106,6 +120,30 @@ class TestProfileEndpoint(unittest.TestCase): self.assertEqual(second_contact.contacttype.name, "Rohrpost") self.assertEqual(second_contact.content, "Ausgang 2") + skills = profile.skills + self.assertEqual(len(skills), 2) + + first_skill = skills[0] + self.assertEqual(first_skill.skill.id, 3) + self.assertEqual(first_skill.skill.name, "Python") + self.assertEqual(first_skill.level, 4) + + second_skill = skills[1] + self.assertEqual(second_skill.skill.id, 13) + self.assertEqual(second_skill.skill.name, "Tschunkproduktion") + self.assertEqual(second_skill.level, 5) + + languages = profile.languages + self.assertEqual(len(languages), 2) + + first_language = languages[0] + self.assertEqual(first_language.language_id, "de") + self.assertEqual(first_language.level, 4) + + second_language = languages[1] + self.assertEqual(second_language.language_id, "es") + self.assertEqual(second_language.level, 2) + def test_get_profile(self): login_data = {"username": "peter", "password": "geheim"} login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")