From 6ea72182d2d1027bf0db50683bb0ac26872bdac8 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Tue, 12 Apr 2016 14:19:51 +0100 Subject: [PATCH] Download CSV option now available. --- app/main/views/jobs.py | 9 ++++++++- app/templates/views/notifications.html | 6 +++++- app/utils.py | 22 +++++++++++++++++++++- scripts/run_tests.sh | 5 +++++ tests/__init__.py | 5 ++++- tests/app/main/views/test_jobs.py | 24 ++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index abcb0d85c..032653432 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -21,7 +21,8 @@ from app.main import main from app.utils import ( get_page_from_request, generate_previous_next_dict, - user_has_permissions) + user_has_permissions, + generate_notifications_csv) def _parse_filter_args(filter_dict): @@ -132,6 +133,12 @@ def view_notifications(service_id): page + 1, 'Next page', 'page {}'.format(page + 1)) + if 'download' in request.args and request.args['download'] == 'csv': + csv_content = generate_notifications_csv(notifications['notifications']) + return csv_content, 200, { + 'Content-Type': 'text/csv; charset=utf-8', + 'Content-Disposition': 'inline; filename="notifications.csv"' + } return render_template( 'views/notifications.html', notifications=notifications['notifications'], diff --git a/app/templates/views/notifications.html b/app/templates/views/notifications.html index bf7123995..889f084c2 100644 --- a/app/templates/views/notifications.html +++ b/app/templates/views/notifications.html @@ -45,8 +45,12 @@ {{ item.status }} {% endcall %} {% call field() %} - {{ item.created_at | format_datetime}} + {{ item.created_at | format_datetime }} {% endcall %} {% endcall %} + {{ previous_next_navigation(prev_page, next_page) }} + {% endblock %} \ No newline at end of file diff --git a/app/utils.py b/app/utils.py index 682060858..fbefa6aed 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,5 +1,6 @@ import re - +import csv +from io import StringIO from functools import wraps from flask import (abort, session, request, url_for) @@ -85,6 +86,25 @@ def get_errors_for_csv(recipients, template_type): return errors +def generate_notifications_csv(json_list): + from app import format_datetime + content = StringIO() + retval = None + with content as csvfile: + csvwriter = csv.writer(csvfile) + csvwriter.writerow(['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']) + for x in json_list: + csvwriter.writerow([ + x['to'], + x['template']['name'], + x['template']['template_type'], + x['job']['original_file_name'], + x['status'], + format_datetime(x['created_at'])]) + retval = content.getvalue() + return retval + + def get_page_from_request(): if 'page' in request.args: try: diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 38ed4e6ee..03e31b9b8 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -30,6 +30,11 @@ display_result $? 1 "Code style check" npm test display_result $? 2 "Front end code style check" +export NOTIFY_ADMIN_ENVIRONMENT='config.Test' + ## Code coverage py.test --cov=app --cov-report=term-missing tests/ display_result $? 3 "Code coverage" + +#py.test -v +#display_result $? 4 "Unit tests diff --git a/tests/__init__.py b/tests/__init__.py index 91ff5a87e..980345012 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -126,7 +126,10 @@ def notification_json(service_id, data = { 'notifications': [{ 'to': to, - 'template': {'id': template['id'], 'name': template['name']}, + 'template': { + 'id': template['id'], + 'name': template['name'], + 'template_type': template['template_type']}, 'job': {'id': job['id'], 'original_file_name': job['original_file_name']}, 'sent_at': sent_at, 'status': status, diff --git a/tests/app/main/views/test_jobs.py b/tests/app/main/views/test_jobs.py index 994ae27c8..9bd273bec 100644 --- a/tests/app/main/views/test_jobs.py +++ b/tests/app/main/views/test_jobs.py @@ -1,6 +1,7 @@ from flask import url_for from bs4 import BeautifulSoup import json +from app.utils import generate_notifications_csv def test_should_return_list_of_all_jobs(app_, @@ -179,3 +180,26 @@ def test_should_show_notifications_for_a_service_with_next_previous(app_, assert url_for('main.view_notifications', service_id=service_one['id'], page=1) in content assert 'Previous page' in content assert 'Next page' in content + + +def test_should_download_notifications_for_a_service(app_, + service_one, + api_user_active, + mock_login, + mock_get_user, + mock_get_user_by_email, + mock_get_service, + mock_get_notifications, + mock_has_permissions): + with app_.test_request_context(): + with app_.test_client() as client: + client.login(api_user_active) + response = client.get(url_for( + 'main.view_notifications', + service_id=service_one['id'], + download='csv')) + csv_content = generate_notifications_csv( + mock_get_notifications(service_one['id'])['notifications']) + assert response.status_code == 200 + assert response.get_data(as_text=True) == csv_content + assert 'text/csv' in response.headers['Content-Type']