mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-15 19:01:04 -04:00
Merge pull request #2141 from alphagov/paginate-complaints-page
Paginate complaints page
This commit is contained in:
@@ -199,7 +199,7 @@ def get_notifications(service_id, message_type, status_override=None):
|
||||
# TODO get the api to return count of pages as well.
|
||||
page = get_page_from_request()
|
||||
if page is None:
|
||||
abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args['page'], None))
|
||||
abort(404, "Invalid page argument ({}).".format(request.args.get('page')))
|
||||
if message_type not in ['email', 'sms', 'letter']:
|
||||
abort(404)
|
||||
filter_args = parse_filter_args(request.args)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import itertools
|
||||
from datetime import datetime
|
||||
|
||||
from flask import render_template, request, url_for
|
||||
from flask import abort, render_template, request, url_for
|
||||
from flask_login import login_required
|
||||
|
||||
from app import (
|
||||
@@ -12,7 +12,12 @@ from app import (
|
||||
from app.main import main
|
||||
from app.main.forms import DateFilterForm
|
||||
from app.statistics_utils import get_formatted_percentage
|
||||
from app.utils import user_is_platform_admin
|
||||
from app.utils import (
|
||||
generate_next_dict,
|
||||
generate_previous_dict,
|
||||
get_page_from_request,
|
||||
user_is_platform_admin,
|
||||
)
|
||||
|
||||
COMPLAINT_THRESHOLD = 0.02
|
||||
FAILURE_THRESHOLD = 3
|
||||
@@ -190,11 +195,26 @@ def platform_admin_services():
|
||||
@login_required
|
||||
@user_is_platform_admin
|
||||
def platform_admin_list_complaints():
|
||||
complaints = complaint_api_client.get_all_complaints()
|
||||
page = get_page_from_request()
|
||||
if page is None:
|
||||
abort(404, "Invalid page argument ({}).".format(request.args.get('page')))
|
||||
|
||||
response = complaint_api_client.get_all_complaints(page=page)
|
||||
|
||||
prev_page = None
|
||||
if response['links'].get('prev'):
|
||||
prev_page = generate_previous_dict('main.platform_admin_list_complaints', None, page)
|
||||
next_page = None
|
||||
if response['links'].get('next'):
|
||||
next_page = generate_next_dict('main.platform_admin_list_complaints', None, page)
|
||||
|
||||
return render_template(
|
||||
'views/platform-admin/complaints.html',
|
||||
complaints=complaints,
|
||||
complaints=response['complaints'],
|
||||
page_title='All Complaints',
|
||||
page=page,
|
||||
prev_page=prev_page,
|
||||
next_page=next_page,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@ class ComplaintApiClient(NotifyAdminAPIClient):
|
||||
def __init__(self):
|
||||
super().__init__("a" * 73, "b")
|
||||
|
||||
def get_all_complaints(self):
|
||||
return self.get('/complaint')
|
||||
def get_all_complaints(self, page=1):
|
||||
params = {'page': page}
|
||||
return self.get('/complaint', params=params)
|
||||
|
||||
def get_complaint_count(self, params_dict=None):
|
||||
return self.get('/complaint/count-by-date-range', params=params_dict)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{% extends "views/platform-admin/_base_template.html" %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/previous-next-navigation.html" import previous_next_navigation %}
|
||||
{% from "components/table.html" import list_table, field, text_field, link_field, right_aligned_field_heading, hidden_field_heading %}
|
||||
|
||||
{% block per_page_title %}
|
||||
@@ -28,9 +29,10 @@
|
||||
|
||||
{{ text_field(item.complaint_type) }}
|
||||
|
||||
{{ text_field(item.complaint_date|format_datetime_short) }}
|
||||
{{ text_field(item.complaint_date|format_datetime_short if item.complaint_date else None) }}
|
||||
|
||||
{% endcall %}
|
||||
|
||||
{{ previous_next_navigation(prev_page, next_page) }}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -662,7 +662,7 @@ def test_platform_admin_list_complaints(
|
||||
'created_at': '2018-06-05T13:50:30.012354',
|
||||
}
|
||||
mock = mocker.patch('app.complaint_api_client.get_all_complaints',
|
||||
return_value=[complaint])
|
||||
return_value={'complaints': [complaint], 'links': {}})
|
||||
|
||||
client.login(platform_admin_user)
|
||||
response = client.get(url_for('main.platform_admin_list_complaints'))
|
||||
@@ -673,6 +673,50 @@ def test_platform_admin_list_complaints(
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_should_show_complaints_with_next_previous(mocker, client, platform_admin_user, service_one, fake_uuid):
|
||||
mock_get_user(mocker, user=platform_admin_user)
|
||||
client.login(platform_admin_user)
|
||||
|
||||
api_response = {
|
||||
'complaints': [{'complaint_date': None,
|
||||
'complaint_type': None,
|
||||
'created_at': '2017-12-18T05:00:00.000000Z',
|
||||
'id': fake_uuid,
|
||||
'notification_id': fake_uuid,
|
||||
'service_id': service_one['id'],
|
||||
'service_name': service_one['name'],
|
||||
'ses_feedback_id': 'None'}],
|
||||
'links': {'last': '/complaint?page=3', 'next': '/complaint?page=3', 'prev': '/complaint?page=1'}
|
||||
}
|
||||
|
||||
mocker.patch('app.complaint_api_client.get_all_complaints', return_value=api_response)
|
||||
|
||||
response = client.get(url_for('main.platform_admin_list_complaints', page=2))
|
||||
|
||||
assert response.status_code == 200
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
|
||||
next_page_link = page.find('a', {'rel': 'next'})
|
||||
prev_page_link = page.find('a', {'rel': 'previous'})
|
||||
assert (url_for('main.platform_admin_list_complaints', page=3) in next_page_link['href'])
|
||||
assert 'Next page' in next_page_link.text.strip()
|
||||
assert 'page 3' in next_page_link.text.strip()
|
||||
assert (url_for('main.platform_admin_list_complaints', page=1) in prev_page_link['href'])
|
||||
assert 'Previous page' in prev_page_link.text.strip()
|
||||
assert 'page 1' in prev_page_link.text.strip()
|
||||
|
||||
|
||||
def test_platform_admin_list_complaints_returns_404_with_invalid_page(mocker, client, platform_admin_user):
|
||||
mock_get_user(mocker, user=platform_admin_user)
|
||||
client.login(platform_admin_user)
|
||||
|
||||
mocker.patch('app.complaint_api_client.get_all_complaints', return_value={'complaints': [], 'links': {}})
|
||||
|
||||
response = client.get(url_for('main.platform_admin_list_complaints', page='invalid'))
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize('number, total, threshold, result', [
|
||||
(0, 0, 0, False),
|
||||
(1, 1, 0, True),
|
||||
|
||||
@@ -7,7 +7,16 @@ def test_get_all_complaints(mocker):
|
||||
mock = mocker.patch('app.notify_client.complaint_api_client.ComplaintApiClient.get')
|
||||
|
||||
client.get_all_complaints()
|
||||
mock.assert_called_once_with('/complaint')
|
||||
mock.assert_called_once_with('/complaint', params={'page': 1})
|
||||
|
||||
|
||||
def test_get_all_complaints_with_a_page_number_specified(mocker):
|
||||
client = ComplaintApiClient()
|
||||
|
||||
mock = mocker.patch('app.notify_client.complaint_api_client.ComplaintApiClient.get')
|
||||
|
||||
client.get_all_complaints(page=3)
|
||||
mock.assert_called_once_with('/complaint', params={'page': 3})
|
||||
|
||||
|
||||
def test_get_complaint_count(mocker):
|
||||
|
||||
Reference in New Issue
Block a user