2017-06-12 17:21:25 +01:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-06-18 18:00:36 +01:00
|
|
|
|
from datetime import datetime
|
2017-12-30 16:54:39 +00:00
|
|
|
|
|
2017-06-12 17:21:25 +01:00
|
|
|
|
from flask import (
|
2018-02-20 11:22:17 +00:00
|
|
|
|
Response,
|
2023-12-15 10:29:42 -08:00
|
|
|
|
flash,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
jsonify,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
render_template,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
request,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
stream_with_context,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
url_for,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
)
|
2018-03-01 15:38:42 +00:00
|
|
|
|
|
2024-01-24 11:54:25 -08:00
|
|
|
|
from app import current_service, job_api_client, notification_api_client
|
2017-06-12 17:21:25 +01:00
|
|
|
|
from app.main import main
|
2018-09-06 10:41:59 +01:00
|
|
|
|
from app.notify_client.api_key_api_client import KEY_TYPE_TEST
|
2017-06-12 17:21:25 +01:00
|
|
|
|
from app.utils import (
|
2018-02-20 11:22:17 +00:00
|
|
|
|
DELIVERED_STATUSES,
|
|
|
|
|
|
FAILURE_STATUSES,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
get_help_argument,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
parse_filter_args,
|
|
|
|
|
|
set_status_filters,
|
|
|
|
|
|
)
|
2024-01-24 11:59:31 -08:00
|
|
|
|
from app.utils.csv import generate_notifications_csv, get_user_preferred_timezone
|
2021-06-09 15:15:35 +01:00
|
|
|
|
from app.utils.templates import get_template
|
2021-06-09 13:19:05 +01:00
|
|
|
|
from app.utils.user import user_has_permissions
|
2017-06-12 17:21:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
|
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>")
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@user_has_permissions("view_activity", "send_messages")
|
2023-12-15 10:29:42 -08:00
|
|
|
|
def view_notification(service_id, notification_id, error_message=None):
|
|
|
|
|
|
if error_message:
|
|
|
|
|
|
flash(error_message)
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
notification = notification_api_client.get_notification(
|
|
|
|
|
|
service_id, str(notification_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
notification["template"].update({"reply_to_text": notification["reply_to_text"]})
|
2018-01-03 10:44:36 +00:00
|
|
|
|
|
2018-11-02 14:18:58 +00:00
|
|
|
|
personalisation = get_all_personalisation_from_notification(notification)
|
2018-03-08 17:10:34 +00:00
|
|
|
|
|
2017-06-24 17:12:45 +01:00
|
|
|
|
template = get_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
notification["template"],
|
2017-06-24 17:12:45 +01:00
|
|
|
|
current_service,
|
|
|
|
|
|
show_recipient=True,
|
2017-06-24 17:29:28 +01:00
|
|
|
|
redact_missing_personalisation=True,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
sms_sender=notification["reply_to_text"],
|
|
|
|
|
|
email_reply_to=notification["reply_to_text"],
|
2017-06-24 17:12:45 +01:00
|
|
|
|
)
|
2018-11-02 14:18:58 +00:00
|
|
|
|
template.values = personalisation
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if notification["job"]:
|
|
|
|
|
|
job = job_api_client.get_job(service_id, notification["job"]["id"])["data"]
|
2017-06-24 17:12:45 +01:00
|
|
|
|
else:
|
|
|
|
|
|
job = None
|
2017-07-13 13:05:41 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if get_help_argument() or request.args.get("help") == "0":
|
2019-06-07 16:16:13 +01:00
|
|
|
|
# help=0 is set when you’ve just sent a notification. We
|
|
|
|
|
|
# only want to show the back link when you’ve navigated to a
|
|
|
|
|
|
# notification, not when you’ve just sent it.
|
|
|
|
|
|
back_link = None
|
2023-08-25 09:12:23 -07:00
|
|
|
|
elif request.args.get("from_job"):
|
2019-05-09 17:33:22 +01:00
|
|
|
|
back_link = url_for(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"main.view_job",
|
2019-05-09 17:33:22 +01:00
|
|
|
|
service_id=current_service.id,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
job_id=request.args.get("from_job"),
|
2019-05-09 17:33:22 +01:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
back_link = url_for(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"main.view_notifications",
|
2019-05-09 17:33:22 +01:00
|
|
|
|
service_id=current_service.id,
|
|
|
|
|
|
message_type=template.template_type,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
status="sending,delivered,failed",
|
2019-05-09 17:33:22 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2017-06-12 17:21:25 +01:00
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/notifications/notification.html",
|
|
|
|
|
|
finished=(notification["status"] in (DELIVERED_STATUSES + FAILURE_STATUSES)),
|
|
|
|
|
|
notification_status=notification["status"],
|
2019-10-30 11:28:38 +00:00
|
|
|
|
message=error_message,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
uploaded_file_name="Report",
|
2017-06-24 17:12:45 +01:00
|
|
|
|
template=template,
|
|
|
|
|
|
job=job,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
updates_url=url_for(
|
|
|
|
|
|
".view_notification_updates",
|
|
|
|
|
|
service_id=service_id,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
notification_id=notification["id"],
|
|
|
|
|
|
status=request.args.get("status"),
|
|
|
|
|
|
help=get_help_argument(),
|
2017-06-12 17:21:25 +01:00
|
|
|
|
),
|
|
|
|
|
|
partials=get_single_notification_partials(notification),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
created_by=notification.get("created_by"),
|
|
|
|
|
|
created_at=notification["created_at"],
|
2024-02-02 08:31:05 -08:00
|
|
|
|
updated_at=notification["sent_at"],
|
2017-07-13 13:05:41 +01:00
|
|
|
|
help=get_help_argument(),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
notification_id=notification["id"],
|
|
|
|
|
|
can_receive_inbound=(current_service.has_permission("inbound_sms")),
|
|
|
|
|
|
sent_with_test_key=(notification.get("key_type") == KEY_TYPE_TEST),
|
2019-05-09 17:33:22 +01:00
|
|
|
|
back_link=back_link,
|
2017-06-12 17:21:25 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
|
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>.json")
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@user_has_permissions("view_activity", "send_messages")
|
2017-06-12 17:21:25 +01:00
|
|
|
|
def view_notification_updates(service_id, notification_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return jsonify(
|
|
|
|
|
|
**get_single_notification_partials(
|
|
|
|
|
|
notification_api_client.get_notification(service_id, notification_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-06-12 17:21:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_single_notification_partials(notification):
|
|
|
|
|
|
return {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"status": render_template(
|
|
|
|
|
|
"partials/notifications/status.html",
|
2018-09-06 10:41:59 +01:00
|
|
|
|
notification=notification,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
sent_with_test_key=(notification.get("key_type") == KEY_TYPE_TEST),
|
2017-06-12 17:21:25 +01:00
|
|
|
|
),
|
|
|
|
|
|
}
|
2017-06-24 17:12:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_personalisation_from_notification(notification):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if notification["template"].get("redact_personalisation"):
|
|
|
|
|
|
notification["personalisation"] = {}
|
2017-06-24 17:29:28 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if notification["template"]["template_type"] == "email":
|
|
|
|
|
|
notification["personalisation"]["email_address"] = notification["to"]
|
2017-06-24 17:29:28 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if notification["template"]["template_type"] == "sms":
|
|
|
|
|
|
notification["personalisation"]["phone_number"] = notification["to"]
|
2017-06-24 17:29:28 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return notification["personalisation"]
|
2017-12-30 16:54:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
|
@main.route("/services/<uuid:service_id>/download-notifications.csv")
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@user_has_permissions("view_activity")
|
2017-12-30 16:54:39 +00:00
|
|
|
|
def download_notifications_csv(service_id):
|
|
|
|
|
|
filter_args = parse_filter_args(request.args)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
filter_args["status"] = set_status_filters(filter_args)
|
2024-06-10 10:15:22 -06:00
|
|
|
|
number_of_days = request.args["number_of_days"]
|
2023-08-25 09:12:23 -07:00
|
|
|
|
service_data_retention_days = current_service.get_days_of_retention(
|
2024-06-10 10:15:22 -06:00
|
|
|
|
filter_args.get("message_type")[0], number_of_days
|
2023-08-25 09:12:23 -07:00
|
|
|
|
)
|
2024-01-24 11:54:25 -08:00
|
|
|
|
file_time = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
|
2024-01-24 11:59:31 -08:00
|
|
|
|
file_time = f"{file_time} {get_user_preferred_timezone()}"
|
2024-01-24 11:54:25 -08:00
|
|
|
|
|
2017-12-30 16:54:39 +00:00
|
|
|
|
return Response(
|
|
|
|
|
|
stream_with_context(
|
|
|
|
|
|
generate_notifications_csv(
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
job_id=None,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
status=filter_args.get("status"),
|
|
|
|
|
|
page=request.args.get("page", 1),
|
2018-07-02 15:48:00 +01:00
|
|
|
|
page_size=10000,
|
2018-01-15 12:29:03 +00:00
|
|
|
|
format_for_csv=True,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
template_type=filter_args.get("message_type"),
|
2018-09-27 14:51:36 +01:00
|
|
|
|
limit_days=service_data_retention_days,
|
2017-12-30 16:54:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
|
mimetype="text/csv",
|
2017-12-30 16:54:39 +00:00
|
|
|
|
headers={
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"Content-Disposition": 'inline; filename="{} - {} - {} report.csv"'.format(
|
2024-01-24 11:54:25 -08:00
|
|
|
|
file_time,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
filter_args["message_type"][0],
|
|
|
|
|
|
current_service.name,
|
|
|
|
|
|
)
|
|
|
|
|
|
},
|
2017-12-30 16:54:39 +00:00
|
|
|
|
)
|