implement languages update
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
weeman 2021-06-27 13:51:07 +02:00
parent 15b71459ee
commit 5113f6995e
Signed by: weeman
GPG Key ID: 34F0524D4DA694A1
2 changed files with 62 additions and 1 deletions

View File

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

View File

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