API - workouts refactoring
This commit is contained in:
@ -15,8 +15,8 @@ from sqlalchemy.types import JSON, Enum
|
||||
from fittrackee import db
|
||||
from fittrackee.files import get_absolute_file_path
|
||||
|
||||
from .utils_format import convert_in_duration, convert_value_to_integer
|
||||
from .utils_id import encode_uuid
|
||||
from .utils.convert import convert_in_duration, convert_value_to_integer
|
||||
from .utils.short_id import encode_uuid
|
||||
|
||||
BaseModel: DeclarativeMeta = db.Model
|
||||
record_types = [
|
||||
|
@ -16,12 +16,9 @@ from fittrackee.users.decorators import authenticate, authenticate_as_admin
|
||||
from fittrackee.users.models import User
|
||||
|
||||
from .models import Sport, Workout
|
||||
from .utils import (
|
||||
get_average_speed,
|
||||
get_datetime_from_request_args,
|
||||
get_upload_dir_size,
|
||||
)
|
||||
from .utils_format import convert_timedelta_to_integer
|
||||
from .utils.convert import convert_timedelta_to_integer
|
||||
from .utils.uploads import get_upload_dir_size
|
||||
from .utils.workouts import get_average_speed, get_datetime_from_request_args
|
||||
|
||||
stats_blueprint = Blueprint('stats', __name__)
|
||||
|
||||
|
0
fittrackee/workouts/utils/__init__.py
Normal file
0
fittrackee/workouts/utils/__init__.py
Normal file
@ -3,8 +3,8 @@ from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import gpxpy.gpx
|
||||
|
||||
from .exceptions import WorkoutGPXException
|
||||
from .utils_weather import get_weather
|
||||
from ..exceptions import WorkoutGPXException
|
||||
from .weather import get_weather
|
||||
|
||||
|
||||
def open_gpx_file(gpx_file: str) -> Optional[gpxpy.gpx.GPX]:
|
35
fittrackee/workouts/utils/maps.py
Normal file
35
fittrackee/workouts/utils/maps.py
Normal file
@ -0,0 +1,35 @@
|
||||
import hashlib
|
||||
from typing import List
|
||||
|
||||
from flask import current_app
|
||||
from staticmap import Line, StaticMap
|
||||
|
||||
from fittrackee.files import get_absolute_file_path
|
||||
|
||||
|
||||
def generate_map(map_filepath: str, map_data: List) -> None:
|
||||
"""
|
||||
Generate and save map image from map data
|
||||
"""
|
||||
m = StaticMap(400, 225, 10)
|
||||
if not current_app.config['TILE_SERVER']['DEFAULT_STATICMAP']:
|
||||
m.url_template = current_app.config['TILE_SERVER']['URL'].replace(
|
||||
'{s}.', ''
|
||||
)
|
||||
line = Line(map_data, '#3388FF', 4)
|
||||
m.add_line(line)
|
||||
image = m.render()
|
||||
image.save(map_filepath)
|
||||
|
||||
|
||||
def get_map_hash(map_filepath: str) -> str:
|
||||
"""
|
||||
Generate a md5 hash used as id instead of workout id, to retrieve map
|
||||
image (maps are sensitive data)
|
||||
"""
|
||||
md5 = hashlib.md5()
|
||||
absolute_map_filepath = get_absolute_file_path(map_filepath)
|
||||
with open(absolute_map_filepath, 'rb') as f:
|
||||
for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
|
||||
md5.update(chunk)
|
||||
return md5.hexdigest()
|
@ -5,7 +5,7 @@ import shortuuid
|
||||
|
||||
def encode_uuid(uuid_value: UUID) -> str:
|
||||
"""
|
||||
Return short id string from an UUID
|
||||
Return short id string from a UUID
|
||||
"""
|
||||
return shortuuid.encode(uuid_value)
|
||||
|
16
fittrackee/workouts/utils/uploads.py
Normal file
16
fittrackee/workouts/utils/uploads.py
Normal file
@ -0,0 +1,16 @@
|
||||
import os
|
||||
|
||||
from fittrackee.files import get_absolute_file_path
|
||||
|
||||
|
||||
def get_upload_dir_size() -> int:
|
||||
"""
|
||||
Return upload directory size
|
||||
"""
|
||||
upload_path = get_absolute_file_path('')
|
||||
total_size = 0
|
||||
for dir_path, _, filenames in os.walk(upload_path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dir_path, f)
|
||||
total_size += os.path.getsize(fp)
|
||||
return total_size
|
@ -1,4 +1,3 @@
|
||||
import hashlib
|
||||
import os
|
||||
import tempfile
|
||||
import zipfile
|
||||
@ -10,7 +9,6 @@ import gpxpy.gpx
|
||||
import pytz
|
||||
from flask import current_app
|
||||
from sqlalchemy import exc
|
||||
from staticmap import Line, StaticMap
|
||||
from werkzeug.datastructures import FileStorage
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
@ -18,9 +16,10 @@ 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_gpx import get_gpx_info
|
||||
from ..exceptions import WorkoutException
|
||||
from ..models import Sport, Workout, WorkoutSegment
|
||||
from .gpx import get_gpx_info
|
||||
from .maps import generate_map, get_map_hash
|
||||
|
||||
|
||||
def get_datetime_with_tz(
|
||||
@ -258,39 +257,11 @@ def get_new_file_path(
|
||||
return file_path
|
||||
|
||||
|
||||
def generate_map(map_filepath: str, map_data: List) -> None:
|
||||
"""
|
||||
Generate and save map image from map data
|
||||
"""
|
||||
m = StaticMap(400, 225, 10)
|
||||
if not current_app.config['TILE_SERVER']['DEFAULT_STATICMAP']:
|
||||
m.url_template = current_app.config['TILE_SERVER']['URL'].replace(
|
||||
'{s}.', ''
|
||||
)
|
||||
line = Line(map_data, '#3388FF', 4)
|
||||
m.add_line(line)
|
||||
image = m.render()
|
||||
image.save(map_filepath)
|
||||
|
||||
|
||||
def get_map_hash(map_filepath: str) -> str:
|
||||
"""
|
||||
Generate a md5 hash used as id instead of workout id, to retrieve map
|
||||
image (maps are sensitive data)
|
||||
"""
|
||||
md5 = hashlib.md5()
|
||||
absolute_map_filepath = get_absolute_file_path(map_filepath)
|
||||
with open(absolute_map_filepath, 'rb') as f:
|
||||
for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
|
||||
md5.update(chunk)
|
||||
return md5.hexdigest()
|
||||
|
||||
|
||||
def process_one_gpx_file(
|
||||
params: Dict, filename: str, stopped_speed_threshold: float
|
||||
) -> Workout:
|
||||
"""
|
||||
Get all data from a gpx file to create an workout with map image
|
||||
Get all data from a gpx file to create a workout with map image
|
||||
"""
|
||||
try:
|
||||
gpx_data, map_data, weather_data = get_gpx_info(
|
||||
@ -433,19 +404,6 @@ def process_files(
|
||||
)
|
||||
|
||||
|
||||
def get_upload_dir_size() -> int:
|
||||
"""
|
||||
Return upload directory size
|
||||
"""
|
||||
upload_path = get_absolute_file_path('')
|
||||
total_size = 0
|
||||
for dir_path, _, filenames in os.walk(upload_path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dir_path, f)
|
||||
total_size += os.path.getsize(fp)
|
||||
return total_size
|
||||
|
||||
|
||||
def get_average_speed(
|
||||
nb_workouts: int, total_average_speed: float, workout_average_speed: float
|
||||
) -> float:
|
@ -33,7 +33,14 @@ from fittrackee.users.models import User
|
||||
from fittrackee.users.utils import can_view_workout
|
||||
|
||||
from .models import Workout
|
||||
from .utils import (
|
||||
from .utils.convert import convert_in_duration
|
||||
from .utils.gpx import (
|
||||
WorkoutGPXException,
|
||||
extract_segment_from_gpx_file,
|
||||
get_chart_data,
|
||||
)
|
||||
from .utils.short_id import decode_short_id
|
||||
from .utils.workouts import (
|
||||
WorkoutException,
|
||||
create_workout,
|
||||
edit_workout,
|
||||
@ -41,13 +48,6 @@ from .utils import (
|
||||
get_datetime_from_request_args,
|
||||
process_files,
|
||||
)
|
||||
from .utils_format import convert_in_duration
|
||||
from .utils_gpx import (
|
||||
WorkoutGPXException,
|
||||
extract_segment_from_gpx_file,
|
||||
get_chart_data,
|
||||
)
|
||||
from .utils_id import decode_short_id
|
||||
|
||||
workouts_blueprint = Blueprint('workouts', __name__)
|
||||
|
||||
|
Reference in New Issue
Block a user