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 flask import Blueprint, current_app, request
|
||||||
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
|
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 .models import AppConfig
|
||||||
from .utils import update_app_config_from_database
|
from .utils import update_app_config_from_database
|
||||||
|
|
||||||
|
@ -19,9 +19,9 @@ from werkzeug.exceptions import RequestEntityTooLarge
|
|||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
from ..workouts.utils_files import get_absolute_file_path
|
from ..workouts.utils_files import get_absolute_file_path
|
||||||
|
from .decorators import authenticate
|
||||||
from .models import User
|
from .models import User
|
||||||
from .utils import (
|
from .utils import (
|
||||||
authenticate,
|
|
||||||
check_passwords,
|
check_passwords,
|
||||||
display_readable_file_size,
|
display_readable_file_size,
|
||||||
get_readable_duration,
|
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 sqlalchemy import exc
|
||||||
|
|
||||||
from ..workouts.utils_files import get_absolute_file_path
|
from ..workouts.utils_files import get_absolute_file_path
|
||||||
|
from .decorators import authenticate, authenticate_as_admin
|
||||||
from .models import User, Workout
|
from .models import User, Workout
|
||||||
from .utils import authenticate, authenticate_as_admin
|
|
||||||
|
|
||||||
users_blueprint = Blueprint('users', __name__)
|
users_blueprint = Blueprint('users', __name__)
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from functools import wraps
|
from typing import Optional, Tuple, Union
|
||||||
from typing import Any, Callable, Optional, Tuple, Union
|
|
||||||
|
|
||||||
import humanize
|
import humanize
|
||||||
from fittrackee.responses import (
|
from fittrackee.responses import (
|
||||||
@ -11,7 +10,7 @@ from fittrackee.responses import (
|
|||||||
PayloadTooLargeErrorResponse,
|
PayloadTooLargeErrorResponse,
|
||||||
UnauthorizedErrorResponse,
|
UnauthorizedErrorResponse,
|
||||||
)
|
)
|
||||||
from flask import Request, current_app, request
|
from flask import Request, current_app
|
||||||
|
|
||||||
from .models import User
|
from .models import User
|
||||||
|
|
||||||
@ -130,34 +129,6 @@ def verify_user(
|
|||||||
return None, resp
|
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(
|
def can_view_workout(
|
||||||
auth_user_id: int, workout_user_id: int
|
auth_user_id: int, workout_user_id: int
|
||||||
) -> Optional[HttpResponse]:
|
) -> Optional[HttpResponse]:
|
||||||
|
@ -2,7 +2,7 @@ from typing import Dict
|
|||||||
|
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
|
|
||||||
from ..users.utils import authenticate
|
from ..users.decorators import authenticate
|
||||||
from .models import Record
|
from .models import Record
|
||||||
|
|
||||||
records_blueprint = Blueprint('records', __name__)
|
records_blueprint = Blueprint('records', __name__)
|
||||||
|
@ -10,8 +10,8 @@ from fittrackee.responses import (
|
|||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
from sqlalchemy import exc
|
from sqlalchemy import exc
|
||||||
|
|
||||||
|
from ..users.decorators import authenticate, authenticate_as_admin
|
||||||
from ..users.models import User
|
from ..users.models import User
|
||||||
from ..users.utils import authenticate, authenticate_as_admin
|
|
||||||
from .models import Sport
|
from .models import Sport
|
||||||
|
|
||||||
sports_blueprint = Blueprint('sports', __name__)
|
sports_blueprint = Blueprint('sports', __name__)
|
||||||
|
@ -12,8 +12,8 @@ from fittrackee.responses import (
|
|||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
|
||||||
|
from ..users.decorators import authenticate, authenticate_as_admin
|
||||||
from ..users.models import User
|
from ..users.models import User
|
||||||
from ..users.utils import authenticate, authenticate_as_admin
|
|
||||||
from .models import Sport, Workout
|
from .models import Sport, Workout
|
||||||
from .utils import get_datetime_with_tz, get_upload_dir_size
|
from .utils import get_datetime_with_tz, get_upload_dir_size
|
||||||
from .utils_format import convert_timedelta_to_integer
|
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 flask import Blueprint, Response, current_app, request, send_file
|
||||||
from sqlalchemy import exc
|
from sqlalchemy import exc
|
||||||
|
|
||||||
from ..users.utils import (
|
from ..users.decorators import authenticate
|
||||||
User,
|
from ..users.utils import User, can_view_workout, verify_extension_and_size
|
||||||
authenticate,
|
|
||||||
can_view_workout,
|
|
||||||
verify_extension_and_size,
|
|
||||||
)
|
|
||||||
from .models import Workout
|
from .models import Workout
|
||||||
from .utils import (
|
from .utils import (
|
||||||
WorkoutException,
|
WorkoutException,
|
||||||
|
Loading…
Reference in New Issue
Block a user