mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-16 03:10:16 -04:00
Get live services csv report
This commit is contained in:
@@ -3,16 +3,26 @@ import re
|
||||
from collections import OrderedDict
|
||||
from datetime import datetime
|
||||
|
||||
from flask import abort, flash, redirect, render_template, request, url_for
|
||||
from flask import (
|
||||
Response,
|
||||
abort,
|
||||
flash,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
url_for,
|
||||
)
|
||||
from flask_login import login_required
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from requests import RequestException
|
||||
|
||||
from app import (
|
||||
complaint_api_client,
|
||||
format_date_numeric,
|
||||
letter_jobs_client,
|
||||
platform_stats_api_client,
|
||||
service_api_client,
|
||||
user_api_client,
|
||||
)
|
||||
from app.extensions import antivirus_client, redis_client
|
||||
from app.main import main
|
||||
@@ -28,6 +38,7 @@ from app.statistics_utils import (
|
||||
)
|
||||
from app.template_previews import validate_letter
|
||||
from app.utils import (
|
||||
Spreadsheet,
|
||||
generate_next_dict,
|
||||
generate_previous_dict,
|
||||
get_page_from_request,
|
||||
@@ -196,6 +207,46 @@ def platform_admin_reports():
|
||||
)
|
||||
|
||||
|
||||
@main.route("/platform-admin/reports/live-services.csv")
|
||||
@login_required
|
||||
@user_is_platform_admin
|
||||
def live_services_csv():
|
||||
services = service_api_client.get_services()["data"]
|
||||
live_services_columns = [
|
||||
"Service ID", "Organisation", "Service name", "Consent to research", "Main contact",
|
||||
"Contact email", "Contact mobile", "Live date", "SMS volume", "Email volume", "Letter volume"
|
||||
]
|
||||
live_services_data = []
|
||||
live_services_data.append(live_services_columns)
|
||||
for service in services:
|
||||
if service["count_as_live"]:
|
||||
main_contact = None
|
||||
if service["go_live_user"]:
|
||||
main_contact = user_api_client.get_user(service["go_live_user"])
|
||||
live_services_data.append([
|
||||
service["id"],
|
||||
service["organisation"],
|
||||
service["name"],
|
||||
service["consent_to_research"],
|
||||
main_contact.name if main_contact else None,
|
||||
main_contact.email_address if main_contact else None,
|
||||
main_contact.mobile_number if main_contact else None,
|
||||
service["go_live_at"],
|
||||
service["volume_sms"],
|
||||
service["volume_email"],
|
||||
service["volume_letter"],
|
||||
])
|
||||
return Response(
|
||||
Spreadsheet.from_rows(live_services_data).as_csv_data,
|
||||
mimetype='text/csv',
|
||||
headers={
|
||||
'Content-Disposition': 'inline; filename="{} live services report.csv"'.format(
|
||||
format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@main.route("/platform-admin/complaints")
|
||||
@login_required
|
||||
@user_is_platform_admin
|
||||
|
||||
@@ -85,6 +85,7 @@ class HeaderNavigation(Navigation):
|
||||
'find_users_by_email',
|
||||
'letter_branding',
|
||||
'live_services',
|
||||
'live_services_csv',
|
||||
'organisations',
|
||||
'platform_admin',
|
||||
'platform_admin_letter_validation_preview',
|
||||
@@ -477,6 +478,7 @@ class MainNavigation(Navigation):
|
||||
'letter_branding',
|
||||
'letter_branding_preview_image',
|
||||
'live_services',
|
||||
'live_services_csv',
|
||||
'letter_template',
|
||||
'message_status',
|
||||
'manage_org_users',
|
||||
@@ -690,6 +692,7 @@ class CaseworkNavigation(Navigation):
|
||||
'letter_template',
|
||||
'link_service_to_organisation',
|
||||
'live_services',
|
||||
'live_services_csv',
|
||||
'manage_org_users',
|
||||
'manage_template_folder',
|
||||
'manage_users',
|
||||
@@ -949,6 +952,7 @@ class OrgNavigation(Navigation):
|
||||
'letter_template',
|
||||
'link_service_to_organisation',
|
||||
'live_services',
|
||||
'live_services_csv',
|
||||
'manage_template_folder',
|
||||
'manage_users',
|
||||
'message_status',
|
||||
|
||||
@@ -10,4 +10,6 @@
|
||||
Reports
|
||||
</h1>
|
||||
|
||||
<a target="_blank" href="{{ url_for('main.live_services_csv') }}">Download live services information sheet</a>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -246,7 +246,6 @@ class Spreadsheet():
|
||||
def from_rows(cls, rows, filename=''):
|
||||
with StringIO() as converted:
|
||||
output = csv.writer(converted)
|
||||
|
||||
for row in rows:
|
||||
output.writerow(row)
|
||||
return cls(converted.getvalue(), filename)
|
||||
|
||||
@@ -932,11 +932,42 @@ def test_clear_cache_requires_option(client_request, platform_admin_user, mocker
|
||||
def test_reports_page(
|
||||
client,
|
||||
platform_admin_user,
|
||||
mocker,
|
||||
mocker
|
||||
):
|
||||
mock_get_user(mocker, user=platform_admin_user)
|
||||
client.login(platform_admin_user)
|
||||
|
||||
response = client.get(url_for('main.platform_admin_reports'))
|
||||
|
||||
assert response.status_code == 200
|
||||
response.get_data(as_text=True)
|
||||
page = response.get_data(as_text=True)
|
||||
assert "Download live services information sheet" in page
|
||||
|
||||
|
||||
def test_get_live_services_report(client, platform_admin_user, mocker):
|
||||
mock_get_user(mocker, user=platform_admin_user)
|
||||
client.login(platform_admin_user)
|
||||
|
||||
mocker.patch(
|
||||
'app.service_api_client.get_services',
|
||||
return_value={'data': [
|
||||
{'id': '1', 'organisation': 'Forest', 'name': 'jessie the oak tree', 'consent_to_research': True,
|
||||
'go_live_at': '2014-03-29', 'volume_sms': 100, 'volume_email': 50, 'volume_letter': 20,
|
||||
'count_as_live': True, 'go_live_user': '123'},
|
||||
{'id': '2', 'organisation': 'Forest', 'name': 'james the pine tree', 'consent_to_research': None,
|
||||
'go_live_at': '2015-03-26', 'volume_sms': None, 'volume_email': 60, 'volume_letter': 0,
|
||||
'count_as_live': True, 'go_live_user': None},
|
||||
{'id': '3', 'organisation': 'Forest', 'name': 'gary the rock', 'consent_to_research': None,
|
||||
'go_live_at': None, 'volume_sms': None, 'volume_email': 0, 'volume_letter': 0,
|
||||
'count_as_live': False, 'go_live_user': None},
|
||||
]}
|
||||
)
|
||||
response = client.get(url_for('main.live_services_csv'))
|
||||
assert response.status_code == 200
|
||||
report = response.get_data(as_text=True)
|
||||
assert report.strip() == (
|
||||
'Service ID,Organisation,Service name,Consent to research,'
|
||||
+ 'Main contact,Contact email,Contact mobile,Live date,SMS volume,Email volume,Letter volume\r\n1,Forest'
|
||||
+ ',jessie the oak tree,True,Platform admin user,platform@admin.gov.uk,07700 900762,2014-03-29,100,50,20\r\n2,'
|
||||
+ 'Forest,james the pine tree,,,,,2015-03-26,,60,0'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user