API - refactor user decorators
This commit is contained in:
parent
bc182442fc
commit
d0475eec9f
@ -9,7 +9,7 @@ from fittrackee.responses import (
|
||||
from flask import Blueprint, current_app, request
|
||||
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
|
||||
|
||||
from ..users.utils import authenticate_as_admin
|
||||
from ..users.decorators import authenticate_as_admin
|
||||
from .models import AppConfig
|
||||
from .utils import update_app_config_from_database
|
||||
|
||||
|
@ -19,9 +19,9 @@ from werkzeug.exceptions import RequestEntityTooLarge
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from ..workouts.utils_files import get_absolute_file_path
|
||||
from .decorators import authenticate
|
||||
from .models import User
|
||||
from .utils import (
|
||||
authenticate,
|
||||
check_passwords,
|
||||
display_readable_file_size,
|
||||
get_readable_duration,
|
||||
|
35
fittrackee/users/decorators.py
Normal file
35
fittrackee/users/decorators.py
Normal file
@ -0,0 +1,35 @@
|
||||
from functools import wraps
|
||||
from typing import Any, Callable, Union
|
||||
|
||||
from fittrackee.responses import HttpResponse
|
||||
from flask import request
|
||||
|
||||
from .utils import verify_user
|
||||
|
||||
|
||||
def authenticate(f: Callable) -> Callable:
|
||||
@wraps(f)
|
||||
def decorated_function(
|
||||
*args: Any, **kwargs: Any
|
||||
) -> Union[Callable, HttpResponse]:
|
||||
verify_admin = False
|
||||
response_object, resp = verify_user(request, verify_admin)
|
||||
if response_object:
|
||||
return response_object
|
||||
return f(resp, *args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
||||
|
||||
def authenticate_as_admin(f: Callable) -> Callable:
|
||||
@wraps(f)
|
||||
def decorated_function(
|
||||
*args: Any, **kwargs: Any
|
||||
) -> Union[Callable, HttpResponse]:
|
||||
verify_admin = True
|
||||
response_object, resp = verify_user(request, verify_admin)
|
||||
if response_object:
|
||||
return response_object
|
||||
return f(resp, *args, **kwargs)
|
||||
|
||||
return decorated_function
|
@ -15,8 +15,8 @@ from flask import Blueprint, request, send_file
|
||||
from sqlalchemy import exc
|
||||
|
||||
from ..workouts.utils_files import get_absolute_file_path
|
||||
from .decorators import authenticate, authenticate_as_admin
|
||||
from .models import User, Workout
|
||||
from .utils import authenticate, authenticate_as_admin
|
||||
|
||||
users_blueprint = Blueprint('users', __name__)
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
import re
|
||||
from datetime import timedelta
|
||||
from functools import wraps
|
||||
from typing import Any, Callable, Optional, Tuple, Union
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
import humanize
|
||||
from fittrackee.responses import (
|
||||
@ -11,7 +10,7 @@ from fittrackee.responses import (
|
||||
PayloadTooLargeErrorResponse,
|
||||
UnauthorizedErrorResponse,
|
||||
)
|
||||
from flask import Request, current_app, request
|
||||
from flask import Request, current_app
|
||||
|
||||
from .models import User
|
||||
|
||||
@ -130,34 +129,6 @@ def verify_user(
|
||||
return None, resp
|
||||
|
||||
|
||||
def authenticate(f: Callable) -> Callable:
|
||||
@wraps(f)
|
||||
def decorated_function(
|
||||
*args: Any, **kwargs: Any
|
||||
) -> Union[Callable, HttpResponse]:
|
||||
verify_admin = False
|
||||
response_object, resp = verify_user(request, verify_admin)
|
||||
if response_object:
|
||||
return response_object
|
||||
return f(resp, *args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
||||
|
||||
def authenticate_as_admin(f: Callable) -> Callable:
|
||||
@wraps(f)
|
||||
def decorated_function(
|
||||
*args: Any, **kwargs: Any
|
||||
) -> Union[Callable, HttpResponse]:
|
||||
verify_admin = True
|
||||
response_object, resp = verify_user(request, verify_admin)
|
||||
if response_object:
|
||||
return response_object
|
||||
return f(resp, *args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
||||
|
||||
def can_view_workout(
|
||||
auth_user_id: int, workout_user_id: int
|
||||
) -> Optional[HttpResponse]:
|
||||
|
@ -2,7 +2,7 @@ from typing import Dict
|
||||
|
||||
from flask import Blueprint
|
||||
|
||||
from ..users.utils import authenticate
|
||||
from ..users.decorators import authenticate
|
||||
from .models import Record
|
||||
|
||||
records_blueprint = Blueprint('records', __name__)
|
||||
|
@ -10,8 +10,8 @@ from fittrackee.responses import (
|
||||
from flask import Blueprint, request
|
||||
from sqlalchemy import exc
|
||||
|
||||
from ..users.decorators import authenticate, authenticate_as_admin
|
||||
from ..users.models import User
|
||||
from ..users.utils import authenticate, authenticate_as_admin
|
||||
from .models import Sport
|
||||
|
||||
sports_blueprint = Blueprint('sports', __name__)
|
||||
|
@ -12,8 +12,8 @@ from fittrackee.responses import (
|
||||
from flask import Blueprint, request
|
||||
from sqlalchemy import func
|
||||
|
||||
from ..users.decorators import authenticate, authenticate_as_admin
|
||||
from ..users.models import User
|
||||
from ..users.utils import authenticate, authenticate_as_admin
|
||||
from .models import Sport, Workout
|
||||
from .utils import get_datetime_with_tz, get_upload_dir_size
|
||||
from .utils_format import convert_timedelta_to_integer
|
||||
|
@ -18,12 +18,8 @@ from fittrackee.responses import (
|
||||
from flask import Blueprint, Response, current_app, request, send_file
|
||||
from sqlalchemy import exc
|
||||
|
||||
from ..users.utils import (
|
||||
User,
|
||||
authenticate,
|
||||
can_view_workout,
|
||||
verify_extension_and_size,
|
||||
)
|
||||
from ..users.decorators import authenticate
|
||||
from ..users.utils import User, can_view_workout, verify_extension_and_size
|
||||
from .models import Workout
|
||||
from .utils import (
|
||||
WorkoutException,
|
||||
|
Loading…
Reference in New Issue
Block a user