From 1697f6051f6110d84e57a04bf955e083d9db17c5 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 1 Dec 2021 19:44:57 +0100 Subject: [PATCH] API - minor refactoring --- fittrackee/users/decorators.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/fittrackee/users/decorators.py b/fittrackee/users/decorators.py index b3a841a5..6430071f 100644 --- a/fittrackee/users/decorators.py +++ b/fittrackee/users/decorators.py @@ -8,15 +8,22 @@ from fittrackee.responses import HttpResponse from .utils import verify_user +def verify_auth_user( + f: Callable, verify_admin: bool, *args: Any, **kwargs: Any +) -> Union[Callable, HttpResponse]: + response_object, user = verify_user(request, verify_admin=verify_admin) + if response_object: + return response_object + return f(user, *args, **kwargs) + + 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) - if response_object: - return response_object - return f(user, *args, **kwargs) + verify_admin = False + return verify_auth_user(f, verify_admin, *args, **kwargs) return decorated_function @@ -26,9 +33,7 @@ def authenticate_as_admin(f: Callable) -> Callable: def decorated_function( *args: Any, **kwargs: Any ) -> Union[Callable, HttpResponse]: - response_object, user = verify_user(request, verify_admin=True) - if response_object: - return response_object - return f(user, *args, **kwargs) + verify_admin = True + return verify_auth_user(f, verify_admin, *args, **kwargs) return decorated_function