FitTrackee/fittrackee/users/decorators.py

35 lines
918 B
Python
Raw Normal View History

2021-01-20 16:24:01 +01:00
from functools import wraps
from typing import Any, Callable, Union
from flask import request
2021-01-20 16:47:00 +01:00
from fittrackee.responses import HttpResponse
2021-01-20 16:24:01 +01:00
from .utils import verify_user
def authenticate(f: Callable) -> Callable:
@wraps(f)
def decorated_function(
*args: Any, **kwargs: Any
) -> Union[Callable, HttpResponse]:
response_object, user = verify_user(request, verify_admin=False)
2021-01-20 16:24:01 +01:00
if response_object:
return response_object
return f(user, *args, **kwargs)
2021-01-20 16:24:01 +01:00
return decorated_function
def authenticate_as_admin(f: Callable) -> Callable:
@wraps(f)
def decorated_function(
*args: Any, **kwargs: Any
) -> Union[Callable, HttpResponse]:
response_object, user = verify_user(request, verify_admin=True)
2021-01-20 16:24:01 +01:00
if response_object:
return response_object
return f(user, *args, **kwargs)
2021-01-20 16:24:01 +01:00
return decorated_function