API: timezone support for activities fetch - #11

=> calendar and workouts filter OK on client app
This commit is contained in:
Sam 2018-06-11 18:11:37 +02:00
parent 0a1a2082f4
commit 30a16f64b0
4 changed files with 147 additions and 8 deletions

View File

@ -11,7 +11,7 @@ from ..users.utils import authenticate, verify_extension
from .models import Activity
from .utils import (
ActivityException, create_activity, edit_activity, get_chart_data,
process_files
get_datetime_with_tz, process_files
)
from .utils_format import convert_in_duration
@ -26,7 +26,14 @@ def get_activities(auth_user_id):
params = request.args.copy()
page = 1 if 'page' not in params.keys() else int(params.get('page'))
date_from = params.get('from')
if date_from:
date_from = datetime.strptime(date_from, '%Y-%m-%d')
_, date_from = get_datetime_with_tz(auth_user_id, date_from)
date_to = params.get('to')
if date_to:
date_to = datetime.strptime(f'{date_to} 23:59:59',
'%Y-%m-%d %H:%M:%S')
_, date_to = get_datetime_with_tz(auth_user_id, date_to)
distance_from = params.get('distance_from')
distance_to = params.get('distance_to')
duration_from = params.get('duration_from')
@ -39,11 +46,9 @@ def get_activities(auth_user_id):
activities = Activity.query.filter(
Activity.user_id == auth_user_id,
Activity.sport_id == sport_id if sport_id else True,
Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d')
if date_from else True,
Activity.activity_date < (
datetime.strptime(date_to, '%Y-%m-%d') + timedelta(days=1)
) if date_to else True,
Activity.activity_date >= date_from if date_from else True,
Activity.activity_date < date_to + timedelta(seconds=1)
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)

View File

@ -56,6 +56,15 @@ def user_1_full():
return user
@pytest.fixture()
def user_1_paris():
user = User(username='test', email='test@test.com', password='12345678')
user.timezone = 'Europe/Paris'
db.session.add(user)
db.session.commit()
return user
@pytest.fixture()
def user_2():
user = User(username='toto', email='toto@toto.com', password='87654321')

View File

@ -1,5 +1,6 @@
import json
import os
from datetime import datetime
from io import BytesIO
from fittrackee_api.activities.models import Activity
@ -612,6 +613,131 @@ def test_get_an_activity_wo_gpx_with_timezone(app, user_1, sport_1_cycling):
assert data['data']['activities'][0]['title'] == 'Cycling - 2018-05-15 14:05:00' # noqa
def test_get_activities_date_filter_with_timezone_new_york(
app, user_1_full, sport_1_cycling
):
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'
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-01-01 00:00',
distance=10
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/activities?from=2018-01-01&to=2018-01-31',
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 'Mon, 01 Jan 2018 05:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
assert 'Cycling - 2018-01-01 00:00:00' == data['data']['activities'][0]['title'] # noqa
def test_get_activities_date_filter_with_timezone_paris(
app, user_1_paris, sport_1_cycling, activity_cycling_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'
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2017-31-12 23:59',
distance=10
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-01-01 00:00',
distance=10
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
activity_cycling_user_1.activity_date = datetime.strptime(
'31/01/2018 21:59:59', '%d/%m/%Y %H:%M:%S')
activity_cycling_user_1.title = 'Test'
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-02-01 00:00',
distance=10
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/activities?from=2018-01-01&to=2018-01-31',
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 'Wed, 31 Jan 2018 21:59:59 GMT' == data['data']['activities'][0]['activity_date'] # noqa
assert 'Test' == data['data']['activities'][0]['title']
assert 'Sun, 31 Dec 2017 23:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
assert 'Cycling - 2018-01-01 00:00:00' == data['data']['activities'][1]['title'] # noqa
def test_add_an_activity_no_gpx_invalid_payload(app, user_1, sport_1_cycling):
client = app.test_client()
resp_login = client.post(

View File

@ -363,10 +363,9 @@ def test_edit_an_activity_wo_gpx(
def test_edit_an_activity_wo_gpx_timezone(
app, user_1, sport_1_cycling, sport_2_running
app, user_1_paris, sport_1_cycling, sport_2_running
):
client = app.test_client()
user_1.timezone = 'Europe/Paris'
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(