add pagination to inbox page

This commit is contained in:
chrisw
2018-03-21 15:08:03 +00:00
parent bf58a4347b
commit f5c467e4ff
7 changed files with 90 additions and 32 deletions

View File

@@ -105,7 +105,7 @@ def get_sms_thread(service_id, user_number):
notification_api_client.get_notifications_for_service(service_id, notification_api_client.get_notifications_for_service(service_id,
to=user_number, to=user_number,
template_type='sms')['notifications'] + template_type='sms')['notifications'] +
service_api_client.get_inbound_sms(service_id, user_number=user_number) service_api_client.get_inbound_sms(service_id, user_number=user_number)['data']
), key=lambda notification: notification['created_at']): ), key=lambda notification: notification['created_at']):
is_inbound = ('notify_number' in notification) is_inbound = ('notify_number' in notification)

View File

@@ -31,6 +31,8 @@ from app.utils import (
FAILURE_STATUSES, FAILURE_STATUSES,
REQUESTED_STATUSES, REQUESTED_STATUSES,
Spreadsheet, Spreadsheet,
generate_next_dict,
generate_previous_dict,
get_current_financial_year, get_current_financial_year,
user_has_permissions, user_has_permissions,
) )
@@ -195,7 +197,7 @@ def inbox(service_id):
return render_template( return render_template(
'views/dashboard/inbox.html', 'views/dashboard/inbox.html',
partials=get_inbox_partials(service_id), partials=get_inbox_partials(service_id),
updates_url=url_for('.inbox_updates', service_id=service_id), updates_url=url_for('.inbox_updates', service_id=service_id, page=request.args.get('page')),
) )
@@ -221,7 +223,7 @@ def inbox_download(service_id):
message['user_number'], message['user_number'],
message['content'].lstrip(('=+-@')), message['content'].lstrip(('=+-@')),
format_datetime_numeric(message['created_at']), format_datetime_numeric(message['created_at']),
] for message in service_api_client.get_inbound_sms(service_id)] ] for message in service_api_client.get_inbound_sms(service_id)['data']]
).as_csv_data, ).as_csv_data,
mimetype='text/csv', mimetype='text/csv',
headers={ headers={
@@ -233,11 +235,12 @@ def inbox_download(service_id):
def get_inbox_partials(service_id): def get_inbox_partials(service_id):
page = int(request.args.get('page', 1))
if 'inbound_sms' not in current_service['permissions']: if 'inbound_sms' not in current_service['permissions']:
abort(403) abort(403)
inbound_messages = service_api_client.get_inbound_sms(service_id) inbound_messages_data = service_api_client.get_inbound_sms(service_id, page=page)
inbound_messages = inbound_messages_data['data']
messages_to_show = {} messages_to_show = {}
# get the most recent message for each number # get the most recent message for each number
@@ -254,12 +257,22 @@ def get_inbox_partials(service_id):
else: else:
inbound_number = None inbound_number = None
prev_page = None
if page > 1:
prev_page = generate_previous_dict('main.inbox', service_id, page)
next_page = None
if inbound_messages_data['has_next']:
next_page = generate_next_dict('main.inbox', service_id, page)
return {'messages': render_template( return {'messages': render_template(
'views/dashboard/_inbox_messages.html', 'views/dashboard/_inbox_messages.html',
messages=list(messages_to_show), messages=list(messages_to_show),
count_of_messages=len(inbound_messages), count_of_messages=len(inbound_messages),
count_of_users=count_of_users, count_of_users=count_of_users,
inbound_number=inbound_number, inbound_number=inbound_number,
prev_page=prev_page,
next_page=next_page
)} )}

View File

@@ -261,13 +261,16 @@ class ServiceAPIClient(NotifyAdminAPIClient):
def update_whitelist(self, service_id, data): def update_whitelist(self, service_id, data):
return self.put(url='/service/{}/whitelist'.format(service_id), data=data) return self.put(url='/service/{}/whitelist'.format(service_id), data=data)
def get_inbound_sms(self, service_id, user_number=''): def get_inbound_sms(self, service_id, user_number='', page=None):
return self.get( return self.get(
'/service/{}/inbound-sms?user_number={}'.format( '/service/{}/inbound-sms'.format(
service_id, service_id,
user_number, ),
) params={
)['data'] 'user_number': user_number,
'page': page
}
)
def get_inbound_sms_by_id(self, service_id, notification_id): def get_inbound_sms_by_id(self, service_id, notification_id):
return self.get( return self.get(

View File

@@ -1,4 +1,5 @@
{% from "components/table.html" import list_table, field, hidden_field_heading, right_aligned_field_heading, row_heading %} {% from "components/table.html" import list_table, field, hidden_field_heading, right_aligned_field_heading, row_heading %}
{% from "components/previous-next-navigation.html" import previous_next_navigation %}
{% from "components/message-count-label.html" import message_count_label %} {% from "components/message-count-label.html" import message_count_label %}
<div class="ajax-block-container"> <div class="ajax-block-container">
@@ -39,4 +40,7 @@
from {{ count_of_users }} user{{ '' if 1 == count_of_users else 's' }} from {{ count_of_users }} user{{ '' if 1 == count_of_users else 's' }}
</p> </p>
{% endif %} {% endif %}
{{ previous_next_navigation(prev_page, next_page) }}
</div> </div>

View File

@@ -196,13 +196,16 @@ def test_view_conversation_with_empty_inbound(
): ):
mock_get_inbound_sms = mocker.patch( mock_get_inbound_sms = mocker.patch(
'app.main.views.conversation.service_api_client.get_inbound_sms', 'app.main.views.conversation.service_api_client.get_inbound_sms',
return_value=[{ return_value={
'user_number': '07900000001', 'has_next': False,
'notify_number': '07900000002', 'data': [{
'content': '', 'user_number': '07900000001',
'created_at': datetime.utcnow().isoformat(), 'notify_number': '07900000002',
'id': fake_uuid 'content': '',
}] 'created_at': datetime.utcnow().isoformat(),
'id': fake_uuid
}]
}
) )
page = client_request.get( page = client_request.get(

View File

@@ -200,6 +200,27 @@ def test_inbox_showing_inbound_messages(
) )
def test_get_inbound_sms_shows_page_links(
logged_in_client,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_detailed_service,
mock_get_template_statistics,
mock_get_usage,
mock_get_inbound_sms,
mock_get_inbound_number_for_service,
):
service_one['permissions'] = ['inbound_sms']
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID, page=2))
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert 'Next page' in page.find('li', {'class': 'next-page'}).text
assert 'Previous page' in page.find('li', {'class': 'previous-page'}).text
def test_empty_inbox( def test_empty_inbox(
logged_in_client, logged_in_client,
service_one, service_one,
@@ -222,6 +243,8 @@ def test_empty_inbox(
'When users text your services phone number (0781239871) youll see the messages here' 'When users text your services phone number (0781239871) youll see the messages here'
) )
assert not page.select('a[download]') assert not page.select('a[download]')
assert not page.select('li.next-page')
assert not page.select('li.previous-page')
@pytest.mark.parametrize('endpoint', [ @pytest.mark.parametrize('endpoint', [
@@ -333,13 +356,16 @@ def test_download_inbox_strips_formulae(
mocker.patch( mocker.patch(
'app.service_api_client.get_inbound_sms', 'app.service_api_client.get_inbound_sms',
return_value=[{ return_value={
'user_number': 'elevenchars', 'has_next': False,
'notify_number': 'foo', 'data': [{
'content': message_content, 'user_number': 'elevenchars',
'created_at': datetime.utcnow().isoformat(), 'notify_number': 'foo',
'id': fake_uuid, 'content': message_content,
}], 'created_at': datetime.utcnow().isoformat(),
'id': fake_uuid,
}]
},
) )
response = logged_in_client.get( response = logged_in_client.get(
url_for('main.inbox_download', service_id=SERVICE_ONE_ID) url_for('main.inbox_download', service_id=SERVICE_ONE_ID)

View File

@@ -1862,14 +1862,18 @@ def mock_get_inbound_sms(mocker):
def _get_inbound_sms( def _get_inbound_sms(
service_id, service_id,
user_number=None, user_number=None,
page=1
): ):
return [{ return {
'user_number': '0790090000' + str(i), 'has_next': True,
'notify_number': '07900000002', 'data': [{
'content': 'message-{}'.format(index + 1), 'user_number': '0790090000' + str(i),
'created_at': (datetime.utcnow() - timedelta(minutes=60 * (i + 1), seconds=index)).isoformat(), 'notify_number': '07900000002',
'id': sample_uuid(), 'content': 'message-{}'.format(index + 1),
} for index, i in enumerate([0, 0, 0, 2, 4, 6, 8, 8])] 'created_at': (datetime.utcnow() - timedelta(minutes=60 * (i + 1), seconds=index)).isoformat(),
'id': sample_uuid(),
} for index, i in enumerate([0, 0, 0, 2, 4, 6, 8, 8])]
}
return mocker.patch( return mocker.patch(
'app.service_api_client.get_inbound_sms', 'app.service_api_client.get_inbound_sms',
@@ -1881,8 +1885,13 @@ def mock_get_inbound_sms(mocker):
def mock_get_inbound_sms_with_no_messages(mocker): def mock_get_inbound_sms_with_no_messages(mocker):
def _get_inbound_sms( def _get_inbound_sms(
service_id, service_id,
user_number=None,
page=1
): ):
return [] return {
'has_next': False,
'data': []
}
return mocker.patch( return mocker.patch(
'app.service_api_client.get_inbound_sms', 'app.service_api_client.get_inbound_sms',