mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-05 16:38:59 -04:00
Put the download csv link on the activity page.
Still needs some tests, probably some refactoring as well.
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from orderedset import OrderedSet
|
||||
from itertools import chain
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
@@ -18,7 +16,6 @@ from notifications_utils.template import (
|
||||
Template,
|
||||
WithSubjectTemplate,
|
||||
)
|
||||
from werkzeug.datastructures import MultiDict
|
||||
|
||||
from app import (
|
||||
job_api_client,
|
||||
@@ -35,39 +32,11 @@ from app.utils import (
|
||||
user_has_permissions,
|
||||
generate_notifications_csv,
|
||||
get_time_left,
|
||||
REQUESTED_STATUSES,
|
||||
FAILURE_STATUSES,
|
||||
SENDING_STATUSES,
|
||||
DELIVERED_STATUSES,
|
||||
get_letter_timings,
|
||||
)
|
||||
parse_filter_args, set_status_filters)
|
||||
from app.statistics_utils import add_rate_to_job
|
||||
|
||||
|
||||
def _parse_filter_args(filter_dict):
|
||||
if not isinstance(filter_dict, MultiDict):
|
||||
filter_dict = MultiDict(filter_dict)
|
||||
|
||||
return MultiDict(
|
||||
(
|
||||
key,
|
||||
(','.join(filter_dict.getlist(key))).split(',')
|
||||
)
|
||||
for key in filter_dict.keys()
|
||||
if ''.join(filter_dict.getlist(key))
|
||||
)
|
||||
|
||||
|
||||
def _set_status_filters(filter_args):
|
||||
status_filters = filter_args.get('status', [])
|
||||
return list(OrderedSet(chain(
|
||||
(status_filters or REQUESTED_STATUSES),
|
||||
DELIVERED_STATUSES if 'delivered' in status_filters else [],
|
||||
SENDING_STATUSES if 'sending' in status_filters else [],
|
||||
FAILURE_STATUSES if 'failed' in status_filters else []
|
||||
)))
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs")
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
@@ -104,8 +73,8 @@ def view_job(service_id, job_id):
|
||||
if job['job_status'] == 'cancelled':
|
||||
abort(404)
|
||||
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
filter_args['status'] = _set_status_filters(filter_args)
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args['status'] = set_status_filters(filter_args)
|
||||
|
||||
total_notifications = job.get('notification_count', 0)
|
||||
processed_notifications = job.get('notifications_delivered', 0) + job.get('notifications_failed', 0)
|
||||
@@ -146,8 +115,8 @@ def view_job_csv(service_id, job_id):
|
||||
template_id=job['template'],
|
||||
version=job['template_version']
|
||||
)['data']
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
filter_args['status'] = _set_status_filters(filter_args)
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args['status'] = set_status_filters(filter_args)
|
||||
|
||||
return Response(
|
||||
stream_with_context(
|
||||
@@ -206,6 +175,12 @@ def view_notifications(service_id, message_type):
|
||||
page=request.args.get('page', 1),
|
||||
to=request.form.get('to', ''),
|
||||
search_form=SearchNotificationsForm(to=request.form.get('to', '')),
|
||||
download_link=url_for(
|
||||
'.download_notifications_csv',
|
||||
service_id=current_service['id'],
|
||||
message_type=message_type,
|
||||
status=request.args.get('status')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -226,8 +201,8 @@ def get_notifications(service_id, message_type, status_override=None):
|
||||
abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args['page'], None))
|
||||
if message_type not in ['email', 'sms', 'letter']:
|
||||
abort(404)
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
filter_args['status'] = _set_status_filters(filter_args)
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args['status'] = set_status_filters(filter_args)
|
||||
if request.path.endswith('csv'):
|
||||
return Response(
|
||||
generate_notifications_csv(
|
||||
@@ -250,7 +225,6 @@ def get_notifications(service_id, message_type, status_override=None):
|
||||
limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'],
|
||||
to=request.form.get('to', ''),
|
||||
)
|
||||
|
||||
url_args = {
|
||||
'message_type': message_type,
|
||||
'status': request.args.get('status')
|
||||
@@ -361,8 +335,8 @@ def _get_job_counts(job):
|
||||
|
||||
|
||||
def get_job_partials(job, template):
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
filter_args['status'] = _set_status_filters(filter_args)
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args['status'] = set_status_filters(filter_args)
|
||||
notifications = notification_api_client.get_notifications_for_service(
|
||||
job['service'], job['id'], status=filter_args['status']
|
||||
)
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
|
||||
from flask import (
|
||||
abort,
|
||||
render_template,
|
||||
jsonify,
|
||||
request,
|
||||
url_for,
|
||||
)
|
||||
Response, stream_with_context)
|
||||
from flask_login import login_required
|
||||
|
||||
from app import (
|
||||
notification_api_client,
|
||||
job_api_client,
|
||||
current_service
|
||||
)
|
||||
current_service,
|
||||
format_date_numeric)
|
||||
from app.main import main
|
||||
from app.template_previews import TemplatePreview
|
||||
from app.utils import (
|
||||
@@ -23,7 +25,7 @@ from app.utils import (
|
||||
get_letter_timings,
|
||||
FAILURE_STATUSES,
|
||||
DELIVERED_STATUSES,
|
||||
)
|
||||
generate_notifications_csv, parse_filter_args, set_status_filters)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/notification/<uuid:notification_id>")
|
||||
@@ -134,3 +136,31 @@ def get_all_personalisation_from_notification(notification):
|
||||
notification['personalisation']['phone_number'] = notification['to']
|
||||
|
||||
return notification['personalisation']
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/download-notifications.csv")
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
def download_notifications_csv(service_id):
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args['status'] = set_status_filters(filter_args)
|
||||
|
||||
return Response(
|
||||
stream_with_context(
|
||||
generate_notifications_csv(
|
||||
service_id=service_id,
|
||||
job_id=None,
|
||||
status=filter_args.get('status'),
|
||||
page=request.args.get('page', 1),
|
||||
page_size=5000,
|
||||
format_for_csv=True
|
||||
)
|
||||
),
|
||||
mimetype='text/csv',
|
||||
headers={
|
||||
'Content-Disposition': 'inline; filename="{} - {} - {} report.csv"'.format(
|
||||
format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
|
||||
filter_args['message_type'][0],
|
||||
current_service['name'])
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user