implement address update

This commit is contained in:
weeman 2021-06-27 12:20:36 +02:00
parent 101bc20923
commit 54a6686474
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
3 changed files with 32 additions and 1 deletions

View File

@ -1,9 +1,25 @@
from flask import make_response, request from flask import make_response, request
from ki.models import User, Profile from ki.models import Address, User, Profile
from app import db 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): def update_profile(user_id):
user = User.query.filter(User.id == int(user_id)).first() 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.volunteerwork = request.json.get("volunteerwork", "")
profile.freetext = request.json.get("freetext", "") profile.freetext = request.json.get("freetext", "")
update_address(profile, request.json.get("address", {}))
db.session.commit() db.session.commit()
return make_response({"profile": profile.to_dict()}) return make_response({"profile": profile.to_dict()})

View File

@ -122,6 +122,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
def update_profile(user_id): def update_profile(user_id):
return update_profile_handler(user_id) return update_profile_handler(user_id)

View File

@ -39,6 +39,15 @@ class TestProfileEndpoint(unittest.TestCase):
"pronouns": "Monsieur", "pronouns": "Monsieur",
"volunteerwork": "ja", "volunteerwork": "ja",
"freetext": "Hallo", "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", response = self.client.post("/users/1/profile",
data=json.dumps(data), data=json.dumps(data),
@ -56,6 +65,9 @@ class TestProfileEndpoint(unittest.TestCase):
self.assertEqual("ja", profile.volunteerwork) self.assertEqual("ja", profile.volunteerwork)
self.assertEqual("Hallo", profile.freetext) self.assertEqual("Hallo", profile.freetext)
address = profile.address
self.assertEqual(address.name, "Peeeda")
def test_get_profile(self): def test_get_profile(self):
login_data = {"username": "peter", "password": "geheim"} login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login", login_response = self.client.post("/users/login",