From 9700b98af1524143c9f32105be6f7ccb194759d3 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 16 Feb 2022 12:55:55 +0100 Subject: [PATCH] API - move file utils --- fittrackee/files.py | 23 +++++++++++++++++++++++ fittrackee/responses.py | 16 +--------------- fittrackee/tests/test_utils.py | 2 +- fittrackee/users/auth.py | 2 +- fittrackee/users/users.py | 2 +- fittrackee/workouts/models.py | 2 +- fittrackee/workouts/utils.py | 2 +- fittrackee/workouts/utils_files.py | 7 ------- 8 files changed, 29 insertions(+), 27 deletions(-) create mode 100644 fittrackee/files.py delete mode 100644 fittrackee/workouts/utils_files.py diff --git a/fittrackee/files.py b/fittrackee/files.py new file mode 100644 index 00000000..9b691798 --- /dev/null +++ b/fittrackee/files.py @@ -0,0 +1,23 @@ +import os +from typing import Union + +from flask import current_app + + +def display_readable_file_size(size_in_bytes: Union[float, int]) -> str: + """ + Return readable file size from size in bytes + """ + if size_in_bytes == 0: + return '0 bytes' + if size_in_bytes == 1: + return '1 byte' + for unit in [' bytes', 'KB', 'MB', 'GB', 'TB']: + if abs(size_in_bytes) < 1024.0: + return f'{size_in_bytes:3.1f}{unit}' + size_in_bytes /= 1024.0 + return f'{size_in_bytes} bytes' + + +def get_absolute_file_path(relative_path: str) -> str: + return os.path.join(current_app.config['UPLOAD_FOLDER'], relative_path) diff --git a/fittrackee/responses.py b/fittrackee/responses.py index a3fbef3c..430c34eb 100644 --- a/fittrackee/responses.py +++ b/fittrackee/responses.py @@ -5,27 +5,13 @@ from flask import Response from flask_sqlalchemy import SQLAlchemy from fittrackee import appLog +from fittrackee.files import display_readable_file_size def get_empty_data_for_datatype(data_type: str) -> Union[str, List]: return '' if data_type in ['gpx', 'chart_data'] else [] -def display_readable_file_size(size_in_bytes: Union[float, int]) -> str: - """ - Return readable file size from size in bytes - """ - if size_in_bytes == 0: - return '0 bytes' - if size_in_bytes == 1: - return '1 byte' - for unit in [' bytes', 'KB', 'MB', 'GB', 'TB']: - if abs(size_in_bytes) < 1024.0: - return f'{size_in_bytes:3.1f}{unit}' - size_in_bytes /= 1024.0 - return f'{size_in_bytes} bytes' - - class HttpResponse(Response): def __init__( self, diff --git a/fittrackee/tests/test_utils.py b/fittrackee/tests/test_utils.py index dad80141..b0a2069a 100644 --- a/fittrackee/tests/test_utils.py +++ b/fittrackee/tests/test_utils.py @@ -3,7 +3,7 @@ from uuid import uuid4 import pytest -from fittrackee.responses import display_readable_file_size +from fittrackee.files import display_readable_file_size from fittrackee.utils import get_readable_duration diff --git a/fittrackee/users/auth.py b/fittrackee/users/auth.py index 48b7fb9e..98bd49a3 100644 --- a/fittrackee/users/auth.py +++ b/fittrackee/users/auth.py @@ -10,6 +10,7 @@ from werkzeug.exceptions import RequestEntityTooLarge from werkzeug.utils import secure_filename from fittrackee import appLog, bcrypt, db +from fittrackee.files import get_absolute_file_path from fittrackee.responses import ( ForbiddenErrorResponse, HttpResponse, @@ -22,7 +23,6 @@ from fittrackee.responses import ( from fittrackee.tasks import reset_password_email from fittrackee.utils import get_readable_duration, verify_extension_and_size from fittrackee.workouts.models import Sport -from fittrackee.workouts.utils_files import get_absolute_file_path from .decorators import authenticate from .models import User, UserSportPreference diff --git a/fittrackee/users/users.py b/fittrackee/users/users.py index 9c556830..4fc4ee93 100644 --- a/fittrackee/users/users.py +++ b/fittrackee/users/users.py @@ -7,6 +7,7 @@ from flask import Blueprint, request, send_file from sqlalchemy import exc from fittrackee import db +from fittrackee.files import get_absolute_file_path from fittrackee.responses import ( ForbiddenErrorResponse, HttpResponse, @@ -16,7 +17,6 @@ from fittrackee.responses import ( handle_error_and_return_response, ) from fittrackee.workouts.models import Record, Workout, WorkoutSegment -from fittrackee.workouts.utils_files import get_absolute_file_path from .decorators import authenticate, authenticate_as_admin from .exceptions import UserNotFoundException diff --git a/fittrackee/workouts/models.py b/fittrackee/workouts/models.py index 60c944d3..083c88a4 100644 --- a/fittrackee/workouts/models.py +++ b/fittrackee/workouts/models.py @@ -13,8 +13,8 @@ from sqlalchemy.orm.session import Session, object_session from sqlalchemy.types import JSON, Enum from fittrackee import db +from fittrackee.files import get_absolute_file_path -from .utils_files import get_absolute_file_path from .utils_format import convert_in_duration, convert_value_to_integer from .utils_id import encode_uuid diff --git a/fittrackee/workouts/utils.py b/fittrackee/workouts/utils.py index 033be370..03243263 100644 --- a/fittrackee/workouts/utils.py +++ b/fittrackee/workouts/utils.py @@ -15,11 +15,11 @@ from werkzeug.datastructures import FileStorage from werkzeug.utils import secure_filename from fittrackee import db +from fittrackee.files import get_absolute_file_path from fittrackee.users.models import User, UserSportPreference from .exceptions import WorkoutException from .models import Sport, Workout, WorkoutSegment -from .utils_files import get_absolute_file_path from .utils_gpx import get_gpx_info diff --git a/fittrackee/workouts/utils_files.py b/fittrackee/workouts/utils_files.py deleted file mode 100644 index 64bb6c82..00000000 --- a/fittrackee/workouts/utils_files.py +++ /dev/null @@ -1,7 +0,0 @@ -import os - -from flask import current_app - - -def get_absolute_file_path(relative_path: str) -> str: - return os.path.join(current_app.config['UPLOAD_FOLDER'], relative_path)