diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index 0dc9eead..cff8dd12 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -8,8 +8,7 @@ from sqlalchemy import exc from ..users.utils import authenticate, verify_extension from .models import Activity from .utils import ( - create_activity_with_gpx, create_activity_wo_gpx, edit_activity_wo_gpx, - get_file_path, get_gpx_info + create_activity, edit_activity_wo_gpx, get_file_path, get_gpx_info ) activities_blueprint = Blueprint('activities', __name__) @@ -138,8 +137,8 @@ def post_activity(auth_user_id): return jsonify(response_object), 500 try: - new_activity = create_activity_with_gpx( - auth_user_id, gpx_data, file_path, activity_data.get('sport_id')) + new_activity = create_activity( + auth_user_id, activity_data, gpx_data, file_path) db.session.add(new_activity) db.session.commit() response_object = { @@ -176,7 +175,7 @@ def post_activity_no_gpx(auth_user_id): return jsonify(response_object), 400 try: - new_activity = create_activity_wo_gpx(auth_user_id, activity_data) + new_activity = create_activity(auth_user_id, activity_data) db.session.add(new_activity) db.session.commit() diff --git a/mpwo_api/mpwo_api/activities/utils.py b/mpwo_api/mpwo_api/activities/utils.py index 78265dd5..4a1fb864 100644 --- a/mpwo_api/mpwo_api/activities/utils.py +++ b/mpwo_api/mpwo_api/activities/utils.py @@ -8,39 +8,38 @@ from werkzeug.utils import secure_filename from .models import Activity -def create_activity_with_gpx(auth_user_id, gpx_data, file_path, sport_id): - new_activity = Activity( - user_id=auth_user_id, - sport_id=sport_id, - activity_date=gpx_data['start'], - duration=timedelta(seconds=gpx_data['duration']) - ) - new_activity.gpx = file_path - new_activity.pauses = timedelta(seconds=gpx_data['stop_time']) - new_activity.moving = timedelta(seconds=gpx_data['moving_time']) - new_activity.distance = gpx_data['distance'] - new_activity.min_alt = gpx_data['elevation_min'] - new_activity.max_alt = gpx_data['elevation_max'] - new_activity.descent = gpx_data['downhill'] - new_activity.ascent = gpx_data['uphill'] - new_activity.max_speed = gpx_data['max_speed'] - new_activity.ave_speed = gpx_data['average_speed'] - return new_activity +def create_activity( + auth_user_id, activity_data, gpx_data=None, file_path=None +): + activity_date = gpx_data['start'] if gpx_data else datetime.strptime( + activity_data.get('activity_date'), '%Y-%m-%d %H:%M') + duration = timedelta(seconds=gpx_data['duration']) if gpx_data \ + else timedelta(seconds=activity_data.get('duration')) - -def create_activity_wo_gpx(auth_user_id, activity_data): new_activity = Activity( user_id=auth_user_id, sport_id=activity_data.get('sport_id'), - activity_date=datetime.strptime( - activity_data.get('activity_date'), '%Y-%m-%d %H:%M'), - duration=timedelta(seconds=activity_data.get('duration')) + activity_date=activity_date, + duration=duration ) - new_activity.moving = new_activity.duration - new_activity.distance = activity_data.get('distance') - new_activity.ave_speed = new_activity.distance / ( - new_activity.duration.seconds / 3600) - new_activity.max_speed = new_activity.ave_speed + + if gpx_data: + new_activity.gpx = file_path + new_activity.pauses = timedelta(seconds=gpx_data['stop_time']) + new_activity.moving = timedelta(seconds=gpx_data['moving_time']) + new_activity.distance = gpx_data['distance'] + new_activity.min_alt = gpx_data['elevation_min'] + new_activity.max_alt = gpx_data['elevation_max'] + new_activity.descent = gpx_data['downhill'] + new_activity.ascent = gpx_data['uphill'] + new_activity.max_speed = gpx_data['max_speed'] + new_activity.ave_speed = gpx_data['average_speed'] + else: + new_activity.moving = duration + new_activity.distance = activity_data.get('distance') + new_activity.ave_speed = new_activity.distance / ( + duration.seconds / 3600) + new_activity.max_speed = new_activity.ave_speed return new_activity