From 54a66864740d8149c15f608cc978c55f3bcdee26 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 27 Jun 2021 12:20:36 +0200 Subject: [PATCH] implement address update --- ki/handlers/update_profile.py | 20 +++++++++++++++++++- ki/routes.py | 1 + ki/test/test_profile_endpoint.py | 12 ++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index d36dffa..b6b1897 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -1,9 +1,25 @@ from flask import make_response, request -from ki.models import User, Profile +from ki.models import Address, User, Profile from app import db +def update_address(profile, address_data): + address = profile.address + + if (address is None): + address = Address(profile=profile) + db.session.add(address) + + address.name = address_data.get("name", "") + address.street = address_data.get("street", "") + address.house_number = address_data.get("house_number", "") + address.additional = address_data.get("additional", "") + address.postcode = address_data.get("postcode", "") + address.city = address_data.get("city", "") + address.country = address_data.get("country", "") + + def update_profile(user_id): user = User.query.filter(User.id == int(user_id)).first() @@ -20,6 +36,8 @@ def update_profile(user_id): profile.volunteerwork = request.json.get("volunteerwork", "") profile.freetext = request.json.get("freetext", "") + update_address(profile, request.json.get("address", {})) + db.session.commit() return make_response({"profile": profile.to_dict()}) diff --git a/ki/routes.py b/ki/routes.py index 1a9c0b9..f0f4622 100644 --- a/ki/routes.py +++ b/ki/routes.py @@ -122,6 +122,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) diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 9591795..bf23287 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -39,6 +39,15 @@ class TestProfileEndpoint(unittest.TestCase): "pronouns": "Monsieur", "volunteerwork": "ja", "freetext": "Hallo", + "address": { + "name": "Peeeda", + "street": "Bachstraße", + "house_number": "42x", + "additional": "oben", + "postcode": "23232", + "city": "Travemünde", + "country": "Deutschland" + } } response = self.client.post("/users/1/profile", data=json.dumps(data), @@ -56,6 +65,9 @@ class TestProfileEndpoint(unittest.TestCase): self.assertEqual("ja", profile.volunteerwork) self.assertEqual("Hallo", profile.freetext) + address = profile.address + self.assertEqual(address.name, "Peeeda") + def test_get_profile(self): login_data = {"username": "peter", "password": "geheim"} login_response = self.client.post("/users/login",