Files
notifications-admin/app/main/views/notifications.py
T

137 lines
4.4 KiB
Python
Raw Normal View History

2017-06-12 17:21:25 +01:00
# -*- coding: utf-8 -*-
from flask import (
abort,
2017-06-12 17:21:25 +01:00
render_template,
jsonify,
request,
url_for,
)
from flask_login import login_required
from app import (
notification_api_client,
job_api_client,
2017-06-12 17:21:25 +01:00
current_service
)
from app.main import main
2017-07-13 13:28:11 +01:00
from app.template_previews import TemplatePreview
2017-06-12 17:21:25 +01:00
from app.utils import (
user_has_permissions,
get_help_argument,
get_template,
get_time_left,
2017-07-13 13:05:41 +01:00
get_letter_timings,
2017-06-12 17:21:25 +01:00
FAILURE_STATUSES,
DELIVERED_STATUSES,
)
2017-07-13 13:28:11 +01:00
@main.route("/services/<service_id>/notification/<uuid:notification_id>")
2017-06-12 17:21:25 +01:00
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_notification(service_id, notification_id):
2017-07-13 13:28:11 +01:00
notification = notification_api_client.get_notification(service_id, str(notification_id))
template = get_template(
notification['template'],
current_service,
letter_preview_url=url_for(
'.view_letter_notification_as_preview',
service_id=service_id,
2017-07-13 13:28:11 +01:00
notification_id=notification_id,
filetype='png',
),
show_recipient=True,
2017-06-24 17:29:28 +01:00
redact_missing_personalisation=True,
)
template.values = get_all_personalisation_from_notification(notification)
if notification['job']:
job = job_api_client.get_job(service_id, notification['job']['id'])['data']
else:
job = None
2017-07-13 13:05:41 +01:00
2017-06-12 17:21:25 +01:00
return render_template(
'views/notifications/notification.html',
finished=(notification['status'] in (DELIVERED_STATUSES + FAILURE_STATUSES)),
uploaded_file_name='Report',
template=template,
job=job,
2017-06-12 17:21:25 +01:00
updates_url=url_for(
".view_notification_updates",
service_id=service_id,
notification_id=notification['id'],
status=request.args.get('status'),
help=get_help_argument()
),
partials=get_single_notification_partials(notification),
created_by=notification.get('created_by'),
created_at=notification['created_at'],
2017-07-13 13:05:41 +01:00
help=get_help_argument(),
estimated_letter_delivery_date=get_letter_timings(notification['created_at']).earliest_delivery,
2017-10-16 10:30:59 +01:00
notification_id=notification['id'],
can_receive_inbound=('inbound_sms' in current_service['permissions']),
2017-06-12 17:21:25 +01:00
)
@main.route("/services/<service_id>/notification/<uuid:notification_id>.<filetype>")
2017-07-13 13:28:11 +01:00
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_letter_notification_as_preview(service_id, notification_id, filetype):
if filetype not in ('pdf', 'png'):
abort(404)
2017-07-13 13:28:11 +01:00
notification = notification_api_client.get_notification(service_id, notification_id)
template = get_template(
notification['template'],
current_service,
letter_preview_url=url_for(
'.view_letter_notification_as_preview',
2017-07-13 13:28:11 +01:00
service_id=service_id,
notification_id=notification_id,
filetype='png',
2017-07-13 13:28:11 +01:00
),
)
template.values = notification['personalisation']
return TemplatePreview.from_utils_template(template, filetype, page=request.args.get('page'))
2017-07-13 13:28:11 +01:00
@main.route("/services/<service_id>/notification/<notification_id>.json")
2017-06-12 17:21:25 +01:00
@user_has_permissions('view_activity', admin_override=True)
def view_notification_updates(service_id, notification_id):
return jsonify(**get_single_notification_partials(
notification_api_client.get_notification(service_id, notification_id)
))
def get_single_notification_partials(notification):
return {
'notifications': render_template(
'partials/notifications/notifications.html',
notification=notification,
more_than_one_page=False,
percentage_complete=100,
time_left=get_time_left(notification['created_at']),
),
'status': render_template(
'partials/notifications/status.html',
notification=notification
),
}
def get_all_personalisation_from_notification(notification):
2017-06-24 17:29:28 +01:00
if notification['template'].get('redact_personalisation'):
notification['personalisation'] = {}
if notification['template']['template_type'] == 'email':
notification['personalisation']['email_address'] = notification['to']
2017-06-24 17:29:28 +01:00
if notification['template']['template_type'] == 'sms':
notification['personalisation']['phone_number'] = notification['to']
2017-06-24 17:29:28 +01:00
return notification['personalisation']