From 179befac71096dfa1c8a39e92ea1849052c10a3b Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 4 Jun 2018 11:06:52 +0200 Subject: [PATCH] API: fetch activities w/ date filter --- mpwo_api/mpwo_api/activities/activities.py | 19 ++- .../tests/test_activities_api_0_get.py | 148 ++++++++++++++++++ 2 files changed, 163 insertions(+), 4 deletions(-) diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index 236c1096..37e194f8 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -1,6 +1,7 @@ import json import os import shutil +from datetime import datetime from flask import Blueprint, current_app, jsonify, request, send_file from mpwo_api import appLog, db @@ -22,10 +23,20 @@ def get_activities(auth_user_id): """Get all activities for authenticated user""" try: params = request.args.copy() - page = 1 if len(params) == 0 else int(params.pop('page')) - activities = Activity.query.filter_by(user_id=auth_user_id)\ - .order_by(Activity.activity_date.desc()).paginate( - page, 5, False).items + page = 1 if 'page' not in params.keys() else int(params.get('page')) + date_from = params.get('from') + date_to = params.get('to') + 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 = { 'status': 'success', 'data': { diff --git a/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py b/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py index 295c131b..bcc6cc2e 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py +++ b/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py @@ -196,6 +196,154 @@ def test_get_activities_pagination_error( 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( app, user_1, sport_1_cycling, activity_cycling_user_1 ):