From 339ce7e898f425476da32650f592e5232edbd8d6 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 6 Oct 2024 11:30:24 +0200 Subject: [PATCH 1/2] API & Client - add ability to override title when adding workout w/ gpx --- .../workouts/test_workouts_api_1_post.py | 30 +++++++++++++++++++ fittrackee/workouts/utils/workouts.py | 8 ++++- fittrackee/workouts/workouts.py | 17 ++++++----- .../src/components/Workout/WorkoutEdition.vue | 26 +++++++++++----- .../src/locales/en/workouts.json | 1 + .../src/locales/fr/workouts.json | 1 + .../src/store/modules/workouts/actions.ts | 3 +- fittrackee_client/src/types/workouts.ts | 2 +- 8 files changed, 71 insertions(+), 17 deletions(-) diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 484ce37f3..4e7f9bc68 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -481,6 +481,36 @@ def test_it_adds_a_workout_with_gpx_without_name( ) assert_workout_data_with_gpx(data) + def test_it_adds_a_workout_with_provided_title( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + gpx_file: str, + ) -> None: + title = "some title" + client, auth_token = self.get_test_client_and_auth_token( + app, user_1.email + ) + + response = client.post( + '/api/workouts', + data=dict( + file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), + data=f'{{"sport_id": 1, "title": "{title}"}}', + ), + headers=dict( + content_type='multipart/form-data', + Authorization=f'Bearer {auth_token}', + ), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 201 + assert 'created' in data['status'] + assert len(data['data']['workouts']) == 1 + assert data['data']['workouts'][0]['title'] == title + def test_it_adds_a_workout_with_gpx_without_name_timezone( self, app: Flask, diff --git a/fittrackee/workouts/utils/workouts.py b/fittrackee/workouts/utils/workouts.py index c0001a4aa..458098a57 100644 --- a/fittrackee/workouts/utils/workouts.py +++ b/fittrackee/workouts/utils/workouts.py @@ -131,7 +131,13 @@ def create_workout( else timedelta(seconds=workout_data['duration']) ) distance = gpx_data['distance'] if gpx_data else workout_data['distance'] - title = gpx_data['name'] if gpx_data else workout_data.get('title', '') + title = ( + workout_data.get('title', '') + if workout_data.get('title', '') + else gpx_data['name'] + if gpx_data + else '' + ) new_workout = Workout( user_id=user.id, diff --git a/fittrackee/workouts/workouts.py b/fittrackee/workouts/workouts.py index 43856fc88..e7637ba4d 100644 --- a/fittrackee/workouts/workouts.py +++ b/fittrackee/workouts/workouts.py @@ -1043,13 +1043,16 @@ def post_workout(auth_user: User) -> Union[Tuple[Dict, int], HttpResponse]: } :form file: gpx file (allowed extensions: .gpx, .zip) - :form data: sport id, equipment id, description and notes, for example: - `{"sport_id": 1, "notes": "", "description": "", "equipment_ids": []}`. - Double quotes in notes and description must be escaped. + :form data: sport id, equipment id, description, title and notes, + for example: + ``{"sport_id": 1, "notes": "", "title": "", "description": "", + "equipment_ids": []}``. + Double quotes in notes, description and title must be escaped. - The maximum length of notes is 500 characters and that of the - description is 10000 characters. - Otherwise, they will be truncated. + The maximum length is 500 characters for notes, 10000 characters for + description and 255 for title. Otherwise, they will be truncated. + When description and title are provided, they replace the description + and title from gpx file. For `equipment_ids`, the id of the equipment to associate with this workout. @@ -1057,7 +1060,7 @@ def post_workout(auth_user: User) -> Union[Tuple[Dict, int], HttpResponse]: If not provided and default equipment exists for sport, default equipment will be associated. - Notes, description and equipment ids are not mandatory. + Notes, description, and title and equipment ids are not mandatory. :reqheader Authorization: OAuth 2.0 Bearer Token diff --git a/fittrackee_client/src/components/Workout/WorkoutEdition.vue b/fittrackee_client/src/components/Workout/WorkoutEdition.vue index 6c79edd76..d2bcaccb4 100644 --- a/fittrackee_client/src/components/Workout/WorkoutEdition.vue +++ b/fittrackee_client/src/components/Workout/WorkoutEdition.vue @@ -93,7 +93,7 @@ -
+
+
+ +