API: activities - minor refactor

This commit is contained in:
Sam 2018-05-10 10:21:58 +02:00
parent 32777061ac
commit 5f98f1525d
2 changed files with 31 additions and 33 deletions

View File

@ -8,8 +8,7 @@ from sqlalchemy import exc
from ..users.utils import authenticate, verify_extension from ..users.utils import authenticate, verify_extension
from .models import Activity from .models import Activity
from .utils import ( from .utils import (
create_activity_with_gpx, create_activity_wo_gpx, edit_activity_wo_gpx, create_activity, edit_activity_wo_gpx, get_file_path, get_gpx_info
get_file_path, get_gpx_info
) )
activities_blueprint = Blueprint('activities', __name__) activities_blueprint = Blueprint('activities', __name__)
@ -138,8 +137,8 @@ def post_activity(auth_user_id):
return jsonify(response_object), 500 return jsonify(response_object), 500
try: try:
new_activity = create_activity_with_gpx( new_activity = create_activity(
auth_user_id, gpx_data, file_path, activity_data.get('sport_id')) auth_user_id, activity_data, gpx_data, file_path)
db.session.add(new_activity) db.session.add(new_activity)
db.session.commit() db.session.commit()
response_object = { response_object = {
@ -176,7 +175,7 @@ def post_activity_no_gpx(auth_user_id):
return jsonify(response_object), 400 return jsonify(response_object), 400
try: 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.add(new_activity)
db.session.commit() db.session.commit()

View File

@ -8,13 +8,22 @@ from werkzeug.utils import secure_filename
from .models import Activity 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( new_activity = Activity(
user_id=auth_user_id, user_id=auth_user_id,
sport_id=sport_id, sport_id=activity_data.get('sport_id'),
activity_date=gpx_data['start'], activity_date=activity_date,
duration=timedelta(seconds=gpx_data['duration']) duration=duration
) )
if gpx_data:
new_activity.gpx = file_path new_activity.gpx = file_path
new_activity.pauses = timedelta(seconds=gpx_data['stop_time']) new_activity.pauses = timedelta(seconds=gpx_data['stop_time'])
new_activity.moving = timedelta(seconds=gpx_data['moving_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.ascent = gpx_data['uphill']
new_activity.max_speed = gpx_data['max_speed'] new_activity.max_speed = gpx_data['max_speed']
new_activity.ave_speed = gpx_data['average_speed'] new_activity.ave_speed = gpx_data['average_speed']
return new_activity else:
new_activity.moving = 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'))
)
new_activity.moving = new_activity.duration
new_activity.distance = activity_data.get('distance') new_activity.distance = activity_data.get('distance')
new_activity.ave_speed = new_activity.distance / ( new_activity.ave_speed = new_activity.distance / (
new_activity.duration.seconds / 3600) duration.seconds / 3600)
new_activity.max_speed = new_activity.ave_speed new_activity.max_speed = new_activity.ave_speed
return new_activity return new_activity