API - change workout files naming
This commit is contained in:
parent
111bfb725f
commit
af364c39f6
@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional
|
||||||
@ -250,6 +251,56 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
|
|||||||
assert 'just a workout' == data['data']['workouts'][0]['title']
|
assert 'just a workout' == data['data']['workouts'][0]['title']
|
||||||
assert_workout_data_with_gpx(data)
|
assert_workout_data_with_gpx(data)
|
||||||
|
|
||||||
|
def test_it_creates_workout_with_expecting_gpx_path(
|
||||||
|
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
|
||||||
|
) -> None:
|
||||||
|
client, auth_token = self.get_test_client_and_auth_token(
|
||||||
|
app, user_1.email
|
||||||
|
)
|
||||||
|
|
||||||
|
client.post(
|
||||||
|
'/api/workouts',
|
||||||
|
data=dict(
|
||||||
|
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||||
|
data='{"sport_id": 1}',
|
||||||
|
),
|
||||||
|
headers=dict(
|
||||||
|
content_type='multipart/form-data',
|
||||||
|
Authorization=f'Bearer {auth_token}',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
workout = Workout.query.first()
|
||||||
|
assert re.match(
|
||||||
|
r'^workouts/1/2018-03-13_12-44-45_1_([\w\d_-]*).gpx$',
|
||||||
|
workout.gpx,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_it_creates_workout_with_expecting_map_path(
|
||||||
|
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
|
||||||
|
) -> None:
|
||||||
|
client, auth_token = self.get_test_client_and_auth_token(
|
||||||
|
app, user_1.email
|
||||||
|
)
|
||||||
|
|
||||||
|
client.post(
|
||||||
|
'/api/workouts',
|
||||||
|
data=dict(
|
||||||
|
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||||
|
data='{"sport_id": 1}',
|
||||||
|
),
|
||||||
|
headers=dict(
|
||||||
|
content_type='multipart/form-data',
|
||||||
|
Authorization=f'Bearer {auth_token}',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
workout = Workout.query.first()
|
||||||
|
assert re.match(
|
||||||
|
r'^workouts/1/2018-03-13_12-44-45_1_([\w\d_-]*).png$',
|
||||||
|
workout.map,
|
||||||
|
)
|
||||||
|
|
||||||
def test_it_adds_a_workout_with_gpx_without_name(
|
def test_it_adds_a_workout_with_gpx_without_name(
|
||||||
self,
|
self,
|
||||||
app: Flask,
|
app: Flask,
|
||||||
|
@ -255,7 +255,7 @@ def get_file_path(dir_path: str, filename: str) -> str:
|
|||||||
def get_new_file_path(
|
def get_new_file_path(
|
||||||
auth_user_id: int,
|
auth_user_id: int,
|
||||||
workout_date: str,
|
workout_date: str,
|
||||||
sport: str,
|
sport_id: int,
|
||||||
old_filename: Optional[str] = None,
|
old_filename: Optional[str] = None,
|
||||||
extension: Optional[str] = None,
|
extension: Optional[str] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
@ -265,11 +265,9 @@ def get_new_file_path(
|
|||||||
if not extension and old_filename:
|
if not extension and old_filename:
|
||||||
extension = f".{old_filename.rsplit('.', 1)[1].lower()}"
|
extension = f".{old_filename.rsplit('.', 1)[1].lower()}"
|
||||||
_, new_filename = tempfile.mkstemp(
|
_, new_filename = tempfile.mkstemp(
|
||||||
prefix=f'{workout_date}_{sport}_', suffix=extension
|
prefix=f'{workout_date}_{sport_id}_', suffix=extension
|
||||||
)
|
)
|
||||||
dir_path = os.path.join('workouts', str(auth_user_id))
|
dir_path = os.path.join('workouts', str(auth_user_id))
|
||||||
if not os.path.exists(dir_path):
|
|
||||||
os.makedirs(dir_path)
|
|
||||||
file_path = os.path.join(dir_path, new_filename.split('/')[-1])
|
file_path = os.path.join(dir_path, new_filename.split('/')[-1])
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
@ -285,11 +283,16 @@ def process_one_gpx_file(
|
|||||||
params['file_path'], stopped_speed_threshold
|
params['file_path'], stopped_speed_threshold
|
||||||
)
|
)
|
||||||
auth_user = params['auth_user']
|
auth_user = params['auth_user']
|
||||||
|
workout_date, _ = get_workout_datetime(
|
||||||
|
workout_date=gpx_data['start'],
|
||||||
|
date_str_format=None if gpx_data else '%Y-%m-%d %H:%M',
|
||||||
|
user_timezone=None,
|
||||||
|
)
|
||||||
new_filepath = get_new_file_path(
|
new_filepath = get_new_file_path(
|
||||||
auth_user_id=auth_user.id,
|
auth_user_id=auth_user.id,
|
||||||
workout_date=gpx_data['start'],
|
workout_date=workout_date.strftime('%Y-%m-%d_%H-%M-%S'),
|
||||||
old_filename=filename,
|
old_filename=filename,
|
||||||
sport=params['sport_label'],
|
sport_id=params['sport_id'],
|
||||||
)
|
)
|
||||||
absolute_gpx_filepath = get_absolute_file_path(new_filepath)
|
absolute_gpx_filepath = get_absolute_file_path(new_filepath)
|
||||||
os.rename(params['file_path'], absolute_gpx_filepath)
|
os.rename(params['file_path'], absolute_gpx_filepath)
|
||||||
@ -297,9 +300,9 @@ def process_one_gpx_file(
|
|||||||
|
|
||||||
map_filepath = get_new_file_path(
|
map_filepath = get_new_file_path(
|
||||||
auth_user_id=auth_user.id,
|
auth_user_id=auth_user.id,
|
||||||
workout_date=gpx_data['start'],
|
workout_date=workout_date.strftime('%Y-%m-%d_%H-%M-%S'),
|
||||||
extension='.png',
|
extension='.png',
|
||||||
sport=params['sport_label'],
|
sport_id=params['sport_id'],
|
||||||
)
|
)
|
||||||
absolute_map_filepath = get_absolute_file_path(map_filepath)
|
absolute_map_filepath = get_absolute_file_path(map_filepath)
|
||||||
generate_map(absolute_map_filepath, map_data)
|
generate_map(absolute_map_filepath, map_data)
|
||||||
@ -397,7 +400,7 @@ def process_files(
|
|||||||
'auth_user': auth_user,
|
'auth_user': auth_user,
|
||||||
'workout_data': workout_data,
|
'workout_data': workout_data,
|
||||||
'file_path': file_path,
|
'file_path': file_path,
|
||||||
'sport_label': sport.label,
|
'sport_id': sport.id,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user