From 68b84f50ca7b1b1c4a69b320c351e8df4769eff7 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 27 Jun 2021 13:07:54 +0200 Subject: [PATCH] implement contacts update --- ki/actions/seed.py | 8 +++++++ ki/handlers/update_profile.py | 32 ++++++++++++++++++++++--- ki/routes.py | 2 +- ki/test/test_profile_endpoint.py | 40 +++++++++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/ki/actions/seed.py b/ki/actions/seed.py index 8be1889..f725ac4 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -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", diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index b6b1897..90463df 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -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() diff --git a/ki/routes.py b/ki/routes.py index f0f4622..58c1513 100644 --- a/ki/routes.py +++ b/ki/routes.py @@ -124,7 +124,7 @@ def get_user_profile(user_id): @app.route("/users//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") diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index bf23287..7e50bb8 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -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,