2016-01-19 11:23:09 +00:00
|
|
|
|
from flask import (
|
|
|
|
|
|
Blueprint,
|
|
|
|
|
|
jsonify,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
request,
|
2017-04-26 15:56:45 +01:00
|
|
|
|
current_app
|
2016-02-16 11:22:44 +00:00
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
|
from app import api_user, authenticated_service
|
2017-07-19 13:50:29 +01:00
|
|
|
|
from app.config import QueueNames
|
2016-02-22 17:17:29 +00:00
|
|
|
|
from app.dao import (
|
2016-02-25 11:35:32 +00:00
|
|
|
|
notifications_dao
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2017-06-05 15:54:40 +01:00
|
|
|
|
from app.errors import (
|
|
|
|
|
|
register_errors,
|
|
|
|
|
|
InvalidRequest
|
|
|
|
|
|
)
|
2017-06-28 12:14:36 +01:00
|
|
|
|
from app.models import (
|
2020-06-16 17:55:02 +01:00
|
|
|
|
EMAIL_TYPE, SMS_TYPE,
|
2017-08-23 14:56:03 +01:00
|
|
|
|
KEY_TYPE_TEAM, PRIORITY,
|
|
|
|
|
|
LETTER_TYPE)
|
2017-05-05 15:23:06 +01:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
simulated_recipient
|
|
|
|
|
|
)
|
|
|
|
|
|
from app.notifications.validators import (
|
2020-06-16 17:55:02 +01:00
|
|
|
|
check_if_service_can_send_to_number,
|
2017-06-29 18:02:21 +01:00
|
|
|
|
check_rate_limiting,
|
|
|
|
|
|
service_has_permission,
|
2020-06-16 17:55:02 +01:00
|
|
|
|
validate_template,
|
2017-05-05 15:23:06 +01:00
|
|
|
|
)
|
2016-02-03 13:16:19 +00:00
|
|
|
|
from app.schemas import (
|
2016-02-16 11:22:44 +00:00
|
|
|
|
email_notification_schema,
|
|
|
|
|
|
sms_template_notification_schema,
|
2016-07-26 14:33:14 +01:00
|
|
|
|
notification_with_personalisation_schema,
|
2017-11-01 15:02:50 +00:00
|
|
|
|
notifications_filter_schema
|
2016-02-16 11:22:44 +00:00
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
from app.service.utils import service_allowed_to_send_to
|
2020-04-06 14:25:43 +01:00
|
|
|
|
from app.utils import pagination_links, get_public_notify_type_text
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
2018-08-16 16:25:58 +01:00
|
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
notifications = Blueprint('notifications', __name__)
|
|
|
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
|
register_errors(notifications)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
|
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
2016-08-09 13:07:48 +01:00
|
|
|
|
def get_notification_by_id(notification_id):
|
2017-05-05 15:23:06 +01:00
|
|
|
|
notification = notifications_dao.get_notification_with_personalisation(
|
|
|
|
|
|
str(authenticated_service.id),
|
|
|
|
|
|
notification_id,
|
|
|
|
|
|
key_type=None)
|
2016-07-26 14:33:14 +01:00
|
|
|
|
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
|
@notifications.route('/notifications', methods=['GET'])
|
|
|
|
|
|
def get_all_notifications():
|
2016-06-14 15:07:23 +01:00
|
|
|
|
data = notifications_filter_schema.load(request.args).data
|
2016-09-15 16:01:26 +01:00
|
|
|
|
include_jobs = data.get('include_jobs', False)
|
2016-11-22 17:30:03 +00:00
|
|
|
|
page = data.get('page', 1)
|
2017-01-20 12:26:55 +00:00
|
|
|
|
page_size = data.get('page_size', current_app.config.get('API_PAGE_SIZE'))
|
2016-04-28 16:10:35 +01:00
|
|
|
|
limit_days = data.get('limit_days')
|
2016-03-01 13:30:10 +00:00
|
|
|
|
|
2016-03-21 12:37:34 +00:00
|
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
2017-05-05 15:23:06 +01:00
|
|
|
|
str(authenticated_service.id),
|
2016-08-09 13:07:48 +01:00
|
|
|
|
personalisation=True,
|
2016-03-21 12:37:34 +00:00
|
|
|
|
filter_dict=data,
|
2016-04-19 10:52:52 +01:00
|
|
|
|
page=page,
|
2016-04-28 16:10:35 +01:00
|
|
|
|
page_size=page_size,
|
2016-06-30 18:43:15 +01:00
|
|
|
|
limit_days=limit_days,
|
2016-09-15 16:01:26 +01:00
|
|
|
|
key_type=api_user.key_type,
|
|
|
|
|
|
include_jobs=include_jobs)
|
2016-03-01 13:30:10 +00:00
|
|
|
|
return jsonify(
|
2016-07-26 14:33:14 +01:00
|
|
|
|
notifications=notification_with_personalisation_schema.dump(pagination.items, many=True).data,
|
2016-04-19 10:52:52 +01:00
|
|
|
|
page_size=page_size,
|
|
|
|
|
|
total=pagination.total,
|
2016-03-01 13:30:10 +00:00
|
|
|
|
links=pagination_links(
|
2016-03-21 12:37:34 +00:00
|
|
|
|
pagination,
|
2016-03-01 13:30:10 +00:00
|
|
|
|
'.get_all_notifications',
|
2016-03-16 16:47:18 +00:00
|
|
|
|
**request.args.to_dict()
|
2016-03-01 13:30:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
|
@notifications.route('/notifications/<string:notification_type>', methods=['POST'])
|
2016-02-25 11:35:32 +00:00
|
|
|
|
def send_notification(notification_type):
|
2017-03-30 10:46:23 +01:00
|
|
|
|
|
2017-08-23 14:56:03 +01:00
|
|
|
|
if notification_type not in [SMS_TYPE, EMAIL_TYPE]:
|
|
|
|
|
|
msg = "{} notification type is not supported".format(notification_type)
|
|
|
|
|
|
msg = msg + ", please use the latest version of the client" if notification_type == LETTER_TYPE else msg
|
|
|
|
|
|
raise InvalidRequest(msg, 400)
|
2016-02-24 09:55:05 +00:00
|
|
|
|
|
2017-01-18 09:56:26 +00:00
|
|
|
|
notification_form, errors = (
|
2016-07-21 12:20:50 +01:00
|
|
|
|
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
2016-02-29 11:03:48 +00:00
|
|
|
|
).load(request.get_json())
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2016-09-23 15:46:48 +01:00
|
|
|
|
if errors:
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
|
check_rate_limiting(authenticated_service, api_user)
|
2016-09-06 14:21:36 +01:00
|
|
|
|
|
2020-03-04 17:04:11 +00:00
|
|
|
|
template, template_with_content = validate_template(
|
2017-04-26 15:56:45 +01:00
|
|
|
|
template_id=notification_form['template'],
|
2020-03-04 17:04:11 +00:00
|
|
|
|
personalisation=notification_form.get('personalisation', {}),
|
|
|
|
|
|
service=authenticated_service,
|
|
|
|
|
|
notification_type=notification_type
|
|
|
|
|
|
)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
|
_service_allowed_to_send_to(notification_form, authenticated_service)
|
2017-06-30 15:00:44 +01:00
|
|
|
|
if not service_has_permission(notification_type, authenticated_service.permissions):
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
{'service': ["Cannot send {}".format(get_public_notify_type_text(notification_type, plural=True))]},
|
|
|
|
|
|
status_code=400
|
|
|
|
|
|
)
|
2017-06-29 18:02:21 +01:00
|
|
|
|
|
2017-06-30 15:00:44 +01:00
|
|
|
|
if notification_type == SMS_TYPE:
|
2020-06-16 17:55:02 +01:00
|
|
|
|
check_if_service_can_send_to_number(authenticated_service, notification_form['to'])
|
2017-01-17 12:08:24 +00:00
|
|
|
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
2017-01-18 09:56:26 +00:00
|
|
|
|
simulated = simulated_recipient(notification_form['to'], notification_type)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
notification_model = persist_notification(template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
2020-08-04 15:41:42 +01:00
|
|
|
|
postage=template.postage,
|
2017-04-26 15:56:45 +01:00
|
|
|
|
recipient=request.get_json()['to'],
|
2017-05-05 15:23:06 +01:00
|
|
|
|
service=authenticated_service,
|
2017-03-30 10:46:23 +01:00
|
|
|
|
personalisation=notification_form.get('personalisation', None),
|
|
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
|
api_key_id=api_user.id,
|
|
|
|
|
|
key_type=api_user.key_type,
|
2017-11-27 12:30:50 +00:00
|
|
|
|
simulated=simulated,
|
2020-06-22 10:20:51 +01:00
|
|
|
|
reply_to_text=template.reply_to_text
|
2017-11-27 12:30:50 +00:00
|
|
|
|
)
|
2017-01-17 12:08:24 +00:00
|
|
|
|
if not simulated:
|
2017-05-25 10:51:49 +01:00
|
|
|
|
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
2017-03-30 10:46:23 +01:00
|
|
|
|
send_notification_to_queue(notification=notification_model,
|
2017-05-05 15:23:06 +01:00
|
|
|
|
research_mode=authenticated_service.research_mode,
|
2017-03-30 10:46:23 +01:00
|
|
|
|
queue=queue_name)
|
2017-01-17 12:08:24 +00:00
|
|
|
|
else:
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
|
current_app.logger.debug("POST simulated notification for id: {}".format(notification_model.id))
|
2017-01-18 09:56:26 +00:00
|
|
|
|
notification_form.update({"template_version": template.version})
|
2017-01-17 12:08:24 +00:00
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
return jsonify(
|
|
|
|
|
|
data=get_notification_return_data(
|
2017-01-18 09:56:26 +00:00
|
|
|
|
notification_model.id,
|
|
|
|
|
|
notification_form,
|
2020-03-04 17:04:11 +00:00
|
|
|
|
template_with_content)
|
2016-06-15 12:11:48 +01:00
|
|
|
|
), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_notification_return_data(notification_id, notification, template):
|
|
|
|
|
|
output = {
|
|
|
|
|
|
'template_version': notification['template_version'],
|
2020-04-13 13:48:23 +01:00
|
|
|
|
'notification': {'id': notification_id},
|
|
|
|
|
|
'body': template.content_with_placeholders_filled_in,
|
2016-06-15 12:11:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-13 13:48:23 +01:00
|
|
|
|
if hasattr(template, 'subject'):
|
2020-04-06 14:25:43 +01:00
|
|
|
|
output['subject'] = template.subject
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
|
|
return output
|
2016-05-19 16:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
def _service_allowed_to_send_to(notification, service):
|
|
|
|
|
|
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
|
|
|
|
|
|
if api_user.key_type == KEY_TYPE_TEAM:
|
|
|
|
|
|
message = 'Can’t send to this recipient using a team-only API key'
|
|
|
|
|
|
else:
|
|
|
|
|
|
message = (
|
|
|
|
|
|
'Can’t send to this recipient when service is in trial mode '
|
|
|
|
|
|
'– see https://www.notifications.service.gov.uk/trial-mode'
|
|
|
|
|
|
)
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
{'to': [message]},
|
|
|
|
|
|
status_code=400
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_template_object_for_notification(template, personalisation):
|
2020-04-06 14:25:43 +01:00
|
|
|
|
template_object = template._as_utils_template_with_personalisation(personalisation)
|
2016-12-09 15:56:25 +00:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
if template_object.missing_data:
|
|
|
|
|
|
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
|
|
|
|
|
errors = {'template': [message]}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
template_object.template_type == SMS_TYPE and
|
2019-11-18 16:16:28 +00:00
|
|
|
|
template_object.is_message_too_long()
|
2016-10-03 10:57:10 +01:00
|
|
|
|
):
|
2018-08-16 16:25:58 +01:00
|
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
errors = {'content': [message]}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
return template_object
|