From 0d936e373a290ec35b70123c931bca94080b898b Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 27 Sep 2016 13:24:48 +0100 Subject: [PATCH 1/2] Removed repeated if statements --- app/templates/views/notifications.html | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/templates/views/notifications.html b/app/templates/views/notifications.html index c8b677127..45c18d46d 100644 --- a/app/templates/views/notifications.html +++ b/app/templates/views/notifications.html @@ -39,10 +39,6 @@   Data available for 7 days

- {% endif %} - - - {% if notifications %}
{% endif %} {% call(item, row_number) list_table( From d9eac94f1ee150dc4e5292ced1700c6e19cb41fa Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 27 Sep 2016 15:07:40 +0100 Subject: [PATCH 2/2] Make the activity page update with AJAX The activity page looks the same as the job page. So it should behave the same and not need manually refreshing to see the new stuff. --- app/main/views/jobs.py | 65 ++++++++++++----- app/templates/views/activity/counts.html | 9 +++ .../views/activity/notifications.html | 47 ++++++++++++ app/templates/views/notifications.html | 73 +++---------------- tests/app/main/views/test_jobs.py | 9 +++ 5 files changed, 122 insertions(+), 81 deletions(-) create mode 100644 app/templates/views/activity/counts.html create mode 100644 app/templates/views/activity/notifications.html diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 770d56c46..be376a989 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -2,6 +2,7 @@ import ago import time import dateutil +import json from orderedset import OrderedSet from datetime import datetime, timedelta, timezone from itertools import chain @@ -165,10 +166,28 @@ def view_job_updates(service_id, job_id): @main.route('/services//notifications/') -@main.route('/services//notifications/.csv', endpoint="view_notifications_csv") @login_required @user_has_permissions('view_activity', admin_override=True) def view_notifications(service_id, message_type): + return render_template( + 'views/notifications.html', + partials=get_notifications(service_id, message_type), + message_type=message_type, + status=request.args.get('status') + ) + + +@main.route('/services//notifications/.json') +@user_has_permissions('view_activity', admin_override=True) +def get_notifications_as_json(service_id, message_type): + return jsonify(get_notifications( + service_id, message_type, status_override=request.args.get('status') + )) + + +@main.route('/services//notifications/.csv', endpoint="view_notifications_csv") +@user_has_permissions('view_activity', admin_override=True) +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: @@ -220,26 +239,32 @@ def view_notifications(service_id, message_type): 'Content-Type': 'text/csv; charset=utf-8', 'Content-Disposition': 'inline; filename="notifications.csv"' } - return render_template( - 'views/notifications.html', - notifications=notifications['notifications'], - page=page, - prev_page=prev_page, - next_page=next_page, - status=request.args.get('status'), - message_type=message_type, - download_link=url_for( - '.view_notifications_csv', - service_id=current_service['id'], - message_type=message_type, - status=request.args.get('status') + return { + 'counts': render_template( + 'views/activity/counts.html', + status=request.args.get('status'), + status_filters=get_status_filters( + current_service, + message_type, + service_api_client.get_detailed_service(service_id)['data']['statistics'] + ) ), - status_filters=get_status_filters( - current_service, - message_type, - service_api_client.get_detailed_service(service_id)['data']['statistics'] - ) - ) + 'notifications': render_template( + 'views/activity/notifications.html', + notifications=notifications['notifications'], + page=page, + prev_page=prev_page, + next_page=next_page, + status=request.args.get('status'), + message_type=message_type, + download_link=url_for( + '.view_notifications_csv', + service_id=current_service['id'], + message_type=message_type, + status=request.args.get('status') + ) + ), + } def get_status_filters(service, message_type, statistics): diff --git a/app/templates/views/activity/counts.html b/app/templates/views/activity/counts.html new file mode 100644 index 000000000..b396bb064 --- /dev/null +++ b/app/templates/views/activity/counts.html @@ -0,0 +1,9 @@ +{% from "components/pill.html" import pill %} + +
+ {{ pill( + 'Status', + status_filters, + status + ) }} +
diff --git a/app/templates/views/activity/notifications.html b/app/templates/views/activity/notifications.html new file mode 100644 index 000000000..bd306e11c --- /dev/null +++ b/app/templates/views/activity/notifications.html @@ -0,0 +1,47 @@ +{% 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, row_heading, notification_status_field %} + +
+ + {% if notifications %} +

+ Download this report +   + Data available for 7 days +

+
+ {% endif %} + {% call(item, row_number) list_table( + notifications, + caption="Recent activity", + caption_visible=False, + empty_message='No messages found', + field_headings=['Recipient', 'Status'], + field_headings_visible=False + ) %} + + {% call row_heading() %} +

+ {{ item.to }} +

+

+ {% if item.job %} + From {{ item.job.original_file_name }} + {% else %} + {{ item.template.name }} + from an API call + {% endif %} +

+ {% endcall %} + + {{ notification_status_field(item) }} + + {% endcall %} + {% if notifications %} +
+ {% endif %} + + {{ previous_next_navigation(prev_page, next_page) }} + +
diff --git a/app/templates/views/notifications.html b/app/templates/views/notifications.html index 45c18d46d..8ee8c5da6 100644 --- a/app/templates/views/notifications.html +++ b/app/templates/views/notifications.html @@ -1,8 +1,5 @@ {% extends "withnav_template.html" %} -{% from "components/table.html" import list_table, field, text_field, link_field, right_aligned_field_heading, hidden_field_heading, row_heading, notification_status_field %} -{% from "components/previous-next-navigation.html" import previous_next_navigation %} -{% from "components/page-footer.html" import page_footer %} -{% from "components/pill.html" import pill %} +{% from "components/ajax-block.html" import ajax_block %} {% from "components/message-count-label.html" import message_count_label, recipient_count_label %} {% block page_title %} @@ -12,65 +9,19 @@ {% block maincolumn_content %}

- - - {%- if status != 'delivered,failed' -%} - {%- for label, option, _, _ in status_filters -%} - {%- if status == option -%}{{label}} {% endif -%} - {%- endfor -%} - {%- endif -%} - - - {{- message_count_label(99, message_type, suffix='') | capitalize }} - + {{ message_count_label(99, message_type, suffix='') | capitalize }}

-
- {{ pill( - 'Status', - status_filters, - status - ) }} -
+ {{ ajax_block( + partials, + url_for('.get_notifications_as_json', service_id=current_service.id, message_type=message_type, status=status), + 'counts' + ) }} - {% if notifications %} -

- Download this report -   - Data available for 7 days -

-
- {% endif %} - {% call(item, row_number) list_table( - notifications, - caption="Recent activity", - caption_visible=False, - empty_message='No messages found', - field_headings=['Recipient', 'Status'], - field_headings_visible=False - ) %} - - {% call row_heading() %} -

- {{ item.to }} -

-

- {% if item.job %} - From {{ item.job.original_file_name }} - {% else %} - {{ item.template.name }} - from an API call - {% endif %} -

- {% endcall %} - - {{ notification_status_field(item) }} - - {% endcall %} - {% if notifications %} -
- {% endif %} - - {{ previous_next_navigation(prev_page, next_page) }} + {{ ajax_block( + partials, + url_for('.get_notifications_as_json', service_id=current_service.id, message_type=message_type, status=status), + 'notifications' + ) }} {% endblock %} diff --git a/tests/app/main/views/test_jobs.py b/tests/app/main/views/test_jobs.py index c17c3f3fc..0693d02f5 100644 --- a/tests/app/main/views/test_jobs.py +++ b/tests/app/main/views/test_jobs.py @@ -338,6 +338,15 @@ def test_can_show_notifications( assert csv_response.get_data(as_text=True) == csv_content assert 'text/csv' in csv_response.headers['Content-Type'] + json_response = client.get(url_for( + 'main.get_notifications_as_json', + service_id=service_one['id'], + message_type=message_type, + status=status_argument + )) + json_content = json.loads(json_response.get_data(as_text=True)) + assert json_content.keys() == {'counts', 'notifications'} + def test_should_show_notifications_for_a_service_with_next_previous( app_,