FitTrackee/fittrackee/workouts/records.py

129 lines
3.2 KiB
Python
Raw Normal View History

2021-01-02 19:28:03 +01:00
from typing import Dict
2021-01-01 16:39:25 +01:00
from flask import Blueprint
from fittrackee.oauth2.server import require_auth
from fittrackee.users.models import User
2021-01-20 16:47:00 +01:00
from .models import Record
records_blueprint = Blueprint('records', __name__)
@records_blueprint.route('/records', methods=['GET'])
2022-06-15 19:16:14 +02:00
@require_auth(scopes=['workouts:read'])
def get_records(auth_user: User) -> Dict:
"""
Get all records for authenticated user.
Following types of records are available:
2022-07-27 11:10:29 +02:00
- average speed (record_type: ``AS``)
- farthest distance (record_type: ``FD``)
- highest ascent (record_type: ``HA``)
- longest duration (record_type: ``LD``)
- maximum speed (record_type: ``MS``)
**Scope**: ``workouts:read``
**Example request**:
.. sourcecode:: http
GET /api/records HTTP/1.1
Content-Type: application/json
**Example responses**:
- returning records
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"records": [
{
"id": 9,
"record_type": "AS",
"sport_id": 1,
"user": "admin",
"value": 18,
"workout_date": "Sun, 07 Jul 2019 08:00:00 GMT",
"workout_id": "hvYBqYBRa7wwXpaStWR4V2"
},
{
"id": 10,
"record_type": "FD",
"sport_id": 1,
"user": "admin",
"value": 18,
"workout_date": "Sun, 07 Jul 2019 08:00:00 GMT",
"workout_id": "hvYBqYBRa7wwXpaStWR4V2"
},
2022-07-27 11:10:29 +02:00
{
"id": 13,
"record_type": "HA",
"sport_id": 1,
"user": "Sam",
"value": 43.97,
"workout_date": "Sun, 07 Jul 2019 08:00:00 GMT",
"workout_id": "hvYBqYBRa7wwXpaStWR4V2"
},
{
"id": 11,
"record_type": "LD",
"sport_id": 1,
"user": "admin",
"value": "1:01:00",
"workout_date": "Sun, 07 Jul 2019 08:00:00 GMT",
"workout_id": "hvYBqYBRa7wwXpaStWR4V2"
},
{
"id": 12,
"record_type": "MS",
"sport_id": 1,
"user": "admin",
"value": 18,
"workout_date": "Sun, 07 Jul 2019 08:00:00 GMT",
"workout_id": "hvYBqYBRa7wwXpaStWR4V2"
}
]
},
"status": "success"
}
- no records
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"records": []
},
"status": "success"
}
:reqheader Authorization: OAuth 2.0 Bearer Token
2023-06-18 20:45:39 +02:00
:statuscode 200: ``success``
:statuscode 401:
2023-06-18 20:45:39 +02:00
- ``provide a valid auth token``
- ``signature expired, please log in again``
- ``invalid token, please log in again``
"""
2019-08-28 13:25:39 +02:00
records = (
Record.query.filter_by(user_id=auth_user.id)
2019-08-28 13:25:39 +02:00
.order_by(Record.sport_id.asc(), Record.record_type.asc())
.all()
)
2021-01-01 16:39:25 +01:00
return {
'status': 'success',
2019-08-28 13:25:39 +02:00
'data': {'records': [record.serialize() for record in records]},
}