extract update profile handler handler

This commit is contained in:
weeman 2021-06-27 11:55:08 +02:00
parent 3bed7222ec
commit 101bc20923
Signed by: weeman
GPG Key ID: 34F0524D4DA694A1
3 changed files with 30 additions and 20 deletions

1
ki/handlers/__init__.py Normal file
View File

@ -0,0 +1 @@
from ki.handlers.update_profile import update_profile # noqa

View File

@ -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()})

View File

@ -3,8 +3,9 @@ from flask import g, make_response, request, send_file
from functools import wraps from functools import wraps
from ki.auth import auth from ki.auth import auth
from ki.models import Language, Skill, Token, User, Profile from ki.handlers import update_profile as update_profile_handler
from app import app, db from ki.models import Language, Skill, Token, User
from app import app
def token_auth(func): def token_auth(func):
@ -122,24 +123,7 @@ def get_user_profile(user_id):
@app.route("/users/<user_id>/profile", methods=["POST"]) @app.route("/users/<user_id>/profile", methods=["POST"])
def update_profile(user_id): def update_profile(user_id):
user = User.query.filter(User.id == int(user_id)).first() return update_profile_handler(user_id)
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)
@app.route("/skills") @app.route("/skills")