implement updating skills

This commit is contained in:
2021-06-27 13:38:16 +02:00
parent 68b84f50ca
commit 15b71459ee
7 changed files with 85 additions and 65 deletions

View File

@ -1,6 +1,7 @@
from flask import make_response, request
from sqlalchemy import not_
from ki.models import Address, Contact, ContactType, User, Profile
from ki.models import Address, Contact, ContactType, User, Profile, ProfileSkill, Skill
from app import db
@ -20,15 +21,39 @@ def update_address(profile, address_data):
address.country = address_data.get("country", "")
def update_skills(profile, skills_data):
profile_skill_ids = []
for skill_data in skills_data:
skill_name = skill_data["skill"]["name"]
skill = Skill.query.filter(Skill.name == skill_name).first()
if (skill is None):
skill = Skill(name=skill_name)
db.session.add(skill)
profile_skill = ProfileSkill.query.filter(ProfileSkill.profile == profile, ProfileSkill.skill == skill).first()
if (profile_skill is None):
profile_skill = ProfileSkill(profile=profile, skill=skill)
db.session.add(profile_skill)
profile_skill.level = skill_data["level"]
profile_skill_ids.append(skill.id)
ProfileSkill.query.filter(ProfileSkill.profile == profile,
not_(ProfileSkill.skill_id.in_(profile_skill_ids))).delete()
def update_contacts(profile, contacts_data):
contact_ids_to_be_deleted = list(map(lambda c: c.id, profile.contacts))
for contact_data in contacts_data:
contacttype = ContactType.query.filter(
ContactType.name == contact_data["contacttype"]["name"]).first()
contacttype_name = contact_data["contacttype"]["name"]
contacttype = ContactType.query.filter(ContactType.name == contacttype_name).first()
if (contacttype is None):
contacttype = ContactType(name=contact_data["contacttype"]["name"])
contacttype = ContactType(name=contacttype_name)
db.session.add(contacttype)
if "id" in contact_data:
@ -63,6 +88,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", {}))
db.session.commit()