refresh activity data - wip

This commit is contained in:
Sam 2019-01-06 14:17:34 +01:00
parent 33e594d3d3
commit 1826209916
2 changed files with 98 additions and 13 deletions

View File

@ -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,6 +235,7 @@ def get_gpx_info(gpx_file):
# first gpx point => get weather
if start == 0:
start = point.time
if update_weather_data:
weather_data.append(get_weather(point))
# if a previous segment exists, calculate stopped time between
@ -222,8 +248,10 @@ 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))
if update_map_data:
map_data.append([
point.longitude, point.latitude
])
@ -242,6 +270,8 @@ 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}
if update_map_data:
bounds = gpx.get_bounds()
gpx_data['bounds'] = [
bounds.min_latitude,

View File

@ -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']