From 1826209916130e2971f26efc44ad28977a41d864 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 6 Jan 2019 14:17:34 +0100 Subject: [PATCH] refresh activity data - wip --- .../fittrackee_api/activities/utils.py | 56 ++++++++++++++----- .../tests/test_activities_api_2_patch.py | 55 ++++++++++++++++++ 2 files changed, 98 insertions(+), 13 deletions(-) diff --git a/fittrackee_api/fittrackee_api/activities/utils.py b/fittrackee_api/fittrackee_api/activities/utils.py index 6ebdbab6..ef4a8707 100644 --- a/fittrackee_api/fittrackee_api/activities/utils.py +++ b/fittrackee_api/fittrackee_api/activities/utils.py @@ -119,8 +119,33 @@ def create_segment(activity_id, segment_data): return new_segment +def update_activity(activity): + """ + Note: only gpx_data is be updated for now (the gpx file is NOT modified) + + In a next version, map_data and weather_data will be updated + (case of a modified gpx file, see issue #7) + """ + gpx_data, _, _ = get_gpx_info(get_absolute_file_path(activity.gpx)) + updated_activity = update_activity_data(activity, gpx_data) + updated_activity.duration = gpx_data['duration'] + updated_activity.distance = gpx_data['distance'] + db.session.flush() + + for segment_idx, segment in enumerate(updated_activity.segments): + segment_data = gpx_data['segments'][segment_idx] + updated_segment = update_activity_data(segment, segment_data) + updated_segment.duration = segment_data['duration'] + updated_segment.distance = segment_data['distance'] + db.session.flush() + + return updated_activity + + def edit_activity(activity, activity_data, auth_user_id): user = User.query.filter_by(id=auth_user_id).first() + if activity_data.get('refresh'): + activity = update_activity(activity) if activity_data.get('sport_id'): activity.sport_id = activity_data.get('sport_id') if activity_data.get('title'): @@ -184,7 +209,7 @@ def open_gpx_file(gpx_file): return gpx -def get_gpx_info(gpx_file): +def get_gpx_info(gpx_file, update_map_data=True, update_weather_data=True): gpx = open_gpx_file(gpx_file) if gpx is None: return None @@ -210,7 +235,8 @@ def get_gpx_info(gpx_file): # first gpx point => get weather if start == 0: start = point.time - weather_data.append(get_weather(point)) + if update_weather_data: + weather_data.append(get_weather(point)) # if a previous segment exists, calculate stopped time between # the two segments @@ -222,11 +248,13 @@ def get_gpx_info(gpx_file): prev_seg_last_point = point.time # last gpx point => get weather - if segment_idx == (segments_nb - 1): + if segment_idx == (segments_nb - 1) and update_weather_data: weather_data.append(get_weather(point)) - map_data.append([ - point.longitude, point.latitude - ]) + + if update_map_data: + map_data.append([ + point.longitude, point.latitude + ]) segment_max_speed = (segment.get_moving_data().max_speed if segment.get_moving_data().max_speed else 0) @@ -242,13 +270,15 @@ def get_gpx_info(gpx_file): full_gpx_data = get_gpx_data(gpx, max_speed, start, stopped_time_btwn_seg) gpx_data = {**gpx_data, **full_gpx_data} - bounds = gpx.get_bounds() - gpx_data['bounds'] = [ - bounds.min_latitude, - bounds.min_longitude, - bounds.max_latitude, - bounds.max_longitude - ] + + if update_map_data: + bounds = gpx.get_bounds() + gpx_data['bounds'] = [ + bounds.min_latitude, + bounds.min_longitude, + bounds.max_latitude, + bounds.max_longitude + ] return gpx_data, map_data, weather_data diff --git a/fittrackee_api/fittrackee_api/tests/test_activities_api_2_patch.py b/fittrackee_api/fittrackee_api/tests/test_activities_api_2_patch.py index a968bd3a..da24abb7 100644 --- a/fittrackee_api/fittrackee_api/tests/test_activities_api_2_patch.py +++ b/fittrackee_api/fittrackee_api/tests/test_activities_api_2_patch.py @@ -1,6 +1,8 @@ import json from io import BytesIO +from fittrackee_api.activities.models import Activity + def assert_activity_data_with_gpx(data): assert 'creation_date' in data['data']['activities'][0] @@ -933,3 +935,56 @@ def test_edit_an_activity_no_activity( assert response.status_code == 404 assert 'not found' in data['status'] assert len(data['data']['activities']) == 0 + + +def test_refresh_an_activity_with_gpx( + app, user_1, sport_1_cycling, sport_2_running, gpx_file +): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + client.post( + '/api/activities', + data=dict( + file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), + data='{"sport_id": 1}' + ), + headers=dict( + content_type='multipart/form-data', + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + + # Edit some activity data + activity = Activity.query.filter_by(id=1).first() + activity.ascent = 1000 + activity.min_alt = -100 + + response = client.patch( + '/api/activities/1', + content_type='application/json', + data=json.dumps(dict( + refresh=True, + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert 'success' in data['status'] + assert len(data['data']['activities']) == 1 + assert 1 == data['data']['activities'][0]['sport_id'] + assert 0.4 == data['data']['activities'][0]['ascent'] + assert 975.0 == data['data']['activities'][0]['min_alt']