API & Client: display activities on calendar - #2

This commit is contained in:
Sam 2018-06-04 15:59:28 +02:00
parent ed2bf8898a
commit 747f0ad3f6
5 changed files with 62 additions and 19 deletions

View File

@ -27,6 +27,7 @@ def get_activities(auth_user_id):
date_from = params.get('from') date_from = params.get('from')
date_to = params.get('to') date_to = params.get('to')
order = params.get('order') order = params.get('order')
per_page = int(params.get('per_page')) if params.get('per_page') else 5
activities = Activity.query.filter( activities = Activity.query.filter(
Activity.user_id == auth_user_id, Activity.user_id == auth_user_id,
Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d') Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d')
@ -38,7 +39,7 @@ def get_activities(auth_user_id):
if order == 'asc' if order == 'asc'
else Activity.activity_date.desc() else Activity.activity_date.desc()
).paginate( ).paginate(
page, 5, False page, per_page, False
).items ).items
response_object = { response_object = {
'status': 'success', 'status': 'success',

View File

@ -437,6 +437,51 @@ def test_get_an_activity(
assert '0:17:04' == data['data']['activities'][0]['duration'] assert '0:17:04' == data['data']['activities'][0]['duration']
def test_get_activities_per_page(
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?per_page=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']) == 7
assert 'Wed, 09 May 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
assert 'Mon, 20 Mar 2017 00:00:00 GMT' == data['data']['activities'][6]['activity_date'] # noqa
response = client.get(
'/api/activities?per_page=3',
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']) == 3
assert 'Wed, 09 May 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][2]['activity_date'] # noqa
def test_get_an_activity_invalid_id(app, user_1): def test_get_an_activity_invalid_id(app, user_1):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(

View File

@ -130,7 +130,7 @@ export const getMoreActivities = page => dispatch => mpwoGenericApi
.catch(error => dispatch(setError(`activities: ${error}`))) .catch(error => dispatch(setError(`activities: ${error}`)))
export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi
.getData('activities', { start, end, order: 'asc' }) .getData('activities', { start, end, order: 'asc', per_page: 100 })
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
if (ret.data.activities.length > 0) { if (ret.data.activities.length > 0) {

View File

@ -70,9 +70,12 @@ class Calendar extends React.Component {
filterActivities(day) { filterActivities(day) {
const { activities } = this.props const { activities } = this.props
if (activities) {
return activities return activities
.filter(act => dateFns.isSameDay(act.activity_date, day)) .filter(act => dateFns.isSameDay(act.activity_date, day))
} }
return []
}
renderCells() { renderCells() {
const { monthStart, monthEnd } = this.state const { monthStart, monthEnd } = this.state

View File

@ -7,23 +7,17 @@ export default class MpwoApi {
let url = `${apiUrl}${target}` let url = `${apiUrl}${target}`
if (data.id) { if (data.id) {
url = `${url}/${data.id}` url = `${url}/${data.id}`
} else if (data.page) { } else if (Object.keys(data).length > 0) {
url = `${url}?page=${data.page}` url = `${url}?${
} data.page ? `&page=${data.page}` : ''
if (data.start || data.end) {
url = `${url}${
data.page ? '' : '?'
}${ }${
data.start && `&from=${data.start}` data.start ? `&from=${data.start}` : ''
}${ }${
data.end && `&to=${data.end}` data.end ? `&to=${data.end}` : ''
}`
}
if (data.order) {
url = `${url}${
(data.page || data.start || data.end) ? '' : '?'
}${ }${
`&order=${data.order}` data.order ? `&order=${data.order}` : ''
}${
data.per_page ? `$per_page=${data.per_page}` : ''
}` }`
} }
const params = { const params = {