API - use threshold from preferences if exists when processing gpx file

This commit is contained in:
Sam 2021-11-12 12:33:47 +01:00
parent c05aba92a9
commit 7afdd04d7d
2 changed files with 42 additions and 4 deletions

View File

@ -5,7 +5,7 @@ from flask import Flask
from gpxpy.gpx import MovingData
from werkzeug.datastructures import FileStorage
from fittrackee.users.models import User
from fittrackee.users.models import User, UserSportPreference
from fittrackee.workouts.models import Sport
from fittrackee.workouts.utils import process_files
@ -56,3 +56,33 @@ class TestStoppedSpeedThreshold:
stopped_speed_threshold=expected_threshold
)
gpx_track_segment_mock.assert_called_with(expected_threshold)
def test_it_calls_get_moving_data_with_threshold_depending_from_user_preference( # noqa
self,
app: Flask,
user_1: User,
gpx_file_storage: FileStorage,
sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference,
) -> None:
expected_threshold = 0.7
user_sport_1_preference.stopped_speed_threshold = expected_threshold
with patch(
'fittrackee.workouts.utils.get_new_file_path',
return_value='/tmp/fitTrackee/uploads/test.png',
), patch(
'gpxpy.gpx.GPXTrackSegment.get_moving_data',
return_value=moving_data,
) as gpx_track_segment_mock:
process_files(
auth_user_id=user_1.id,
folders=folders,
workout_data={'sport_id': sport_1_cycling.id},
workout_file=gpx_file_storage,
)
assert gpx_track_segment_mock.call_args_list[0] == call(
stopped_speed_threshold=expected_threshold
)
gpx_track_segment_mock.assert_called_with(expected_threshold)

View File

@ -15,7 +15,7 @@ from werkzeug.datastructures import FileStorage
from werkzeug.utils import secure_filename
from fittrackee import db
from fittrackee.users.models import User
from fittrackee.users.models import User, UserSportPreference
from .exceptions import WorkoutException
from .models import Sport, Workout, WorkoutSegment
@ -400,6 +400,14 @@ def process_files(
f"Sport id: {workout_data.get('sport_id')} does not exist",
)
user = User.query.filter_by(id=auth_user_id).first()
sport_preferences = UserSportPreference.query.filter_by(
user_id=user.id, sport_id=sport.id
).first()
stopped_speed_threshold = (
sport.stopped_speed_threshold
if sport_preferences is None
else sport_preferences.stopped_speed_threshold
)
common_params = {
'user': user,
@ -418,14 +426,14 @@ def process_files(
process_one_gpx_file(
common_params,
filename,
sport.stopped_speed_threshold,
stopped_speed_threshold,
)
]
else:
return process_zip_archive(
common_params,
folders['extract_dir'],
sport.stopped_speed_threshold,
stopped_speed_threshold,
)