dashboard/areas/users/users.py
Davor 5206c78998 MR comments
- fixed order of import in user_service.py
- added error handling for user delete
2022-05-26 09:49:25 +00:00

60 lines
1.4 KiB
Python

from flask import jsonify, request
from flask_jwt_extended import jwt_required
from flask_cors import cross_origin
from flask_expects_json import expects_json
from areas import api_v1
from helpers import KratosApi
from .validation import schema
from .user_service import UserService
@api_v1.route("/users", methods=["GET"])
@jwt_required()
@cross_origin()
def get_users():
res = UserService.get_users()
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["GET"])
@jwt_required()
@cross_origin()
def get_user(id):
res = UserService.get_user(id)
return jsonify(res)
@api_v1.route("/users", methods=["POST"])
@jwt_required()
@cross_origin()
@expects_json(schema)
def post_user():
data = request.get_json()
res = UserService.post_user(data)
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["PUT"])
@jwt_required()
@cross_origin()
@expects_json(schema)
def put_user(id):
data = request.get_json()
res = UserService.put_user(id, data)
return jsonify(res)
@api_v1.route("/users/<string:id>", methods=["DELETE"])
@jwt_required()
@cross_origin()
def delete_user(id):
try:
res = KratosApi.delete("/identities/{}".format(id))
if res.status_code == 204:
UserService.delete_user(id)
return jsonify(), res.status_code
return jsonify(res.json()), res.status_code
except:
return jsonify({"message":"There was an error deleting user"}), 404