API: activities - minor refactor
This commit is contained in:
parent
32777061ac
commit
5f98f1525d
@ -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()
|
||||
|
||||
|
@ -8,13 +8,22 @@ from werkzeug.utils import secure_filename
|
||||
from .models import Activity
|
||||
|
||||
|
||||
def create_activity_with_gpx(auth_user_id, gpx_data, file_path, sport_id):
|
||||
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'))
|
||||
|
||||
new_activity = Activity(
|
||||
user_id=auth_user_id,
|
||||
sport_id=sport_id,
|
||||
activity_date=gpx_data['start'],
|
||||
duration=timedelta(seconds=gpx_data['duration'])
|
||||
sport_id=activity_data.get('sport_id'),
|
||||
activity_date=activity_date,
|
||||
duration=duration
|
||||
)
|
||||
|
||||
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'])
|
||||
@ -25,21 +34,11 @@ def create_activity_with_gpx(auth_user_id, gpx_data, file_path, sport_id):
|
||||
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_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'))
|
||||
)
|
||||
new_activity.moving = new_activity.duration
|
||||
else:
|
||||
new_activity.moving = duration
|
||||
new_activity.distance = activity_data.get('distance')
|
||||
new_activity.ave_speed = new_activity.distance / (
|
||||
new_activity.duration.seconds / 3600)
|
||||
duration.seconds / 3600)
|
||||
new_activity.max_speed = new_activity.ave_speed
|
||||
return new_activity
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user