API: fetch activities w/ date filter
This commit is contained in:
parent
a353e3be80
commit
179befac71
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import Blueprint, current_app, jsonify, request, send_file
|
from flask import Blueprint, current_app, jsonify, request, send_file
|
||||||
from mpwo_api import appLog, db
|
from mpwo_api import appLog, db
|
||||||
@ -22,10 +23,20 @@ def get_activities(auth_user_id):
|
|||||||
"""Get all activities for authenticated user"""
|
"""Get all activities for authenticated user"""
|
||||||
try:
|
try:
|
||||||
params = request.args.copy()
|
params = request.args.copy()
|
||||||
page = 1 if len(params) == 0 else int(params.pop('page'))
|
page = 1 if 'page' not in params.keys() else int(params.get('page'))
|
||||||
activities = Activity.query.filter_by(user_id=auth_user_id)\
|
date_from = params.get('from')
|
||||||
.order_by(Activity.activity_date.desc()).paginate(
|
date_to = params.get('to')
|
||||||
page, 5, False).items
|
activities = Activity.query.filter(
|
||||||
|
Activity.user_id == auth_user_id,
|
||||||
|
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')
|
||||||
|
if date_to else True,
|
||||||
|
).order_by(
|
||||||
|
Activity.activity_date.desc()
|
||||||
|
).paginate(
|
||||||
|
page, 5, False
|
||||||
|
).items
|
||||||
response_object = {
|
response_object = {
|
||||||
'status': 'success',
|
'status': 'success',
|
||||||
'data': {
|
'data': {
|
||||||
|
@ -196,6 +196,154 @@ def test_get_activities_pagination_error(
|
|||||||
assert 'Error. Please try again or contact the administrator.' in data['message'] # noqa
|
assert 'Error. Please try again or contact the administrator.' in data['message'] # noqa
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_date_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?from=2018-02-01&to=2018-02-28',
|
||||||
|
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 'creation_date' in data['data']['activities'][0]
|
||||||
|
assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
|
||||||
|
assert '0:10:00' == data['data']['activities'][0]['duration']
|
||||||
|
assert 'creation_date' in data['data']['activities'][1]
|
||||||
|
assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
|
||||||
|
assert '0:16:40' == data['data']['activities'][1]['duration']
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_date_filter_no_results(
|
||||||
|
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?from=2018-03-01&to=2018-03-30',
|
||||||
|
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']) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_date_filter_from(
|
||||||
|
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?from=2018-04-01',
|
||||||
|
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 'creation_date' in data['data']['activities'][0]
|
||||||
|
assert 'Wed, 09 May 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
|
||||||
|
assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_activities_date_filter_to(
|
||||||
|
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?to=2017-12-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 'Thu, 01 Jun 2017 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_date_filter_paginate(
|
||||||
|
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?from=2017-01-01&page=2',
|
||||||
|
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 'Thu, 01 Jun 2017 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_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
|
||||||
):
|
):
|
||||||
|
Loading…
Reference in New Issue
Block a user