2016-11-11 14:56:33 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2016-10-27 17:34:54 +01:00
|
|
|
from flask import current_app
|
2016-10-27 11:46:37 +01:00
|
|
|
|
2017-04-27 12:41:10 +01:00
|
|
|
from notifications_utils.recipients import (
|
|
|
|
|
get_international_phone_info,
|
2017-05-23 15:45:11 +01:00
|
|
|
validate_and_format_phone_number,
|
|
|
|
|
format_email_address
|
2017-04-27 12:41:10 +01:00
|
|
|
)
|
|
|
|
|
|
2017-02-14 14:22:52 +00:00
|
|
|
from app import redis_store
|
2016-10-27 17:34:54 +01:00
|
|
|
from app.celery import provider_tasks
|
2016-12-01 17:20:05 +00:00
|
|
|
from notifications_utils.clients import redis
|
2017-05-25 10:51:49 +01:00
|
|
|
|
|
|
|
|
from app.config import QueueNames
|
2017-05-30 10:18:18 +01:00
|
|
|
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE, ScheduledNotification
|
2017-05-15 17:27:38 +01:00
|
|
|
from app.dao.notifications_dao import (dao_create_notification,
|
|
|
|
|
dao_delete_notifications_and_history_by_id,
|
|
|
|
|
dao_created_scheduled_notification)
|
2017-06-19 14:58:38 +01:00
|
|
|
from app.v2.errors import BadRequestError
|
2017-05-17 15:06:15 +01:00
|
|
|
from app.utils import get_template_instance, cache_key_for_service_template_counter, convert_bst_to_utc
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_content_for_notification(template, personalisation):
|
2016-12-09 15:56:25 +00:00
|
|
|
template_object = get_template_instance(template.__dict__, personalisation)
|
2016-10-28 17:10:00 +01:00
|
|
|
check_placeholders(template_object)
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
return template_object
|
|
|
|
|
|
|
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
def check_placeholders(template_object):
|
|
|
|
|
if template_object.missing_data:
|
|
|
|
|
message = 'Template missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
2016-10-31 12:22:26 +00:00
|
|
|
raise BadRequestError(fields=[{'template': message}], message=message)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
|
|
|
|
2017-04-27 12:41:10 +01:00
|
|
|
def persist_notification(
|
|
|
|
|
template_id,
|
|
|
|
|
template_version,
|
|
|
|
|
recipient,
|
|
|
|
|
service,
|
|
|
|
|
personalisation,
|
|
|
|
|
notification_type,
|
|
|
|
|
api_key_id,
|
|
|
|
|
key_type,
|
2017-05-17 13:27:05 +01:00
|
|
|
created_at=None,
|
2017-04-27 12:41:10 +01:00
|
|
|
job_id=None,
|
|
|
|
|
job_row_number=None,
|
|
|
|
|
reference=None,
|
|
|
|
|
client_reference=None,
|
|
|
|
|
notification_id=None,
|
2017-06-16 16:30:03 +01:00
|
|
|
simulated=False,
|
|
|
|
|
created_by_id=None
|
2017-04-27 12:41:10 +01:00
|
|
|
):
|
2017-05-05 15:21:13 +01:00
|
|
|
|
2017-05-17 13:25:40 +01:00
|
|
|
notification_created_at = created_at or datetime.utcnow()
|
|
|
|
|
|
2016-11-14 14:41:32 +00:00
|
|
|
notification = Notification(
|
2016-11-25 17:32:01 +00:00
|
|
|
id=notification_id,
|
2016-11-11 14:56:33 +00:00
|
|
|
template_id=template_id,
|
|
|
|
|
template_version=template_version,
|
2016-11-14 14:41:32 +00:00
|
|
|
to=recipient,
|
2016-12-19 16:45:18 +00:00
|
|
|
service_id=service.id,
|
|
|
|
|
service=service,
|
2016-11-11 14:56:33 +00:00
|
|
|
personalisation=personalisation,
|
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
api_key_id=api_key_id,
|
|
|
|
|
key_type=key_type,
|
2017-05-17 13:25:40 +01:00
|
|
|
created_at=notification_created_at,
|
2016-11-11 14:56:33 +00:00
|
|
|
job_id=job_id,
|
2016-11-16 15:44:16 +00:00
|
|
|
job_row_number=job_row_number,
|
2017-04-12 17:56:55 +01:00
|
|
|
client_reference=client_reference,
|
|
|
|
|
reference=reference
|
2016-11-11 14:56:33 +00:00
|
|
|
)
|
2017-04-27 12:41:10 +01:00
|
|
|
|
|
|
|
|
if notification_type == SMS_TYPE:
|
|
|
|
|
formatted_recipient = validate_and_format_phone_number(recipient, international=True)
|
|
|
|
|
recipient_info = get_international_phone_info(formatted_recipient)
|
2017-05-23 14:47:55 +01:00
|
|
|
notification.normalised_to = formatted_recipient
|
2017-04-27 12:41:10 +01:00
|
|
|
notification.international = recipient_info.international
|
|
|
|
|
notification.phone_prefix = recipient_info.country_prefix
|
|
|
|
|
notification.rate_multiplier = recipient_info.billable_units
|
2017-05-23 15:45:11 +01:00
|
|
|
elif notification_type == EMAIL_TYPE:
|
|
|
|
|
notification.normalised_to = format_email_address(notification.to)
|
2017-04-27 12:41:10 +01:00
|
|
|
|
|
|
|
|
# if simulated create a Notification model to return but do not persist the Notification to the dB
|
2017-01-17 12:08:24 +00:00
|
|
|
if not simulated:
|
2017-03-30 10:46:23 +01:00
|
|
|
dao_create_notification(notification)
|
2017-03-30 13:43:44 +01:00
|
|
|
if key_type != KEY_TYPE_TEST:
|
|
|
|
|
if redis_store.get(redis.daily_limit_cache_key(service.id)):
|
|
|
|
|
redis_store.incr(redis.daily_limit_cache_key(service.id))
|
|
|
|
|
if redis_store.get_all_from_hash(cache_key_for_service_template_counter(service.id)):
|
|
|
|
|
redis_store.increment_hash_value(cache_key_for_service_template_counter(service.id), template_id)
|
2017-01-18 09:56:26 +00:00
|
|
|
current_app.logger.info(
|
2017-05-17 13:25:40 +01:00
|
|
|
"{} {} created at {}".format(notification_type, notification_id, notification_created_at)
|
2017-01-18 09:56:26 +00:00
|
|
|
)
|
2016-10-27 17:34:54 +01:00
|
|
|
return notification
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
2016-12-09 12:10:42 +00:00
|
|
|
def send_notification_to_queue(notification, research_mode, queue=None):
|
|
|
|
|
if research_mode or notification.key_type == KEY_TYPE_TEST:
|
2017-05-25 10:51:49 +01:00
|
|
|
queue = QueueNames.RESEARCH_MODE
|
2016-12-09 12:10:42 +00:00
|
|
|
elif not queue:
|
2017-05-25 10:51:49 +01:00
|
|
|
queue = QueueNames.SEND
|
2016-12-09 12:10:42 +00:00
|
|
|
|
|
|
|
|
if notification.notification_type == SMS_TYPE:
|
|
|
|
|
deliver_task = provider_tasks.deliver_sms
|
|
|
|
|
if notification.notification_type == EMAIL_TYPE:
|
|
|
|
|
deliver_task = provider_tasks.deliver_email
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
deliver_task.apply_async([str(notification.id)], queue=queue)
|
2017-06-19 14:58:38 +01:00
|
|
|
except Exception:
|
2016-10-27 17:34:54 +01:00
|
|
|
dao_delete_notifications_and_history_by_id(notification.id)
|
2017-06-19 14:58:38 +01:00
|
|
|
raise
|
2016-10-27 17:34:54 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info(
|
2017-01-18 09:56:26 +00:00
|
|
|
"{} {} sent to the {} queue for delivery".format(notification.notification_type,
|
|
|
|
|
notification.id,
|
|
|
|
|
queue))
|
2017-01-17 12:08:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def simulated_recipient(to_address, notification_type):
|
2017-04-26 15:56:45 +01:00
|
|
|
if notification_type == SMS_TYPE:
|
|
|
|
|
formatted_simulated_numbers = [
|
|
|
|
|
validate_and_format_phone_number(number) for number in current_app.config['SIMULATED_SMS_NUMBERS']
|
|
|
|
|
]
|
|
|
|
|
return to_address in formatted_simulated_numbers
|
|
|
|
|
else:
|
|
|
|
|
return to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES']
|
2017-05-15 17:27:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def persist_scheduled_notification(notification_id, scheduled_for):
|
2017-05-22 14:15:35 +01:00
|
|
|
scheduled_datetime = convert_bst_to_utc(datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M"))
|
2017-05-15 17:27:38 +01:00
|
|
|
scheduled_notification = ScheduledNotification(notification_id=notification_id,
|
2017-05-17 15:06:15 +01:00
|
|
|
scheduled_for=scheduled_datetime)
|
2017-05-15 17:27:38 +01:00
|
|
|
dao_created_scheduled_notification(scheduled_notification)
|