API: add filters for activities - #3

This commit is contained in:
Sam 2018-06-07 19:28:06 +02:00
parent eab356a2c4
commit 479b47455f
6 changed files with 120 additions and 22 deletions

View File

@ -10,8 +10,8 @@ from sqlalchemy import exc
from ..users.utils import authenticate, verify_extension
from .models import Activity
from .utils import (
ActivityException, create_activity, edit_activity, get_chart_data,
process_files
ActivityException, convert_in_duration, create_activity, edit_activity,
get_chart_data, process_files
)
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'))
date_from = params.get('from')
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')
per_page = int(params.get('per_page')) if params.get('per_page') else 5
activities = Activity.query.filter(
@ -34,6 +40,16 @@ def get_activities(auth_user_id):
if date_from else True,
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d')
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(
Activity.activity_date.asc()
if order == 'asc'

View File

@ -360,3 +360,9 @@ def process_files(auth_user_id, activity_data, activity_file, folders):
return [process_one_gpx_file(common_params, filename)]
else:
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))

View File

@ -132,6 +132,7 @@ def seven_activities_user_1():
distance=5,
duration=datetime.timedelta(seconds=1024)
)
activity.ave_speed = float(activity.distance) / (1024 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -141,6 +142,7 @@ def seven_activities_user_1():
distance=10,
duration=datetime.timedelta(seconds=3456)
)
activity.ave_speed = float(activity.distance) / (3456 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -150,6 +152,7 @@ def seven_activities_user_1():
distance=10,
duration=datetime.timedelta(seconds=1024)
)
activity.ave_speed = float(activity.distance) / (1024 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -159,6 +162,7 @@ def seven_activities_user_1():
distance=1,
duration=datetime.timedelta(seconds=600)
)
activity.ave_speed = float(activity.distance) / (600 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -168,6 +172,7 @@ def seven_activities_user_1():
distance=10,
duration=datetime.timedelta(seconds=1000)
)
activity.ave_speed = float(activity.distance) / (1000 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -177,6 +182,7 @@ def seven_activities_user_1():
distance=8,
duration=datetime.timedelta(seconds=6000)
)
activity.ave_speed = float(activity.distance) / (6000 / 3600)
db.session.add(activity)
db.session.flush()
activity = Activity(
@ -186,6 +192,7 @@ def seven_activities_user_1():
distance=10,
duration=datetime.timedelta(seconds=3000)
)
activity.ave_speed = float(activity.distance) / (3000 / 3600)
db.session.add(activity)
db.session.commit()
return activity

View File

@ -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
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(
app, user_1, sport_1_cycling, activity_cycling_user_1
):

View File

@ -8,17 +8,8 @@ export default class FitTrackeeApi {
if (data.id) {
url = `${url}/${data.id}`
} else if (Object.keys(data).length > 0) {
url = `${url}?${
data.page ? `&page=${data.page}` : ''
}${
data.start ? `&from=${data.start}` : ''
}${
data.end ? `&to=${data.end}` : ''
}${
data.order ? `&order=${data.order}` : ''
}${
data.per_page ? `&per_page=${data.per_page}` : ''
}`
url += '?'
Object.keys(data).map(key => url += `&${key}=${data[key]}`)
}
const params = {
url: url,

View File

@ -5,15 +5,8 @@ export default class FitTrackeeApi {
static getStats(userID, type, data = {}) {
let url = `${apiUrl}stats/${userID}/${type}`
if (Object.keys(data).length > 0) {
url = `${url}?${
data.start ? `&from=${data.start}` : ''
}${
data.end ? `&to=${data.end}` : ''
}${
data.time ? `&time=${data.time}` : ''
}${
data.sport_id ? `&sport_id=${data.sport_id}` : ''
}`
url += '?'
Object.keys(data).map(key => url += `&${key}=${data[key]}`)
}
const params = {
url: url,