Merge pull request #2010 from alphagov/one-off-fixxx

Add pagination to inbox page
This commit is contained in:
Chris Waszczuk
2018-04-09 10:30:24 +01:00
committed by GitHub
7 changed files with 137 additions and 64 deletions

View File

@@ -105,7 +105,7 @@ def get_sms_thread(service_id, user_number):
notification_api_client.get_notifications_for_service(service_id,
to=user_number,
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']):
is_inbound = ('notify_number' in notification)

View File

@@ -12,7 +12,6 @@ from flask import (
url_for,
)
from flask_login import login_required
from notifications_utils.recipients import format_phone_number_human_readable
from werkzeug.utils import redirect
from app import (
@@ -31,6 +30,8 @@ from app.utils import (
FAILURE_STATUSES,
REQUESTED_STATUSES,
Spreadsheet,
generate_next_dict,
generate_previous_dict,
get_current_financial_year,
user_has_permissions,
)
@@ -195,7 +196,7 @@ def inbox(service_id):
return render_template(
'views/dashboard/inbox.html',
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 +222,7 @@ def inbox_download(service_id):
message['user_number'],
message['content'].lstrip(('=+-@')),
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,
mimetype='text/csv',
headers={
@@ -233,33 +234,32 @@ def inbox_download(service_id):
def get_inbox_partials(service_id):
page = int(request.args.get('page', 1))
if 'inbound_sms' not in current_service['permissions']:
abort(403)
inbound_messages = service_api_client.get_inbound_sms(service_id)
messages_to_show = {}
# get the most recent message for each number
for message in inbound_messages:
human_readable = format_phone_number_human_readable(message['user_number'])
if human_readable not in messages_to_show:
messages_to_show[human_readable] = message
count_of_users = len(messages_to_show)
messages_to_show = sorted(messages_to_show.values(), key=lambda x: x['created_at'], reverse=True)
inbound_messages_data = service_api_client.get_most_recent_inbound_sms(service_id, page=page)
inbound_messages = inbound_messages_data['data']
if not inbound_messages:
inbound_number = inbound_number_client.get_inbound_sms_number_for_service(service_id)['data']['number']
else:
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(
'views/dashboard/_inbox_messages.html',
messages=list(messages_to_show),
count_of_messages=len(inbound_messages),
count_of_users=count_of_users,
messages=inbound_messages,
inbound_number=inbound_number,
prev_page=prev_page,
next_page=next_page
)}

View File

@@ -261,13 +261,26 @@ class ServiceAPIClient(NotifyAdminAPIClient):
def update_whitelist(self, service_id, 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(
'/service/{}/inbound-sms?user_number={}'.format(
'/service/{}/inbound-sms'.format(
service_id,
user_number,
)
)['data']
),
params={
'user_number': user_number,
'page': page
}
)
def get_most_recent_inbound_sms(self, service_id, page=None):
return self.get(
'/service/{}/inbound-sms/most-recent'.format(
service_id,
),
params={
'page': page
}
)
def get_inbound_sms_by_id(self, service_id, notification_id):
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/previous-next-navigation.html" import previous_next_navigation %}
{% from "components/message-count-label.html" import message_count_label %}
<div class="ajax-block-container">
@@ -33,10 +34,7 @@
</span>
{% endcall %}
{% endcall %}
{% if messages %}
<p class="table-show-more-link">
{{ count_of_messages }} message{{ '' if 1 == count_of_messages else 's' }}
from {{ count_of_users }} user{{ '' if 1 == count_of_users else 's' }}
</p>
{% endif %}
{{ previous_next_navigation(prev_page, next_page) }}
</div>