Download CSV option now available.

This commit is contained in:
Nicholas Staples
2016-04-12 14:19:51 +01:00
parent bcdde451af
commit 6ea72182d2
6 changed files with 67 additions and 4 deletions

View File

@@ -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'],

View File

@@ -45,8 +45,12 @@
{{ item.status }}
{% endcall %}
{% call field() %}
{{ item.created_at | format_datetime}}
{{ item.created_at | format_datetime }}
{% endcall %}
{% endcall %}
<p class="table-show-more-link">
<a href="{{ request.url }}&download=csv">Download csv</a>
</p>
{{ previous_next_navigation(prev_page, next_page) }}
{% endblock %}

View File

@@ -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:

View File

@@ -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

View File

@@ -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,

View File

@@ -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']