implement contacts update
This commit is contained in:
parent
54a6686474
commit
68b84f50ca
@ -55,6 +55,14 @@ def seed(dev: bool):
|
||||
content="@peter:wtf-eg.de")
|
||||
db.session.add(matrix_contact)
|
||||
|
||||
email_type = ContactType(name="E-Mail")
|
||||
db.session.add(email_type)
|
||||
|
||||
email_contact = Contact(profile=peters_profile,
|
||||
contacttype=email_type,
|
||||
content="peter@wtf-eg.de")
|
||||
db.session.add(email_contact)
|
||||
|
||||
peters_address = Address(name="Peter Nichtlustig",
|
||||
street="Waldweg",
|
||||
house_number="23i",
|
||||
|
@ -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()
|
||||
|
||||
|
@ -124,7 +124,7 @@ def get_user_profile(user_id):
|
||||
@app.route("/users/<user_id>/profile", methods=["POST"])
|
||||
@token_auth
|
||||
def update_profile(user_id):
|
||||
return update_profile_handler(user_id)
|
||||
return update_profile_handler(int(user_id))
|
||||
|
||||
|
||||
@app.route("/skills")
|
||||
|
@ -47,7 +47,20 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
"postcode": "23232",
|
||||
"city": "Travemünde",
|
||||
"country": "Deutschland"
|
||||
}
|
||||
},
|
||||
"contacts": [{
|
||||
"id": 1,
|
||||
"contacttype": {
|
||||
"id": 1,
|
||||
"name": "Matrix"
|
||||
},
|
||||
"content": "@peeda:wtf-eg.de"
|
||||
}, {
|
||||
"contacttype": {
|
||||
"name": "Rohrpost"
|
||||
},
|
||||
"content": "Ausgang 2"
|
||||
}]
|
||||
}
|
||||
response = self.client.post("/users/1/profile",
|
||||
data=json.dumps(data),
|
||||
@ -67,6 +80,23 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
|
||||
address = profile.address
|
||||
self.assertEqual(address.name, "Peeeda")
|
||||
self.assertEqual(address.street, "Bachstraße")
|
||||
self.assertEqual(address.house_number, "42x")
|
||||
self.assertEqual(address.additional, "oben")
|
||||
self.assertEqual(address.postcode, "23232")
|
||||
self.assertEqual(address.city, "Travemünde")
|
||||
self.assertEqual(address.country, "Deutschland")
|
||||
|
||||
contacts = profile.contacts
|
||||
self.assertEqual(len(contacts), 2)
|
||||
|
||||
first_contact = contacts[0]
|
||||
self.assertEqual(first_contact.contacttype.name, "Matrix")
|
||||
self.assertEqual(first_contact.content, "@peeda:wtf-eg.de")
|
||||
|
||||
second_contact = contacts[1]
|
||||
self.assertEqual(second_contact.contacttype.name, "Rohrpost")
|
||||
self.assertEqual(second_contact.content, "Ausgang 2")
|
||||
|
||||
def test_get_profile(self):
|
||||
login_data = {"username": "peter", "password": "geheim"}
|
||||
@ -111,6 +141,14 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
"name": "Matrix"
|
||||
},
|
||||
"content": "@peter:wtf-eg.de"
|
||||
}, {
|
||||
"id": 2,
|
||||
"profile_id": 1,
|
||||
"contacttype": {
|
||||
"id": 2,
|
||||
"name": "E-Mail"
|
||||
},
|
||||
"content": "peter@wtf-eg.de"
|
||||
}],
|
||||
"skills": [{
|
||||
"profile_id": 1,
|
||||
|
Loading…
Reference in New Issue
Block a user