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 ..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()
|
||||||
|
|
||||||
|
@ -8,39 +8,38 @@ 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(
|
||||||
new_activity = Activity(
|
auth_user_id, activity_data, gpx_data=None, file_path=None
|
||||||
user_id=auth_user_id,
|
):
|
||||||
sport_id=sport_id,
|
activity_date = gpx_data['start'] if gpx_data else datetime.strptime(
|
||||||
activity_date=gpx_data['start'],
|
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
||||||
duration=timedelta(seconds=gpx_data['duration'])
|
duration = timedelta(seconds=gpx_data['duration']) if gpx_data \
|
||||||
)
|
else timedelta(seconds=activity_data.get('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_wo_gpx(auth_user_id, activity_data):
|
|
||||||
new_activity = Activity(
|
new_activity = Activity(
|
||||||
user_id=auth_user_id,
|
user_id=auth_user_id,
|
||||||
sport_id=activity_data.get('sport_id'),
|
sport_id=activity_data.get('sport_id'),
|
||||||
activity_date=datetime.strptime(
|
activity_date=activity_date,
|
||||||
activity_data.get('activity_date'), '%Y-%m-%d %H:%M'),
|
duration=duration
|
||||||
duration=timedelta(seconds=activity_data.get('duration'))
|
|
||||||
)
|
)
|
||||||
new_activity.moving = new_activity.duration
|
|
||||||
new_activity.distance = activity_data.get('distance')
|
if gpx_data:
|
||||||
new_activity.ave_speed = new_activity.distance / (
|
new_activity.gpx = file_path
|
||||||
new_activity.duration.seconds / 3600)
|
new_activity.pauses = timedelta(seconds=gpx_data['stop_time'])
|
||||||
new_activity.max_speed = new_activity.ave_speed
|
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
|
return new_activity
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user