From 101bc20923e208c0bd2bf482d843cdc34bcdd52d Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 27 Jun 2021 11:55:08 +0200 Subject: [PATCH] extract update profile handler handler --- ki/handlers/__init__.py | 1 + ki/handlers/update_profile.py | 25 +++++++++++++++++++++++++ ki/routes.py | 24 ++++-------------------- 3 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 ki/handlers/__init__.py create mode 100644 ki/handlers/update_profile.py diff --git a/ki/handlers/__init__.py b/ki/handlers/__init__.py new file mode 100644 index 0000000..58e71f0 --- /dev/null +++ b/ki/handlers/__init__.py @@ -0,0 +1 @@ +from ki.handlers.update_profile import update_profile # noqa diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py new file mode 100644 index 0000000..d36dffa --- /dev/null +++ b/ki/handlers/update_profile.py @@ -0,0 +1,25 @@ +from flask import make_response, request + +from ki.models import User, Profile +from app import db + + +def update_profile(user_id): + user = User.query.filter(User.id == int(user_id)).first() + + if user is None: + return make_response({}, 404) + + profile = user.profile + + if (profile is None): + profile = Profile(user=user, nickname=user.auth_id) + db.session.add(profile) + + profile.pronouns = request.json.get("pronouns", "") + profile.volunteerwork = request.json.get("volunteerwork", "") + profile.freetext = request.json.get("freetext", "") + + db.session.commit() + + return make_response({"profile": profile.to_dict()}) diff --git a/ki/routes.py b/ki/routes.py index 1dea412..1a9c0b9 100644 --- a/ki/routes.py +++ b/ki/routes.py @@ -3,8 +3,9 @@ from flask import g, make_response, request, send_file from functools import wraps from ki.auth import auth -from ki.models import Language, Skill, Token, User, Profile -from app import app, db +from ki.handlers import update_profile as update_profile_handler +from ki.models import Language, Skill, Token, User +from app import app def token_auth(func): @@ -122,24 +123,7 @@ def get_user_profile(user_id): @app.route("/users//profile", methods=["POST"]) def update_profile(user_id): - user = User.query.filter(User.id == int(user_id)).first() - - if user is None: - return make_response({}, 404) - - profile = user.profile - - if (profile is None): - profile = Profile(user=user, nickname=user.auth_id) - db.session.add(profile) - - profile.pronouns = request.json.get("pronouns", "") - profile.volunteerwork = request.json.get("volunteerwork", "") - profile.freetext = request.json.get("freetext", "") - - db.session.commit() - - return make_response(profile.to_dict(), 200) + return update_profile_handler(user_id) @app.route("/skills")