API: add filters for activities - #3
This commit is contained in:
parent
eab356a2c4
commit
479b47455f
@ -10,8 +10,8 @@ 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 (
|
||||||
ActivityException, create_activity, edit_activity, get_chart_data,
|
ActivityException, convert_in_duration, create_activity, edit_activity,
|
||||||
process_files
|
get_chart_data, process_files
|
||||||
)
|
)
|
||||||
|
|
||||||
activities_blueprint = Blueprint('activities', __name__)
|
activities_blueprint = Blueprint('activities', __name__)
|
||||||
@ -26,6 +26,12 @@ def get_activities(auth_user_id):
|
|||||||
page = 1 if 'page' not in params.keys() else int(params.get('page'))
|
page = 1 if 'page' not in params.keys() else int(params.get('page'))
|
||||||
date_from = params.get('from')
|
date_from = params.get('from')
|
||||||
date_to = params.get('to')
|
date_to = params.get('to')
|
||||||
|
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')
|
||||||
order = params.get('order')
|
order = params.get('order')
|
||||||
per_page = int(params.get('per_page')) if params.get('per_page') else 5
|
per_page = int(params.get('per_page')) if params.get('per_page') else 5
|
||||||
activities = Activity.query.filter(
|
activities = Activity.query.filter(
|
||||||
@ -34,6 +40,16 @@ def get_activities(auth_user_id):
|
|||||||
if date_from else True,
|
if date_from else True,
|
||||||
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d')
|
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d')
|
||||||
if date_to else True,
|
if date_to else True,
|
||||||
|
Activity.distance >= int(distance_from) if distance_from else True,
|
||||||
|
Activity.distance <= int(distance_to) if distance_to else True,
|
||||||
|
Activity.duration >= convert_in_duration(duration_from)
|
||||||
|
if duration_from else True,
|
||||||
|
Activity.duration <= convert_in_duration(duration_to)
|
||||||
|
if duration_to else True,
|
||||||
|
Activity.ave_speed >= float(ave_speed_from)
|
||||||
|
if ave_speed_from else True,
|
||||||
|
Activity.ave_speed <= float(ave_speed_to)
|
||||||
|
if ave_speed_to else True,
|
||||||
).order_by(
|
).order_by(
|
||||||
Activity.activity_date.asc()
|
Activity.activity_date.asc()
|
||||||
if order == 'asc'
|
if order == 'asc'
|
||||||
|
@ -360,3 +360,9 @@ def process_files(auth_user_id, activity_data, activity_file, folders):
|
|||||||
return [process_one_gpx_file(common_params, filename)]
|
return [process_one_gpx_file(common_params, filename)]
|
||||||
else:
|
else:
|
||||||
return process_zip_archive(common_params, folders['extract_dir'])
|
return process_zip_archive(common_params, folders['extract_dir'])
|
||||||
|
|
||||||
|
|
||||||
|
def convert_in_duration(value):
|
||||||
|
hours = int(value.split(':')[0])
|
||||||
|
minutes = int(value.split(':')[1])
|
||||||
|
return timedelta(seconds=(hours*3600 + minutes*60))
|
||||||
|
@ -132,6 +132,7 @@ def seven_activities_user_1():
|
|||||||
distance=5,
|
distance=5,
|
||||||
duration=datetime.timedelta(seconds=1024)
|
duration=datetime.timedelta(seconds=1024)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (1024 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -141,6 +142,7 @@ def seven_activities_user_1():
|
|||||||
distance=10,
|
distance=10,
|
||||||
duration=datetime.timedelta(seconds=3456)
|
duration=datetime.timedelta(seconds=3456)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (3456 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -150,6 +152,7 @@ def seven_activities_user_1():
|
|||||||
distance=10,
|
distance=10,
|
||||||
duration=datetime.timedelta(seconds=1024)
|
duration=datetime.timedelta(seconds=1024)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (1024 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -159,6 +162,7 @@ def seven_activities_user_1():
|
|||||||
distance=1,
|
distance=1,
|
||||||
duration=datetime.timedelta(seconds=600)
|
duration=datetime.timedelta(seconds=600)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (600 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -168,6 +172,7 @@ def seven_activities_user_1():
|
|||||||
distance=10,
|
distance=10,
|
||||||
duration=datetime.timedelta(seconds=1000)
|
duration=datetime.timedelta(seconds=1000)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (1000 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -177,6 +182,7 @@ def seven_activities_user_1():
|
|||||||
distance=8,
|
distance=8,
|
||||||
duration=datetime.timedelta(seconds=6000)
|
duration=datetime.timedelta(seconds=6000)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (6000 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
activity = Activity(
|
activity = Activity(
|
||||||
@ -186,6 +192,7 @@ def seven_activities_user_1():
|
|||||||
distance=10,
|
distance=10,
|
||||||
duration=datetime.timedelta(seconds=3000)
|
duration=datetime.timedelta(seconds=3000)
|
||||||
)
|
)
|
||||||
|
activity.ave_speed = float(activity.distance) / (3000 / 3600)
|
||||||
db.session.add(activity)
|
db.session.add(activity)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return activity
|
return activity
|
||||||
|
@ -402,6 +402,91 @@ def test_get_activities_date_filter_paginate_order(
|
|||||||
assert 'Wed, 09 May 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
|
assert 'Wed, 09 May 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_distance_filter(
|
||||||
|
app, user_1, sport_1_cycling, seven_activities_user_1
|
||||||
|
):
|
||||||
|
client = app.test_client()
|
||||||
|
resp_login = client.post(
|
||||||
|
'/api/auth/login',
|
||||||
|
data=json.dumps(dict(
|
||||||
|
email='test@test.com',
|
||||||
|
password='12345678'
|
||||||
|
)),
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = client.get(
|
||||||
|
'/api/activities?distance_from=5&distance_to=8',
|
||||||
|
headers=dict(
|
||||||
|
Authorization='Bearer ' + json.loads(
|
||||||
|
resp_login.data.decode()
|
||||||
|
)['auth_token']
|
||||||
|
)
|
||||||
|
)
|
||||||
|
data = json.loads(response.data.decode())
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert 'success' in data['status']
|
||||||
|
assert len(data['data']['activities']) == 2
|
||||||
|
assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
|
||||||
|
assert 'Mon, 20 Mar 2017 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_duration_filter(
|
||||||
|
app, user_1, sport_1_cycling, seven_activities_user_1
|
||||||
|
):
|
||||||
|
client = app.test_client()
|
||||||
|
resp_login = client.post(
|
||||||
|
'/api/auth/login',
|
||||||
|
data=json.dumps(dict(
|
||||||
|
email='test@test.com',
|
||||||
|
password='12345678'
|
||||||
|
)),
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = client.get(
|
||||||
|
'/api/activities?duration_from=00:52&duration_to=01:20',
|
||||||
|
headers=dict(
|
||||||
|
Authorization='Bearer ' + json.loads(
|
||||||
|
resp_login.data.decode()
|
||||||
|
)['auth_token']
|
||||||
|
)
|
||||||
|
)
|
||||||
|
data = json.loads(response.data.decode())
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert 'success' in data['status']
|
||||||
|
assert len(data['data']['activities']) == 1
|
||||||
|
assert 'Thu, 01 Jun 2017 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_ave_speed_filter(
|
||||||
|
app, user_1, sport_1_cycling, seven_activities_user_1
|
||||||
|
):
|
||||||
|
client = app.test_client()
|
||||||
|
resp_login = client.post(
|
||||||
|
'/api/auth/login',
|
||||||
|
data=json.dumps(dict(
|
||||||
|
email='test@test.com',
|
||||||
|
password='12345678'
|
||||||
|
)),
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = client.get(
|
||||||
|
'/api/activities?ave_speed_from=5&ave_speed_to=10',
|
||||||
|
headers=dict(
|
||||||
|
Authorization='Bearer ' + json.loads(
|
||||||
|
resp_login.data.decode()
|
||||||
|
)['auth_token']
|
||||||
|
)
|
||||||
|
)
|
||||||
|
data = json.loads(response.data.decode())
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert 'success' in data['status']
|
||||||
|
assert len(data['data']['activities']) == 1
|
||||||
|
assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_get_an_activity(
|
def test_get_an_activity(
|
||||||
app, user_1, sport_1_cycling, activity_cycling_user_1
|
app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||||
):
|
):
|
||||||
|
@ -8,17 +8,8 @@ export default class FitTrackeeApi {
|
|||||||
if (data.id) {
|
if (data.id) {
|
||||||
url = `${url}/${data.id}`
|
url = `${url}/${data.id}`
|
||||||
} else if (Object.keys(data).length > 0) {
|
} else if (Object.keys(data).length > 0) {
|
||||||
url = `${url}?${
|
url += '?'
|
||||||
data.page ? `&page=${data.page}` : ''
|
Object.keys(data).map(key => url += `&${key}=${data[key]}`)
|
||||||
}${
|
|
||||||
data.start ? `&from=${data.start}` : ''
|
|
||||||
}${
|
|
||||||
data.end ? `&to=${data.end}` : ''
|
|
||||||
}${
|
|
||||||
data.order ? `&order=${data.order}` : ''
|
|
||||||
}${
|
|
||||||
data.per_page ? `&per_page=${data.per_page}` : ''
|
|
||||||
}`
|
|
||||||
}
|
}
|
||||||
const params = {
|
const params = {
|
||||||
url: url,
|
url: url,
|
||||||
|
@ -5,15 +5,8 @@ export default class FitTrackeeApi {
|
|||||||
static getStats(userID, type, data = {}) {
|
static getStats(userID, type, data = {}) {
|
||||||
let url = `${apiUrl}stats/${userID}/${type}`
|
let url = `${apiUrl}stats/${userID}/${type}`
|
||||||
if (Object.keys(data).length > 0) {
|
if (Object.keys(data).length > 0) {
|
||||||
url = `${url}?${
|
url += '?'
|
||||||
data.start ? `&from=${data.start}` : ''
|
Object.keys(data).map(key => url += `&${key}=${data[key]}`)
|
||||||
}${
|
|
||||||
data.end ? `&to=${data.end}` : ''
|
|
||||||
}${
|
|
||||||
data.time ? `&time=${data.time}` : ''
|
|
||||||
}${
|
|
||||||
data.sport_id ? `&sport_id=${data.sport_id}` : ''
|
|
||||||
}`
|
|
||||||
}
|
}
|
||||||
const params = {
|
const params = {
|
||||||
url: url,
|
url: url,
|
||||||
|
Loading…
Reference in New Issue
Block a user