diff options
author | Jesús <heckyel@hyperbola.info> | 2022-03-22 00:39:40 +0800 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2022-03-22 00:39:40 +0800 |
commit | 3fca03988b42adaf8e67cc7137dd1fdba327e197 (patch) | |
tree | ad39a5bc12a03c82b9a4fd980c1b92ca0a1dac93 /utils | |
download | heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.tar.lz heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.tar.xz heroeapi-3fca03988b42adaf8e67cc7137dd1fdba327e197.zip |
initial commit
Diffstat (limited to 'utils')
-rw-r--r-- | utils/decorators.py | 22 | ||||
-rw-r--r-- | utils/errors.py | 155 |
2 files changed, 177 insertions, 0 deletions
diff --git a/utils/decorators.py b/utils/decorators.py new file mode 100644 index 0000000..3b12ead --- /dev/null +++ b/utils/decorators.py @@ -0,0 +1,22 @@ +""" +Decorators +""" +# from config import only_get +from utils.errors import error_method +from database.models import UserModel +from flask_jwt_extended import get_jwt_identity + + +def superuser(func): + def wrapper(*args, **kwargs): + """Check if user is admin""" + user_id = get_jwt_identity() + user_admin = UserModel.objects(id=user_id, admin=True) + if user_admin: + return func(*args, **kwargs) + else: + response = error_method() + return response + + wrapper.__name__ = func.__name__ + return wrapper diff --git a/utils/errors.py b/utils/errors.py new file mode 100644 index 0000000..ab6f1d5 --- /dev/null +++ b/utils/errors.py @@ -0,0 +1,155 @@ +""" +Code Errors reference: + - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + - https://developer.mozilla.org/es/docs/Web/HTTP/Status +""" +import json +from flask import ( + Response +) + + +def internal_server_error(e): + response = Response( + response=json.dumps({ + "msg": "Something went wrong", + "_desc": str(e) + }), + status=500, + mimetype="applications/json" + ) + return response + + +def schema_validation_error(e): + response = Response( + response=json.dumps({ + "msg": "Request is missing required fields", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def unauthorized_error(e): + response = Response( + response=json.dumps({ + "msg": "Invalid username or password", + "_desc": str(e) + }), + status=401, + mimetype="applications/json" + ) + return response + + +def disable_register_error(e): + response = Response( + response=json.dumps({ + "msg": "Unauthorized create user", + "_desc": str(e) + }), + status=401, + mimetype="applications/json" + ) + return response + + +def error_method(): + response = Response( + response=json.dumps({ + "msg": "Method unauthorized for your user" + }), + status=401, + mimetype="applications/json" + ) + return response + + +# Status 204 does not show any msg +def not_data_found(): + response = Response( + status=204, + mimetype="applications/json" + ) + return response + + +# ##### +# Heroe +# ##### +def heroe_already_exists_error(e): + response = Response( + response=json.dumps({ + "msg": "Heroe with given full-name already exists", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def updating_heroe_error(e): + response = Response( + response=json.dumps({ + "msg": "Updating heroe added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +def deleting_heroe_error(e): + response = Response( + response=json.dumps({ + "msg": "Deleting heroe added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +# ##### +# User +# ##### +def user_already_exists_error(e): + response = Response( + response=json.dumps({ + "msg": "User with given username or email already exists", + "_desc": str(e) + }), + status=400, + mimetype="applications/json" + ) + return response + + +def updating_user_error(e): + response = Response( + response=json.dumps({ + "msg": "Updating user added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response + + +def deleting_user_error(e): + response = Response( + response=json.dumps({ + "msg": "Deleting user added by other is forbidden", + "_desc": str(e) + }), + status=403, + mimetype="applications/json" + ) + return response |