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
|
|
|
|
2016-11-25 17:32:01 +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
|
2016-10-27 17:34:54 +01:00
|
|
|
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
|
|
|
|
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE
|
2016-11-21 15:11:19 +00:00
|
|
|
from app.v2.errors import BadRequestError, SendNotificationToQueueError
|
2016-12-09 15:56:25 +00:00
|
|
|
from app.utils import get_template_instance
|
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
|
|
|
|
|
|
|
|
if template_object.additional_data:
|
|
|
|
|
message = 'Template personalisation not needed for template: {}'.format(
|
|
|
|
|
", ".join(template_object.additional_data))
|
2016-10-31 12:22:26 +00:00
|
|
|
raise BadRequestError(fields=[{'template': message}], message=message)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
|
|
|
|
2016-10-27 17:34:54 +01:00
|
|
|
def persist_notification(template_id,
|
|
|
|
|
template_version,
|
|
|
|
|
recipient,
|
2016-12-19 16:45:18 +00:00
|
|
|
service,
|
2016-10-27 17:34:54 +01:00
|
|
|
personalisation,
|
|
|
|
|
notification_type,
|
|
|
|
|
api_key_id,
|
2016-11-11 10:41:39 +00:00
|
|
|
key_type,
|
2016-11-11 14:56:33 +00:00
|
|
|
created_at=None,
|
2016-11-11 10:41:39 +00:00
|
|
|
job_id=None,
|
2016-11-16 15:44:16 +00:00
|
|
|
job_row_number=None,
|
2016-11-25 17:32:01 +00:00
|
|
|
reference=None,
|
|
|
|
|
notification_id=None):
|
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,
|
2016-11-21 17:32:36 +00:00
|
|
|
created_at=created_at or datetime.utcnow(),
|
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,
|
2016-11-17 13:42:34 +00:00
|
|
|
client_reference=reference
|
2016-11-11 14:56:33 +00:00
|
|
|
)
|
2016-10-27 17:34:54 +01:00
|
|
|
dao_create_notification(notification)
|
2016-12-19 16:45:18 +00:00
|
|
|
redis_store.incr(redis.daily_limit_cache_key(service.id))
|
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:
|
|
|
|
|
queue = 'research-mode'
|
|
|
|
|
elif not queue:
|
2016-10-27 17:34:54 +01:00
|
|
|
if notification.notification_type == SMS_TYPE:
|
2016-12-09 12:10:42 +00:00
|
|
|
queue = 'send-sms'
|
2016-10-27 17:34:54 +01:00
|
|
|
if notification.notification_type == EMAIL_TYPE:
|
2016-12-09 12:10:42 +00:00
|
|
|
queue = 'send-email'
|
|
|
|
|
|
|
|
|
|
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)
|
2016-10-27 17:34:54 +01:00
|
|
|
except Exception as e:
|
2016-11-21 15:11:19 +00:00
|
|
|
current_app.logger.exception(e)
|
2016-10-27 17:34:54 +01:00
|
|
|
dao_delete_notifications_and_history_by_id(notification.id)
|
2016-11-21 15:11:19 +00:00
|
|
|
raise SendNotificationToQueueError()
|
2016-10-27 17:34:54 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
|
|
|
|
|
)
|