forked from kompetenzinventar/ki-backend
implement contacts update
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
from flask import make_response, request
|
||||
|
||||
from ki.models import Address, User, Profile
|
||||
from ki.models import Address, Contact, ContactType, User, Profile
|
||||
from app import db
|
||||
|
||||
|
||||
@ -20,8 +20,33 @@ def update_address(profile, address_data):
|
||||
address.country = address_data.get("country", "")
|
||||
|
||||
|
||||
def update_profile(user_id):
|
||||
user = User.query.filter(User.id == int(user_id)).first()
|
||||
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()
|
||||
|
||||
if (contacttype is None):
|
||||
contacttype = ContactType(name=contact_data["contacttype"]["name"])
|
||||
db.session.add(contacttype)
|
||||
|
||||
if "id" in contact_data:
|
||||
contact_id = int(contact_data["id"])
|
||||
contact_ids_to_be_deleted.remove(contact_id)
|
||||
contact = Contact.query.get(contact_id)
|
||||
else:
|
||||
contact = Contact(profile=profile, contacttype=contacttype)
|
||||
db.session.add(contact)
|
||||
|
||||
contact.contacttype_id = contacttype.id
|
||||
contact.content = contact_data["content"]
|
||||
|
||||
Contact.query.filter(Contact.id.in_(contact_ids_to_be_deleted)).delete()
|
||||
|
||||
|
||||
def update_profile(user_id: int):
|
||||
user = User.query.get(user_id)
|
||||
|
||||
if user is None:
|
||||
return make_response({}, 404)
|
||||
@ -37,6 +62,7 @@ def update_profile(user_id):
|
||||
profile.freetext = request.json.get("freetext", "")
|
||||
|
||||
update_address(profile, request.json.get("address", {}))
|
||||
update_contacts(profile, request.json.get("contacts", {}))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
|
Reference in New Issue
Block a user