implement contacts update

This commit is contained in:
weeman 2021-06-27 13:07:54 +02:00
parent 54a6686474
commit 68b84f50ca
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
4 changed files with 77 additions and 5 deletions

View File

@ -55,6 +55,14 @@ def seed(dev: bool):
content="@peter:wtf-eg.de") content="@peter:wtf-eg.de")
db.session.add(matrix_contact) 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", peters_address = Address(name="Peter Nichtlustig",
street="Waldweg", street="Waldweg",
house_number="23i", house_number="23i",

View File

@ -1,6 +1,6 @@
from flask import make_response, request 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 from app import db
@ -20,8 +20,33 @@ def update_address(profile, address_data):
address.country = address_data.get("country", "") address.country = address_data.get("country", "")
def update_profile(user_id): def update_contacts(profile, contacts_data):
user = User.query.filter(User.id == int(user_id)).first() 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: if user is None:
return make_response({}, 404) return make_response({}, 404)
@ -37,6 +62,7 @@ def update_profile(user_id):
profile.freetext = request.json.get("freetext", "") profile.freetext = request.json.get("freetext", "")
update_address(profile, request.json.get("address", {})) update_address(profile, request.json.get("address", {}))
update_contacts(profile, request.json.get("contacts", {}))
db.session.commit() db.session.commit()

View File

@ -124,7 +124,7 @@ def get_user_profile(user_id):
@app.route("/users/<user_id>/profile", methods=["POST"]) @app.route("/users/<user_id>/profile", methods=["POST"])
@token_auth @token_auth
def update_profile(user_id): def update_profile(user_id):
return update_profile_handler(user_id) return update_profile_handler(int(user_id))
@app.route("/skills") @app.route("/skills")

View File

@ -47,7 +47,20 @@ class TestProfileEndpoint(unittest.TestCase):
"postcode": "23232", "postcode": "23232",
"city": "Travemünde", "city": "Travemünde",
"country": "Deutschland" "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", response = self.client.post("/users/1/profile",
data=json.dumps(data), data=json.dumps(data),
@ -67,6 +80,23 @@ class TestProfileEndpoint(unittest.TestCase):
address = profile.address address = profile.address
self.assertEqual(address.name, "Peeeda") 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): def test_get_profile(self):
login_data = {"username": "peter", "password": "geheim"} login_data = {"username": "peter", "password": "geheim"}
@ -111,6 +141,14 @@ class TestProfileEndpoint(unittest.TestCase):
"name": "Matrix" "name": "Matrix"
}, },
"content": "@peter:wtf-eg.de" "content": "@peter:wtf-eg.de"
}, {
"id": 2,
"profile_id": 1,
"contacttype": {
"id": 2,
"name": "E-Mail"
},
"content": "peter@wtf-eg.de"
}], }],
"skills": [{ "skills": [{
"profile_id": 1, "profile_id": 1,