API - refactor + typing errors fix

This commit is contained in:
Sam 2021-05-22 17:14:24 +02:00
parent 25bdb215cb
commit c902bdb911
7 changed files with 32 additions and 39 deletions

View File

@ -528,7 +528,7 @@ def edit_picture(auth_user_id: int) -> Union[Dict, HttpResponse]:
return response_object return response_object
file = request.files['file'] file = request.files['file']
filename = secure_filename(file.filename) filename = secure_filename(file.filename) # type: ignore
dirpath = os.path.join( dirpath = os.path.join(
current_app.config['UPLOAD_FOLDER'], 'pictures', str(auth_user_id) current_app.config['UPLOAD_FOLDER'], 'pictures', str(auth_user_id)
) )

View File

@ -122,12 +122,8 @@ def get_users(auth_user_id: int) -> Dict:
""" """
params = request.args.copy() params = request.args.copy()
page = 1 if 'page' not in params.keys() else int(params.get('page')) page = int(params.get('page', 1))
per_page = ( per_page = int(params.get('per_page', USER_PER_PAGE))
int(params.get('per_page'))
if params.get('per_page')
else USER_PER_PAGE
)
if per_page > 50: if per_page > 50:
per_page = 50 per_page = 50
order_by = params.get('order_by') order_by = params.get('order_by')
@ -364,7 +360,7 @@ def update_user(
:statuscode 500: :statuscode 500:
""" """
user_data = request.get_json() user_data = request.get_json()
if 'admin' not in user_data: if not user_data or user_data.get('admin') is None:
return InvalidPayloadErrorResponse() return InvalidPayloadErrorResponse()
try: try:

View File

@ -21,7 +21,7 @@ def verify_extension_and_size(
return InvalidPayloadErrorResponse('No file part.', 'fail') return InvalidPayloadErrorResponse('No file part.', 'fail')
file = req.files['file'] file = req.files['file']
if file.filename == '': if not file.filename or file.filename == '':
return InvalidPayloadErrorResponse('No selected file.', 'fail') return InvalidPayloadErrorResponse('No selected file.', 'fail')
allowed_extensions = ( allowed_extensions = (

View File

@ -16,7 +16,7 @@ from fittrackee.users.decorators import authenticate, authenticate_as_admin
from fittrackee.users.models import User from fittrackee.users.models import User
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_from_request_args, get_upload_dir_size
from .utils_format import convert_timedelta_to_integer from .utils_format import convert_timedelta_to_integer
stats_blueprint = Blueprint('stats', __name__) stats_blueprint = Blueprint('stats', __name__)
@ -34,16 +34,7 @@ def get_workouts(
return UserNotFoundErrorResponse() return UserNotFoundErrorResponse()
params = request.args.copy() params = request.args.copy()
date_from = params.get('from') date_from, date_to = get_datetime_from_request_args(params, user)
if date_from:
date_from = datetime.strptime(date_from, '%Y-%m-%d')
_, date_from = get_datetime_with_tz(user.timezone, date_from)
date_to = params.get('to')
if date_to:
date_to = datetime.strptime(
f'{date_to} 23:59:59', '%Y-%m-%d %H:%M:%S'
)
_, date_to = get_datetime_with_tz(user.timezone, date_to)
sport_id = params.get('sport_id') sport_id = params.get('sport_id')
time = params.get('time') time = params.get('time')

View File

@ -50,6 +50,25 @@ def get_datetime_with_tz(
return workout_date_tz, workout_date return workout_date_tz, workout_date
def get_datetime_from_request_args(
params: Dict, user: User
) -> Tuple[Optional[datetime], Optional[datetime]]:
date_from = None
date_to = None
date_from_str = params.get('from')
if date_from_str:
date_from = datetime.strptime(date_from_str, '%Y-%m-%d')
_, date_from = get_datetime_with_tz(user.timezone, date_from)
date_to_str = params.get('to')
if date_to_str:
date_to = datetime.strptime(
f'{date_to_str} 23:59:59', '%Y-%m-%d %H:%M:%S'
)
_, date_to = get_datetime_with_tz(user.timezone, date_to)
return date_from, date_to
def update_workout_data( def update_workout_data(
workout: Union[Workout, WorkoutSegment], gpx_data: Dict workout: Union[Workout, WorkoutSegment], gpx_data: Dict
) -> Union[Workout, WorkoutSegment]: ) -> Union[Workout, WorkoutSegment]:

View File

@ -1,7 +1,7 @@
import json import json
import os import os
import shutil import shutil
from datetime import datetime, timedelta from datetime import timedelta
from typing import Any, Dict, List, Optional, Tuple, Union from typing import Any, Dict, List, Optional, Tuple, Union
import requests import requests
@ -31,7 +31,7 @@ from .utils import (
create_workout, create_workout,
edit_workout, edit_workout,
get_absolute_file_path, get_absolute_file_path,
get_datetime_with_tz, get_datetime_from_request_args,
process_files, process_files,
) )
from .utils_format import convert_in_duration from .utils_format import convert_in_duration
@ -196,17 +196,8 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
try: try:
user = User.query.filter_by(id=auth_user_id).first() user = User.query.filter_by(id=auth_user_id).first()
params = request.args.copy() params = request.args.copy()
page = 1 if 'page' not in params.keys() else int(params.get('page')) page = int(params.get('page', 1))
date_from = params.get('from') date_from, date_to = get_datetime_from_request_args(params, user)
if date_from:
date_from = datetime.strptime(date_from, '%Y-%m-%d')
_, date_from = get_datetime_with_tz(user.timezone, date_from)
date_to = params.get('to')
if date_to:
date_to = datetime.strptime(
f'{date_to} 23:59:59', '%Y-%m-%d %H:%M:%S'
)
_, date_to = get_datetime_with_tz(user.timezone, date_to)
distance_from = params.get('distance_from') distance_from = params.get('distance_from')
distance_to = params.get('distance_to') distance_to = params.get('distance_to')
duration_from = params.get('duration_from') duration_from = params.get('duration_from')
@ -217,11 +208,7 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
max_speed_to = params.get('max_speed_to') max_speed_to = params.get('max_speed_to')
order = params.get('order') order = params.get('order')
sport_id = params.get('sport_id') sport_id = params.get('sport_id')
per_page = ( per_page = int(params.get('per_page', DEFAULT_WORKOUTS_PER_PAGE))
int(params.get('per_page'))
if params.get('per_page')
else DEFAULT_WORKOUTS_PER_PAGE
)
if per_page > MAX_WORKOUTS_PER_PAGE: if per_page > MAX_WORKOUTS_PER_PAGE:
per_page = MAX_WORKOUTS_PER_PAGE per_page = MAX_WORKOUTS_PER_PAGE
workouts = ( workouts = (