2018-05-01 17:51:38 +02:00
|
|
|
import json
|
2018-05-09 18:54:30 +02:00
|
|
|
import os
|
2018-05-29 19:06:33 +02:00
|
|
|
import shutil
|
2021-05-22 17:14:24 +02:00
|
|
|
from datetime import timedelta
|
2021-11-30 09:02:41 +01:00
|
|
|
from typing import Dict, List, Optional, Tuple, Union
|
2018-01-21 17:43:13 +01:00
|
|
|
|
2020-09-16 11:47:20 +02:00
|
|
|
import requests
|
2021-11-29 16:54:24 +01:00
|
|
|
from flask import (
|
|
|
|
Blueprint,
|
|
|
|
Response,
|
|
|
|
current_app,
|
|
|
|
request,
|
|
|
|
send_from_directory,
|
|
|
|
)
|
2021-01-20 16:47:00 +01:00
|
|
|
from sqlalchemy import exc
|
2021-02-20 21:37:31 +01:00
|
|
|
from werkzeug.exceptions import RequestEntityTooLarge
|
2022-02-13 12:01:08 +01:00
|
|
|
from werkzeug.utils import secure_filename
|
2021-01-20 16:47:00 +01:00
|
|
|
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee import appLog, db
|
2022-05-27 15:51:40 +02:00
|
|
|
from fittrackee.oauth2.server import require_auth
|
2021-01-01 16:39:25 +01:00
|
|
|
from fittrackee.responses import (
|
|
|
|
DataInvalidPayloadErrorResponse,
|
|
|
|
DataNotFoundErrorResponse,
|
2021-01-02 19:28:03 +01:00
|
|
|
HttpResponse,
|
2021-01-01 16:39:25 +01:00
|
|
|
InternalServerErrorResponse,
|
|
|
|
InvalidPayloadErrorResponse,
|
|
|
|
NotFoundErrorResponse,
|
2021-02-20 21:37:31 +01:00
|
|
|
PayloadTooLargeErrorResponse,
|
2022-02-16 13:03:14 +01:00
|
|
|
get_error_response_if_file_is_invalid,
|
2021-01-01 16:39:25 +01:00
|
|
|
handle_error_and_return_response,
|
|
|
|
)
|
2021-02-20 21:37:31 +01:00
|
|
|
from fittrackee.users.models import User
|
2018-05-01 17:51:38 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
from .models import Workout
|
2022-02-16 17:46:22 +01:00
|
|
|
from .utils.convert import convert_in_duration
|
|
|
|
from .utils.gpx import (
|
|
|
|
WorkoutGPXException,
|
|
|
|
extract_segment_from_gpx_file,
|
|
|
|
get_chart_data,
|
|
|
|
)
|
|
|
|
from .utils.short_id import decode_short_id
|
2022-02-16 18:07:05 +01:00
|
|
|
from .utils.visibility import can_view_workout
|
2022-02-16 17:46:22 +01:00
|
|
|
from .utils.workouts import (
|
2021-01-10 11:16:43 +01:00
|
|
|
WorkoutException,
|
|
|
|
create_workout,
|
|
|
|
edit_workout,
|
2019-08-28 13:25:39 +02:00
|
|
|
get_absolute_file_path,
|
2021-05-22 17:14:24 +02:00
|
|
|
get_datetime_from_request_args,
|
2019-08-28 13:25:39 +02:00
|
|
|
process_files,
|
2018-05-09 18:23:17 +02:00
|
|
|
)
|
2018-01-21 17:43:13 +01:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_blueprint = Blueprint('workouts', __name__)
|
2018-01-21 17:43:13 +01:00
|
|
|
|
2021-02-17 08:37:32 +01:00
|
|
|
DEFAULT_WORKOUTS_PER_PAGE = 5
|
|
|
|
MAX_WORKOUTS_PER_PAGE = 100
|
2020-05-02 18:00:17 +02:00
|
|
|
|
2018-01-21 17:43:13 +01:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route('/workouts', methods=['GET'])
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-12-01 19:22:47 +01:00
|
|
|
def get_workouts(auth_user: User) -> Union[Dict, HttpResponse]:
|
2019-07-14 16:57:16 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get workouts for the authenticated user.
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
**Example requests**:
|
|
|
|
|
2019-07-14 21:36:51 +02:00
|
|
|
- without parameters
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/ HTTP/1.1
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
- with some query parameters
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts?from=2019-07-02&to=2019-07-31&sport_id=1 HTTP/1.1
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
- returning at least one workout
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": [
|
2019-07-14 16:57:16 +02:00
|
|
|
{
|
|
|
|
"ascent": null,
|
|
|
|
"ave_speed": 10.0,
|
|
|
|
"bounds": [],
|
|
|
|
"creation_date": "Sun, 14 Jul 2019 13:51:01 GMT",
|
|
|
|
"descent": null,
|
|
|
|
"distance": 10.0,
|
|
|
|
"duration": "0:17:04",
|
2020-12-30 22:07:43 +01:00
|
|
|
"id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-14 16:57:16 +02:00
|
|
|
"map": null,
|
|
|
|
"max_alt": null,
|
|
|
|
"max_speed": 10.0,
|
|
|
|
"min_alt": null,
|
|
|
|
"modification_date": null,
|
|
|
|
"moving": "0:17:04",
|
2021-01-10 11:16:43 +01:00
|
|
|
"next_workout": 3,
|
2019-07-14 16:57:16 +02:00
|
|
|
"notes": null,
|
|
|
|
"pauses": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"previous_workout": null,
|
2019-07-14 16:57:16 +02:00
|
|
|
"records": [
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"record_type": "MS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 16:57:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"record_type": "LD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": "0:17:04",
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 16:57:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"record_type": "FD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 16:57:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"record_type": "AS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 16:57:16 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"segments": [],
|
|
|
|
"sport_id": 1,
|
|
|
|
"title": null,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2019-07-14 16:57:16 +02:00
|
|
|
"weather_end": null,
|
|
|
|
"weather_start": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"with_gpx": false,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT"
|
2019-07-14 16:57:16 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
- returning no workouts
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": []
|
2019-07-14 16:57:16 +02:00
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:query integer page: page if using pagination (default: 1)
|
2021-01-10 11:16:43 +01:00
|
|
|
:query integer per_page: number of workouts per page
|
2021-02-17 08:37:32 +01:00
|
|
|
(default: 5, max: 100)
|
2019-07-14 16:57:16 +02:00
|
|
|
:query integer sport_id: sport id
|
2019-07-14 21:36:51 +02:00
|
|
|
:query string from: start date (format: ``%Y-%m-%d``)
|
|
|
|
:query string to: end date (format: ``%Y-%m-%d``)
|
2019-07-14 16:57:16 +02:00
|
|
|
:query float distance_from: minimal distance
|
|
|
|
:query float distance_to: maximal distance
|
2019-07-14 21:36:51 +02:00
|
|
|
:query string duration_from: minimal duration (format: ``%H:%M``)
|
|
|
|
:query string duration_to: maximal distance (format: ``%H:%M``)
|
2019-07-14 16:57:16 +02:00
|
|
|
:query float ave_speed_from: minimal average speed
|
|
|
|
:query float ave_speed_to: maximal average speed
|
|
|
|
:query float max_speed_from: minimal max. speed
|
|
|
|
:query float max_speed_to: maximal max. speed
|
2019-07-14 21:36:51 +02:00
|
|
|
:query string order: sorting order (default: ``desc``)
|
2019-07-14 16:57:16 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 200: success
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2019-07-14 16:57:16 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2018-05-10 23:39:59 +02:00
|
|
|
try:
|
|
|
|
params = request.args.copy()
|
2021-05-22 17:14:24 +02:00
|
|
|
page = int(params.get('page', 1))
|
2021-12-01 19:22:47 +01:00
|
|
|
date_from, date_to = get_datetime_from_request_args(params, auth_user)
|
2018-06-07 19:28:06 +02:00
|
|
|
distance_from = params.get('distance_from')
|
|
|
|
distance_to = params.get('distance_to')
|
|
|
|
duration_from = params.get('duration_from')
|
|
|
|
duration_to = params.get('duration_to')
|
|
|
|
ave_speed_from = params.get('ave_speed_from')
|
|
|
|
ave_speed_to = params.get('ave_speed_to')
|
2018-06-11 22:42:04 +02:00
|
|
|
max_speed_from = params.get('max_speed_from')
|
|
|
|
max_speed_to = params.get('max_speed_to')
|
2021-11-02 08:55:21 +01:00
|
|
|
order_by = params.get('order_by', 'workout_date')
|
|
|
|
order = params.get('order', 'desc')
|
2018-06-07 20:31:44 +02:00
|
|
|
sport_id = params.get('sport_id')
|
2021-05-22 17:14:24 +02:00
|
|
|
per_page = int(params.get('per_page', DEFAULT_WORKOUTS_PER_PAGE))
|
2021-02-17 08:37:32 +01:00
|
|
|
if per_page > MAX_WORKOUTS_PER_PAGE:
|
|
|
|
per_page = MAX_WORKOUTS_PER_PAGE
|
2021-11-02 08:55:21 +01:00
|
|
|
workouts_pagination = (
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.query.filter(
|
2021-12-01 19:22:47 +01:00
|
|
|
Workout.user_id == auth_user.id,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.sport_id == sport_id if sport_id else True,
|
|
|
|
Workout.workout_date >= date_from if date_from else True,
|
|
|
|
Workout.workout_date < date_to + timedelta(seconds=1)
|
2019-08-28 13:25:39 +02:00
|
|
|
if date_to
|
|
|
|
else True,
|
2021-11-11 08:57:10 +01:00
|
|
|
Workout.distance >= float(distance_from)
|
2019-08-28 13:25:39 +02:00
|
|
|
if distance_from
|
|
|
|
else True,
|
2021-11-11 08:57:10 +01:00
|
|
|
Workout.distance <= float(distance_to)
|
|
|
|
if distance_to
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.moving >= convert_in_duration(duration_from)
|
2019-08-28 13:25:39 +02:00
|
|
|
if duration_from
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.moving <= convert_in_duration(duration_to)
|
2019-08-28 13:25:39 +02:00
|
|
|
if duration_to
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.ave_speed >= float(ave_speed_from)
|
2019-08-28 13:25:39 +02:00
|
|
|
if ave_speed_from
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.ave_speed <= float(ave_speed_to)
|
2019-08-28 13:25:39 +02:00
|
|
|
if ave_speed_to
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.max_speed >= float(max_speed_from)
|
2019-08-28 13:25:39 +02:00
|
|
|
if max_speed_from
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.max_speed <= float(max_speed_to)
|
2019-08-28 13:25:39 +02:00
|
|
|
if max_speed_to
|
|
|
|
else True,
|
|
|
|
)
|
|
|
|
.order_by(
|
2021-11-02 08:55:21 +01:00
|
|
|
Workout.ave_speed.asc()
|
|
|
|
if order_by == 'ave_speed' and order == 'asc'
|
|
|
|
else True,
|
|
|
|
Workout.ave_speed.desc()
|
|
|
|
if order_by == 'ave_speed' and order == 'desc'
|
|
|
|
else True,
|
|
|
|
Workout.distance.asc()
|
|
|
|
if order_by == 'distance' and order == 'asc'
|
|
|
|
else True,
|
|
|
|
Workout.distance.desc()
|
|
|
|
if order_by == 'distance' and order == 'desc'
|
|
|
|
else True,
|
2021-11-06 22:31:31 +01:00
|
|
|
Workout.moving.asc()
|
2021-11-02 08:55:21 +01:00
|
|
|
if order_by == 'duration' and order == 'asc'
|
|
|
|
else True,
|
2021-11-06 22:31:31 +01:00
|
|
|
Workout.moving.desc()
|
2021-11-02 08:55:21 +01:00
|
|
|
if order_by == 'duration' and order == 'desc'
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.workout_date.asc()
|
2021-11-02 08:55:21 +01:00
|
|
|
if order_by == 'workout_date' and order == 'asc'
|
|
|
|
else True,
|
|
|
|
Workout.workout_date.desc()
|
|
|
|
if order_by == 'workout_date' and order == 'desc'
|
|
|
|
else True,
|
2019-08-28 13:25:39 +02:00
|
|
|
)
|
|
|
|
.paginate(page, per_page, False)
|
|
|
|
)
|
2021-11-02 08:55:21 +01:00
|
|
|
workouts = workouts_pagination.items
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2018-05-10 23:39:59 +02:00
|
|
|
'status': 'success',
|
|
|
|
'data': {
|
2021-01-10 11:16:43 +01:00
|
|
|
'workouts': [workout.serialize(params) for workout in workouts]
|
2019-08-28 13:25:39 +02:00
|
|
|
},
|
2021-11-02 08:55:21 +01:00
|
|
|
'pagination': {
|
|
|
|
'has_next': workouts_pagination.has_next,
|
|
|
|
'has_prev': workouts_pagination.has_prev,
|
|
|
|
'page': workouts_pagination.page,
|
|
|
|
'pages': workouts_pagination.pages,
|
|
|
|
'total': workouts_pagination.total,
|
|
|
|
},
|
2018-01-21 17:43:13 +01:00
|
|
|
}
|
2018-05-10 23:39:59 +02:00
|
|
|
except Exception as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e)
|
2018-05-01 17:51:38 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>', methods=['GET']
|
2020-12-30 19:37:59 +01:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workout(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-14 21:36:51 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get an workout
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF HTTP/1.1
|
2019-07-14 21:36:51 +02:00
|
|
|
|
2019-07-19 11:56:48 +02:00
|
|
|
**Example responses**:
|
|
|
|
|
|
|
|
- success
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": [
|
2019-07-14 21:36:51 +02:00
|
|
|
{
|
|
|
|
"ascent": null,
|
|
|
|
"ave_speed": 16,
|
|
|
|
"bounds": [],
|
|
|
|
"creation_date": "Sun, 14 Jul 2019 18:57:14 GMT",
|
|
|
|
"descent": null,
|
|
|
|
"distance": 12,
|
|
|
|
"duration": "0:45:00",
|
2020-12-30 22:07:43 +01:00
|
|
|
"id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-14 21:36:51 +02:00
|
|
|
"map": null,
|
|
|
|
"max_alt": null,
|
|
|
|
"max_speed": 16,
|
|
|
|
"min_alt": null,
|
|
|
|
"modification_date": "Sun, 14 Jul 2019 18:57:22 GMT",
|
|
|
|
"moving": "0:45:00",
|
2021-01-10 11:16:43 +01:00
|
|
|
"next_workout": 4,
|
|
|
|
"notes": "workout without gpx",
|
2019-07-14 21:36:51 +02:00
|
|
|
"pauses": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"previous_workout": 3,
|
2019-07-14 21:36:51 +02:00
|
|
|
"records": [],
|
|
|
|
"segments": [],
|
|
|
|
"sport_id": 1,
|
|
|
|
"title": "biking on sunday morning",
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2019-07-14 21:36:51 +02:00
|
|
|
"weather_end": null,
|
|
|
|
"weather_start": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"with_gpx": false,
|
|
|
|
"workout_date": "Sun, 07 Jul 2019 07:00:00 GMT"
|
2019-07-14 21:36:51 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:56:48 +02:00
|
|
|
- acitivity not found:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 404 NOT FOUND
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": []
|
2019-07-19 11:56:48 +02:00
|
|
|
},
|
|
|
|
"status": "not found"
|
|
|
|
}
|
2021-12-01 20:31:42 +01:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 200: success
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
|
|
|
:statuscode 403: you do not have permissions
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 404: workout not found
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_uuid = decode_short_id(workout_short_id)
|
|
|
|
workout = Workout.query.filter_by(uuid=workout_uuid).first()
|
|
|
|
if not workout:
|
|
|
|
return DataNotFoundErrorResponse('workouts')
|
2018-05-01 21:26:17 +02:00
|
|
|
|
2021-12-01 19:22:47 +01:00
|
|
|
error_response = can_view_workout(auth_user.id, workout.user_id)
|
2021-01-01 16:39:25 +01:00
|
|
|
if error_response:
|
|
|
|
return error_response
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
2021-01-10 11:16:43 +01:00
|
|
|
'data': {'workouts': [workout.serialize()]},
|
2018-05-02 17:01:31 +02:00
|
|
|
}
|
2018-05-01 21:26:17 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workout_data(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User,
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_short_id: str,
|
2021-01-02 19:28:03 +01:00
|
|
|
data_type: str,
|
|
|
|
segment_id: Optional[int] = None,
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
2021-01-10 11:16:43 +01:00
|
|
|
"""Get data from an workout gpx file"""
|
|
|
|
workout_uuid = decode_short_id(workout_short_id)
|
|
|
|
workout = Workout.query.filter_by(uuid=workout_uuid).first()
|
|
|
|
if not workout:
|
2021-01-01 16:39:25 +01:00
|
|
|
return DataNotFoundErrorResponse(
|
|
|
|
data_type=data_type,
|
2021-11-01 09:44:10 +01:00
|
|
|
message=f'workout not found (id: {workout_short_id})',
|
2019-08-28 13:25:39 +02:00
|
|
|
)
|
2021-01-01 16:39:25 +01:00
|
|
|
|
2021-12-01 19:22:47 +01:00
|
|
|
error_response = can_view_workout(auth_user.id, workout.user_id)
|
2021-01-01 16:39:25 +01:00
|
|
|
if error_response:
|
|
|
|
return error_response
|
2021-01-10 11:16:43 +01:00
|
|
|
if not workout.gpx or workout.gpx == '':
|
2021-01-01 16:39:25 +01:00
|
|
|
return NotFoundErrorResponse(
|
2021-11-01 09:44:10 +01:00
|
|
|
f'no gpx file for this workout (id: {workout_short_id})'
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2021-01-10 11:16:43 +01:00
|
|
|
absolute_gpx_filepath = get_absolute_file_path(workout.gpx)
|
2021-01-02 19:28:03 +01:00
|
|
|
chart_data_content: Optional[List] = []
|
2021-01-01 16:39:25 +01:00
|
|
|
if data_type == 'chart_data':
|
2021-01-02 19:28:03 +01:00
|
|
|
chart_data_content = get_chart_data(
|
|
|
|
absolute_gpx_filepath, segment_id
|
|
|
|
)
|
2021-01-01 16:39:25 +01:00
|
|
|
else: # data_type == 'gpx'
|
|
|
|
with open(absolute_gpx_filepath, encoding='utf-8') as f:
|
2021-01-02 19:28:03 +01:00
|
|
|
gpx_content = f.read()
|
2021-01-01 16:39:25 +01:00
|
|
|
if segment_id is not None:
|
2021-01-02 19:28:03 +01:00
|
|
|
gpx_segment_content = extract_segment_from_gpx_file(
|
|
|
|
gpx_content, segment_id
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
except WorkoutGPXException as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
appLog.error(e.message)
|
|
|
|
if e.status == 'not found':
|
|
|
|
return NotFoundErrorResponse(e.message)
|
|
|
|
return InternalServerErrorResponse(e.message)
|
|
|
|
except Exception as e:
|
|
|
|
return handle_error_and_return_response(e)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
|
|
|
'message': '',
|
2021-01-02 19:28:03 +01:00
|
|
|
'data': (
|
|
|
|
{
|
|
|
|
data_type: chart_data_content
|
|
|
|
if data_type == 'chart_data'
|
|
|
|
else gpx_content
|
|
|
|
if segment_id is None
|
|
|
|
else gpx_segment_content
|
|
|
|
}
|
|
|
|
),
|
2019-08-28 13:25:39 +02:00
|
|
|
}
|
2018-05-28 14:38:32 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>/gpx', methods=['GET']
|
2018-05-28 14:57:41 +02:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workout_gpx(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-19 11:56:48 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get gpx file for an workout displayed on map with Leaflet
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/gpx HTTP/1.1
|
2019-07-19 11:56:48 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"gpx": "gpx file content"
|
|
|
|
},
|
|
|
|
"message": "",
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 200: success
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2020-05-10 15:55:56 +02:00
|
|
|
:statuscode 404:
|
2021-01-10 11:16:43 +01:00
|
|
|
- workout not found
|
|
|
|
- no gpx file for this workout
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-12-01 19:22:47 +01:00
|
|
|
return get_workout_data(auth_user, workout_short_id, 'gpx')
|
2018-05-28 14:57:41 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>/chart_data', methods=['GET']
|
2018-05-28 14:38:32 +02:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workout_chart_data(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-19 11:56:48 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get chart data from an workout gpx file, to display it with Recharts
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/chart HTTP/1.1
|
2019-07-19 11:56:48 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"chart_data": [
|
|
|
|
{
|
|
|
|
"distance": 0,
|
|
|
|
"duration": 0,
|
|
|
|
"elevation": 279.4,
|
2019-08-25 15:05:10 +02:00
|
|
|
"latitude": 51.5078118,
|
|
|
|
"longitude": -0.1232004,
|
2019-07-19 11:56:48 +02:00
|
|
|
"speed": 8.63,
|
|
|
|
"time": "Fri, 14 Jul 2017 13:44:03 GMT"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"distance": 7.5,
|
|
|
|
"duration": 7380,
|
|
|
|
"elevation": 280,
|
2019-08-25 15:05:10 +02:00
|
|
|
"latitude": 51.5079733,
|
|
|
|
"longitude": -0.1234538,
|
2019-07-19 11:56:48 +02:00
|
|
|
"speed": 6.39,
|
|
|
|
"time": "Fri, 14 Jul 2017 15:47:03 GMT"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"message": "",
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 200: success
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2020-05-10 15:55:56 +02:00
|
|
|
:statuscode 404:
|
2021-01-10 11:16:43 +01:00
|
|
|
- workout not found
|
|
|
|
- no gpx file for this workout
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-12-01 19:22:47 +01:00
|
|
|
return get_workout_data(auth_user, workout_short_id, 'chart_data')
|
2018-05-03 21:42:54 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>/gpx/segment/<int:segment_id>',
|
2019-08-28 13:25:39 +02:00
|
|
|
methods=['GET'],
|
2019-08-25 18:54:33 +02:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-01-02 19:28:03 +01:00
|
|
|
def get_segment_gpx(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str, segment_id: int
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-08-25 18:54:33 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get gpx file for an workout segment displayed on map with Leaflet
|
2019-08-25 18:54:33 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/gpx/segment/0 HTTP/1.1
|
2019-08-25 18:54:33 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"gpx": "gpx file content"
|
|
|
|
},
|
|
|
|
"message": "",
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-08-25 18:54:33 +02:00
|
|
|
:param integer segment_id: segment id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 200: success
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 400: no gpx file for this workout
|
2019-08-25 18:54:33 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 404: workout not found
|
2019-08-25 18:54:33 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-12-01 19:22:47 +01:00
|
|
|
return get_workout_data(auth_user, workout_short_id, 'gpx', segment_id)
|
2019-08-25 18:54:33 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>/chart_data/segment/'
|
2020-12-30 22:07:43 +01:00
|
|
|
'<int:segment_id>',
|
2019-08-28 13:25:39 +02:00
|
|
|
methods=['GET'],
|
2019-08-25 18:54:33 +02:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-01-02 19:28:03 +01:00
|
|
|
def get_segment_chart_data(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str, segment_id: int
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-08-25 18:54:33 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get chart data from an workout gpx file, to display it with Recharts
|
2019-08-25 18:54:33 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/chart/segment/0 HTTP/1.1
|
2019-08-25 18:54:33 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"chart_data": [
|
|
|
|
{
|
|
|
|
"distance": 0,
|
|
|
|
"duration": 0,
|
|
|
|
"elevation": 279.4,
|
|
|
|
"latitude": 51.5078118,
|
|
|
|
"longitude": -0.1232004,
|
|
|
|
"speed": 8.63,
|
|
|
|
"time": "Fri, 14 Jul 2017 13:44:03 GMT"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"distance": 7.5,
|
|
|
|
"duration": 7380,
|
|
|
|
"elevation": 280,
|
|
|
|
"latitude": 51.5079733,
|
|
|
|
"longitude": -0.1234538,
|
|
|
|
"speed": 6.39,
|
|
|
|
"time": "Fri, 14 Jul 2017 15:47:03 GMT"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"message": "",
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-08-25 18:54:33 +02:00
|
|
|
:param integer segment_id: segment id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 200: success
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 400: no gpx file for this workout
|
2019-08-25 18:54:33 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 404: workout not found
|
2019-08-25 18:54:33 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
return get_workout_data(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user, workout_short_id, 'chart_data', segment_id
|
2020-12-30 22:07:43 +01:00
|
|
|
)
|
2019-08-25 18:54:33 +02:00
|
|
|
|
|
|
|
|
2021-11-29 16:54:24 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>/gpx/download', methods=['GET']
|
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='read')
|
2021-11-29 16:54:24 +01:00
|
|
|
def download_workout_gpx(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-11-29 16:54:24 +01:00
|
|
|
) -> Union[HttpResponse, Response]:
|
|
|
|
"""
|
|
|
|
Download gpx file
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/gpx/download HTTP/1.1
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/gpx+xml
|
|
|
|
|
|
|
|
:param string workout_short_id: workout short id
|
|
|
|
|
|
|
|
:statuscode 200: success
|
|
|
|
:statuscode 401:
|
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
|
|
|
:statuscode 404:
|
|
|
|
- workout not found
|
|
|
|
- no gpx file for workout
|
|
|
|
"""
|
|
|
|
workout_uuid = decode_short_id(workout_short_id)
|
|
|
|
workout = Workout.query.filter_by(
|
2021-12-01 19:22:47 +01:00
|
|
|
uuid=workout_uuid, user_id=auth_user.id
|
2021-11-29 16:54:24 +01:00
|
|
|
).first()
|
|
|
|
if not workout:
|
|
|
|
return DataNotFoundErrorResponse(
|
|
|
|
data_type='workout',
|
|
|
|
message=f'workout not found (id: {workout_short_id})',
|
|
|
|
)
|
|
|
|
|
|
|
|
if workout.gpx is None:
|
|
|
|
return DataNotFoundErrorResponse(
|
|
|
|
data_type='gpx',
|
|
|
|
message=f'no gpx file for workout (id: {workout_short_id})',
|
|
|
|
)
|
|
|
|
|
|
|
|
return send_from_directory(
|
|
|
|
current_app.config['UPLOAD_FOLDER'],
|
|
|
|
workout.gpx,
|
|
|
|
mimetype='application/gpx+xml',
|
|
|
|
as_attachment=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route('/workouts/map/<map_id>', methods=['GET'])
|
2021-11-30 09:02:41 +01:00
|
|
|
def get_map(map_id: int) -> Union[HttpResponse, Response]:
|
2019-07-19 11:56:48 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Get map image for workouts with gpx
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/map/fa33f4d996844a5c73ecd1ae24456ab8?1563529507772
|
2019-07-19 11:56:48 +02:00
|
|
|
HTTP/1.1
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: image/png
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string map_id: workout map id
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
:statuscode 200: success
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 404: map does not exist
|
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2018-05-30 13:35:27 +02:00
|
|
|
try:
|
2021-01-10 11:16:43 +01:00
|
|
|
workout = Workout.query.filter_by(map_id=map_id).first()
|
|
|
|
if not workout:
|
2021-01-01 16:39:25 +01:00
|
|
|
return NotFoundErrorResponse('Map does not exist.')
|
2021-11-30 09:02:41 +01:00
|
|
|
return send_from_directory(
|
|
|
|
current_app.config['UPLOAD_FOLDER'],
|
|
|
|
workout.map,
|
|
|
|
)
|
2018-05-30 13:35:27 +02:00
|
|
|
except Exception as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e)
|
2018-05-30 13:35:27 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/map_tile/<s>/<z>/<x>/<y>.png', methods=['GET']
|
2020-09-16 11:47:20 +02:00
|
|
|
)
|
2021-01-02 19:28:03 +01:00
|
|
|
def get_map_tile(s: str, z: str, x: str, y: str) -> Tuple[Response, int]:
|
2020-09-16 11:47:20 +02:00
|
|
|
"""
|
|
|
|
Get map tile from tile server.
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/workouts/map_tile/c/13/4109/2930.png HTTP/1.1
|
2020-09-16 11:47:20 +02:00
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: image/png
|
|
|
|
|
|
|
|
:param string s: subdomain
|
|
|
|
:param string z: zoom
|
|
|
|
:param string x: index of the tile along the map's x axis
|
|
|
|
:param string y: index of the tile along the map's y axis
|
|
|
|
|
|
|
|
Status codes are status codes returned by tile server
|
|
|
|
|
|
|
|
"""
|
2022-02-13 12:01:08 +01:00
|
|
|
url = current_app.config['TILE_SERVER']['URL'].format(
|
|
|
|
s=secure_filename(s),
|
|
|
|
z=secure_filename(z),
|
|
|
|
x=secure_filename(x),
|
|
|
|
y=secure_filename(y),
|
|
|
|
)
|
2021-05-22 17:37:44 +02:00
|
|
|
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:88.0)'}
|
2020-09-16 11:47:20 +02:00
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
return (
|
|
|
|
Response(
|
|
|
|
response.content,
|
|
|
|
content_type=response.headers['content-type'],
|
|
|
|
),
|
|
|
|
response.status_code,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route('/workouts', methods=['POST'])
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='write')
|
2021-12-01 19:22:47 +01:00
|
|
|
def post_workout(auth_user: User) -> Union[Tuple[Dict, int], HttpResponse]:
|
2019-07-14 21:36:51 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Post an workout with a gpx file
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
POST /api/workouts/ HTTP/1.1
|
2019-07-14 21:36:51 +02:00
|
|
|
Content-Type: multipart/form-data
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 201 CREATED
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": [
|
2019-07-14 21:36:51 +02:00
|
|
|
{
|
|
|
|
"ascent": null,
|
|
|
|
"ave_speed": 10.0,
|
|
|
|
"bounds": [],
|
|
|
|
"creation_date": "Sun, 14 Jul 2019 13:51:01 GMT",
|
|
|
|
"descent": null,
|
|
|
|
"distance": 10.0,
|
|
|
|
"duration": "0:17:04",
|
2020-12-30 22:07:43 +01:00
|
|
|
"id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-14 21:36:51 +02:00
|
|
|
"map": null,
|
|
|
|
"max_alt": null,
|
|
|
|
"max_speed": 10.0,
|
|
|
|
"min_alt": null,
|
|
|
|
"modification_date": null,
|
|
|
|
"moving": "0:17:04",
|
2021-01-10 11:16:43 +01:00
|
|
|
"next_workout": 3,
|
2019-07-14 21:36:51 +02:00
|
|
|
"notes": null,
|
|
|
|
"pauses": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"previous_workout": null,
|
2019-07-14 21:36:51 +02:00
|
|
|
"records": [
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"record_type": "MS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"record_type": "LD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": "0:17:04",
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"record_type": "FD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"record_type": "AS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"segments": [],
|
|
|
|
"sport_id": 1,
|
|
|
|
"title": null,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2019-07-14 21:36:51 +02:00
|
|
|
"weather_end": null,
|
|
|
|
"weather_start": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"with_gpx": false,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT"
|
2019-07-14 21:36:51 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2019-07-20 14:27:05 +02:00
|
|
|
:form file: gpx file (allowed extensions: .gpx, .zip)
|
2019-07-14 21:36:51 +02:00
|
|
|
:form data: sport id and notes (example: ``{"sport_id": 1, "notes": ""}``)
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 201: workout created
|
2019-07-20 14:27:05 +02:00
|
|
|
:statuscode 400:
|
2021-11-01 09:44:10 +01:00
|
|
|
- invalid payload
|
|
|
|
- no file part
|
|
|
|
- no selected file
|
|
|
|
- file extension not allowed
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
|
|
|
:statuscode 413: error during picture update: file size exceeds 1.0MB
|
2019-07-14 21:36:51 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-02-20 21:37:31 +01:00
|
|
|
try:
|
2022-02-16 13:03:14 +01:00
|
|
|
error_response = get_error_response_if_file_is_invalid(
|
|
|
|
'workout', request
|
|
|
|
)
|
2021-02-20 21:37:31 +01:00
|
|
|
except RequestEntityTooLarge as e:
|
|
|
|
appLog.error(e)
|
|
|
|
return PayloadTooLargeErrorResponse(
|
|
|
|
file_type='workout',
|
|
|
|
file_size=request.content_length,
|
|
|
|
max_size=current_app.config['MAX_CONTENT_LENGTH'],
|
|
|
|
)
|
2021-01-01 16:39:25 +01:00
|
|
|
if error_response:
|
|
|
|
return error_response
|
2018-05-01 17:51:38 +02:00
|
|
|
|
2021-07-14 14:29:10 +02:00
|
|
|
workout_data = json.loads(request.form['data'], strict=False)
|
2021-01-10 11:16:43 +01:00
|
|
|
if not workout_data or workout_data.get('sport_id') is None:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2018-05-01 17:51:38 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_file = request.files['file']
|
2018-05-29 19:06:33 +02:00
|
|
|
upload_dir = os.path.join(
|
2021-12-01 19:22:47 +01:00
|
|
|
current_app.config['UPLOAD_FOLDER'], 'workouts', str(auth_user.id)
|
2019-08-28 13:25:39 +02:00
|
|
|
)
|
2018-05-29 19:06:33 +02:00
|
|
|
folders = {
|
|
|
|
'extract_dir': os.path.join(upload_dir, 'extract'),
|
|
|
|
'tmp_dir': os.path.join(upload_dir, 'tmp'),
|
|
|
|
}
|
2018-05-01 17:51:38 +02:00
|
|
|
|
|
|
|
try:
|
2021-01-10 11:16:43 +01:00
|
|
|
new_workouts = process_files(
|
2021-12-01 19:31:42 +01:00
|
|
|
auth_user, workout_data, workout_file, folders
|
2018-05-11 13:45:54 +02:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
if len(new_workouts) > 0:
|
2018-05-29 16:28:59 +02:00
|
|
|
response_object = {
|
|
|
|
'status': 'created',
|
|
|
|
'data': {
|
2021-01-10 11:16:43 +01:00
|
|
|
'workouts': [
|
|
|
|
new_workout.serialize() for new_workout in new_workouts
|
2019-08-28 13:25:39 +02:00
|
|
|
]
|
|
|
|
},
|
2018-05-02 17:01:31 +02:00
|
|
|
}
|
2018-05-29 16:28:59 +02:00
|
|
|
else:
|
2021-01-10 11:16:43 +01:00
|
|
|
return DataInvalidPayloadErrorResponse('workouts', 'fail')
|
|
|
|
except WorkoutException as e:
|
2018-05-01 17:51:38 +02:00
|
|
|
db.session.rollback()
|
2018-05-29 12:53:13 +02:00
|
|
|
if e.e:
|
|
|
|
appLog.error(e.e)
|
2021-01-01 16:39:25 +01:00
|
|
|
if e.status == 'error':
|
|
|
|
return InternalServerErrorResponse(e.message)
|
|
|
|
return InvalidPayloadErrorResponse(e.message)
|
2018-05-29 19:06:33 +02:00
|
|
|
|
|
|
|
shutil.rmtree(folders['extract_dir'], ignore_errors=True)
|
|
|
|
shutil.rmtree(folders['tmp_dir'], ignore_errors=True)
|
2021-01-01 16:39:25 +01:00
|
|
|
return response_object, 201
|
2018-05-08 15:46:54 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route('/workouts/no_gpx', methods=['POST'])
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='write')
|
2021-01-10 11:16:43 +01:00
|
|
|
def post_workout_no_gpx(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User,
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Tuple[Dict, int], HttpResponse]:
|
2019-07-14 21:36:51 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Post an workout without gpx file
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
POST /api/workouts/no_gpx HTTP/1.1
|
2019-07-14 21:36:51 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 201 CREATED
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": [
|
2019-07-14 21:36:51 +02:00
|
|
|
{
|
|
|
|
"ascent": null,
|
|
|
|
"ave_speed": 10.0,
|
|
|
|
"bounds": [],
|
|
|
|
"creation_date": "Sun, 14 Jul 2019 13:51:01 GMT",
|
|
|
|
"descent": null,
|
|
|
|
"distance": 10.0,
|
|
|
|
"duration": "0:17:04",
|
|
|
|
"map": null,
|
|
|
|
"max_alt": null,
|
|
|
|
"max_speed": 10.0,
|
|
|
|
"min_alt": null,
|
|
|
|
"modification_date": null,
|
|
|
|
"moving": "0:17:04",
|
2021-01-10 11:16:43 +01:00
|
|
|
"next_workout": 3,
|
2019-07-14 21:36:51 +02:00
|
|
|
"notes": null,
|
|
|
|
"pauses": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"previous_workout": null,
|
2019-07-14 21:36:51 +02:00
|
|
|
"records": [
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"record_type": "MS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"record_type": "LD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": "0:17:04",
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"record_type": "FD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"record_type": "AS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"segments": [],
|
|
|
|
"sport_id": 1,
|
|
|
|
"title": null,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2020-12-30 22:07:43 +01:00
|
|
|
"uuid": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-14 21:36:51 +02:00
|
|
|
"weather_end": null,
|
|
|
|
"weather_start": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"with_gpx": false,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT"
|
2019-07-14 21:36:51 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:<json string workout_date: workout date (format: ``%Y-%m-%d %H:%M``)
|
|
|
|
:<json float distance: workout distance in km
|
|
|
|
:<json integer duration: workout duration in seconds
|
2019-07-19 11:56:48 +02:00
|
|
|
:<json string notes: notes (not mandatory)
|
2021-01-10 11:16:43 +01:00
|
|
|
:<json integer sport_id: workout sport id
|
|
|
|
:<json string title: workout title
|
2019-07-14 21:36:51 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 201: workout created
|
2019-07-14 21:36:51 +02:00
|
|
|
:statuscode 400: invalid payload
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2019-07-14 21:36:51 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_data = request.get_json()
|
2019-08-28 13:25:39 +02:00
|
|
|
if (
|
2021-01-10 11:16:43 +01:00
|
|
|
not workout_data
|
|
|
|
or workout_data.get('sport_id') is None
|
|
|
|
or workout_data.get('duration') is None
|
|
|
|
or workout_data.get('distance') is None
|
|
|
|
or workout_data.get('workout_date') is None
|
2019-08-28 13:25:39 +02:00
|
|
|
):
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2018-05-08 15:46:54 +02:00
|
|
|
|
|
|
|
try:
|
2021-12-01 19:22:47 +01:00
|
|
|
new_workout = create_workout(auth_user, workout_data)
|
2021-01-10 11:16:43 +01:00
|
|
|
db.session.add(new_workout)
|
2018-05-08 15:46:54 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2021-01-01 16:39:25 +01:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
'status': 'created',
|
2021-01-10 11:16:43 +01:00
|
|
|
'data': {'workouts': [new_workout.serialize()]},
|
2021-01-01 16:39:25 +01:00
|
|
|
},
|
|
|
|
201,
|
|
|
|
)
|
2018-05-08 15:46:54 +02:00
|
|
|
|
|
|
|
except (exc.IntegrityError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(
|
|
|
|
error=e,
|
2021-01-10 11:16:43 +01:00
|
|
|
message='Error during workout save.',
|
2021-01-01 16:39:25 +01:00
|
|
|
status='fail',
|
|
|
|
db=db,
|
|
|
|
)
|
2018-05-09 18:54:30 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>', methods=['PATCH']
|
2020-12-30 19:37:59 +01:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='write')
|
2021-01-10 11:16:43 +01:00
|
|
|
def update_workout(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-19 11:56:48 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Update an workout
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
PATCH /api/workouts/1 HTTP/1.1
|
2019-07-19 11:56:48 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": [
|
2019-07-19 11:56:48 +02:00
|
|
|
{
|
|
|
|
"ascent": null,
|
|
|
|
"ave_speed": 10.0,
|
|
|
|
"bounds": [],
|
|
|
|
"creation_date": "Sun, 14 Jul 2019 13:51:01 GMT",
|
|
|
|
"descent": null,
|
|
|
|
"distance": 10.0,
|
|
|
|
"duration": "0:17:04",
|
|
|
|
"map": null,
|
|
|
|
"max_alt": null,
|
|
|
|
"max_speed": 10.0,
|
|
|
|
"min_alt": null,
|
|
|
|
"modification_date": null,
|
|
|
|
"moving": "0:17:04",
|
2021-01-10 11:16:43 +01:00
|
|
|
"next_workout": 3,
|
2019-07-19 11:56:48 +02:00
|
|
|
"notes": null,
|
|
|
|
"pauses": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"previous_workout": null,
|
2019-07-19 11:56:48 +02:00
|
|
|
"records": [
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"record_type": "MS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-19 11:56:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"record_type": "LD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": "0:17:04",
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-19 11:56:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"record_type": "FD",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-19 11:56:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"record_type": "AS",
|
|
|
|
"sport_id": 1,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2021-01-10 11:16:43 +01:00
|
|
|
"value": 10.0,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT",
|
|
|
|
"workout_id": "kjxavSTUrJvoAh2wvCeGEF",
|
2019-07-19 11:56:48 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"segments": [],
|
|
|
|
"sport_id": 1,
|
|
|
|
"title": null,
|
2020-02-08 15:17:07 +01:00
|
|
|
"user": "admin",
|
2020-12-30 22:07:43 +01:00
|
|
|
"uuid": "kjxavSTUrJvoAh2wvCeGEF"
|
2019-07-19 11:56:48 +02:00
|
|
|
"weather_end": null,
|
|
|
|
"weather_start": null,
|
2021-01-10 11:16:43 +01:00
|
|
|
"with_gpx": false,
|
|
|
|
"workout_date": "Mon, 01 Jan 2018 00:00:00 GMT"
|
2019-07-19 11:56:48 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
|
|
|
|
|
|
|
:<json string workout_date: workout date (format: ``%Y-%m-%d %H:%M``)
|
|
|
|
(only for workout without gpx)
|
|
|
|
:<json float distance: workout distance in km
|
|
|
|
(only for workout without gpx)
|
|
|
|
:<json integer duration: workout duration in seconds
|
|
|
|
(only for workout without gpx)
|
2019-07-19 11:56:48 +02:00
|
|
|
:<json string notes: notes
|
2021-01-10 11:16:43 +01:00
|
|
|
:<json integer sport_id: workout sport id
|
|
|
|
:<json string title: workout title
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 200: workout updated
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 400: invalid payload
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 404: workout not found
|
2019-07-19 11:56:48 +02:00
|
|
|
:statuscode 500:
|
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_data = request.get_json()
|
|
|
|
if not workout_data:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2018-05-09 20:45:01 +02:00
|
|
|
|
|
|
|
try:
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_uuid = decode_short_id(workout_short_id)
|
|
|
|
workout = Workout.query.filter_by(uuid=workout_uuid).first()
|
|
|
|
if not workout:
|
|
|
|
return DataNotFoundErrorResponse('workouts')
|
2018-06-15 10:50:37 +02:00
|
|
|
|
2021-12-01 19:22:47 +01:00
|
|
|
response_object = can_view_workout(auth_user.id, workout.user_id)
|
2021-01-01 16:39:25 +01:00
|
|
|
if response_object:
|
|
|
|
return response_object
|
|
|
|
|
2021-12-01 20:17:15 +01:00
|
|
|
workout = edit_workout(workout, workout_data, auth_user)
|
2021-01-01 16:39:25 +01:00
|
|
|
db.session.commit()
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
2021-01-10 11:16:43 +01:00
|
|
|
'data': {'workouts': [workout.serialize()]},
|
2018-05-09 20:45:01 +02:00
|
|
|
}
|
2021-01-01 16:39:25 +01:00
|
|
|
|
|
|
|
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
|
|
|
return handle_error_and_return_response(e)
|
2018-05-09 20:45:01 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
@workouts_blueprint.route(
|
|
|
|
'/workouts/<string:workout_short_id>', methods=['DELETE']
|
2018-05-09 18:54:30 +02:00
|
|
|
)
|
2022-05-27 18:19:12 +02:00
|
|
|
@require_auth(scopes='write')
|
2021-01-10 11:16:43 +01:00
|
|
|
def delete_workout(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, workout_short_id: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Tuple[Dict, int], HttpResponse]:
|
2019-07-19 11:56:48 +02:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Delete an workout
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
DELETE /api/workouts/kjxavSTUrJvoAh2wvCeGEF HTTP/1.1
|
2019-07-19 11:56:48 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 204 NO CONTENT
|
|
|
|
Content-Type: application/json
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:param string workout_short_id: workout short id
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 204: workout deleted
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2021-01-10 11:16:43 +01:00
|
|
|
:statuscode 404: workout not found
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 500: error, please try again or contact the administrator
|
2019-07-19 11:56:48 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2018-05-09 18:54:30 +02:00
|
|
|
try:
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_uuid = decode_short_id(workout_short_id)
|
|
|
|
workout = Workout.query.filter_by(uuid=workout_uuid).first()
|
|
|
|
if not workout:
|
|
|
|
return DataNotFoundErrorResponse('workouts')
|
2021-12-01 19:22:47 +01:00
|
|
|
error_response = can_view_workout(auth_user.id, workout.user_id)
|
2021-01-01 16:39:25 +01:00
|
|
|
if error_response:
|
|
|
|
return error_response
|
2018-06-15 10:50:37 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
db.session.delete(workout)
|
2021-01-01 16:39:25 +01:00
|
|
|
db.session.commit()
|
|
|
|
return {'status': 'no content'}, 204
|
2019-08-28 13:25:39 +02:00
|
|
|
except (
|
|
|
|
exc.IntegrityError,
|
|
|
|
exc.OperationalError,
|
|
|
|
ValueError,
|
|
|
|
OSError,
|
|
|
|
) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|