2016-09-20 17:24:28 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
from notifications_utils.recipients import (
|
|
|
|
|
validate_and_format_phone_number
|
|
|
|
|
)
|
2016-12-09 15:56:25 +00:00
|
|
|
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate
|
2016-09-20 17:24:28 +01:00
|
|
|
|
|
|
|
|
from app import clients, statsd_client, create_uuid
|
2016-09-23 15:59:23 +01:00
|
|
|
from app.dao.notifications_dao import dao_update_notification
|
2016-09-20 17:24:28 +01:00
|
|
|
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
|
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
|
|
|
|
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
|
|
|
|
|
|
|
|
|
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE
|
|
|
|
|
|
|
|
|
|
|
2016-09-21 13:27:32 +01:00
|
|
|
def send_sms_to_provider(notification):
|
2016-09-20 17:24:28 +01:00
|
|
|
service = dao_fetch_service_by_id(notification.service_id)
|
2016-09-21 13:27:32 +01:00
|
|
|
provider = provider_to_use(SMS_TYPE, notification.id)
|
2016-09-20 17:24:28 +01:00
|
|
|
if notification.status == 'created':
|
|
|
|
|
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
2016-12-09 15:56:25 +00:00
|
|
|
template = SMSMessageTemplate(
|
2016-09-20 17:24:28 +01:00
|
|
|
template_model.__dict__,
|
2016-12-09 15:56:25 +00:00
|
|
|
values=notification.personalisation,
|
|
|
|
|
prefix=service.name,
|
|
|
|
|
sender=service.sms_sender
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
|
|
|
|
send_sms_response.apply_async(
|
2016-09-21 13:27:32 +01:00
|
|
|
(provider.get_name(), str(notification.id), notification.to), queue='research-mode'
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
notification.billable_units = 0
|
|
|
|
|
else:
|
|
|
|
|
provider.send_sms(
|
|
|
|
|
to=validate_and_format_phone_number(notification.to),
|
2016-12-09 15:56:25 +00:00
|
|
|
content=str(template),
|
2016-09-22 09:16:58 +01:00
|
|
|
reference=str(notification.id),
|
2016-09-20 17:24:28 +01:00
|
|
|
sender=service.sms_sender
|
|
|
|
|
)
|
2016-12-09 15:56:25 +00:00
|
|
|
notification.billable_units = template.fragment_count
|
2016-09-20 17:24:28 +01:00
|
|
|
|
|
|
|
|
notification.sent_at = datetime.utcnow()
|
|
|
|
|
notification.sent_by = provider.get_name()
|
|
|
|
|
notification.status = 'sending'
|
|
|
|
|
dao_update_notification(notification)
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
2016-09-22 09:16:58 +01:00
|
|
|
"SMS {} sent to provider at {}".format(notification.id, notification.sent_at)
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
|
|
|
|
statsd_client.timing("sms.total-time", delta_milliseconds)
|
|
|
|
|
|
|
|
|
|
|
2016-09-22 09:16:58 +01:00
|
|
|
def send_email_to_provider(notification):
|
2016-09-20 17:24:28 +01:00
|
|
|
service = dao_fetch_service_by_id(notification.service_id)
|
2016-09-22 09:16:58 +01:00
|
|
|
provider = provider_to_use(EMAIL_TYPE, notification.id)
|
2016-09-20 17:24:28 +01:00
|
|
|
if notification.status == 'created':
|
|
|
|
|
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
html_email = HTMLEmailTemplate(
|
2016-09-20 17:24:28 +01:00
|
|
|
template_dict,
|
|
|
|
|
values=notification.personalisation,
|
2016-12-09 15:56:25 +00:00
|
|
|
**get_html_email_options(service)
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
plain_text_email = PlainTextEmailTemplate(
|
2016-09-20 17:24:28 +01:00
|
|
|
template_dict,
|
2016-12-09 15:56:25 +00:00
|
|
|
values=notification.personalisation
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
|
|
|
|
reference = str(create_uuid())
|
|
|
|
|
send_email_response.apply_async(
|
|
|
|
|
(provider.get_name(), reference, notification.to), queue='research-mode'
|
|
|
|
|
)
|
2016-09-23 09:54:53 +01:00
|
|
|
notification.billable_units = 0
|
2016-09-20 17:24:28 +01:00
|
|
|
else:
|
|
|
|
|
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
|
|
|
|
|
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
|
|
|
|
reference = provider.send_email(
|
|
|
|
|
from_address,
|
|
|
|
|
notification.to,
|
2016-12-09 15:56:25 +00:00
|
|
|
plain_text_email.subject,
|
|
|
|
|
body=str(plain_text_email),
|
|
|
|
|
html_body=str(html_email),
|
2016-09-20 17:24:28 +01:00
|
|
|
reply_to_address=service.reply_to_email_address,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
notification.reference = reference
|
|
|
|
|
notification.sent_at = datetime.utcnow()
|
|
|
|
|
notification.sent_by = provider.get_name(),
|
|
|
|
|
notification.status = 'sending'
|
|
|
|
|
dao_update_notification(notification)
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
2016-09-22 09:16:58 +01:00
|
|
|
"Email {} sent to provider at {}".format(notification.id, notification.sent_at)
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
|
|
|
|
statsd_client.timing("email.total-time", delta_milliseconds)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def provider_to_use(notification_type, notification_id):
|
|
|
|
|
active_providers_in_order = [
|
|
|
|
|
provider for provider in get_provider_details_by_notification_type(notification_type) if provider.active
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if not active_providers_in_order:
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"{} {} failed as no active providers".format(notification_type, notification_id)
|
|
|
|
|
)
|
|
|
|
|
raise Exception("No active {} providers".format(notification_type))
|
|
|
|
|
|
|
|
|
|
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
|
|
|
|
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
def get_html_email_options(service):
|
2016-09-20 17:24:28 +01:00
|
|
|
govuk_banner = service.branding != BRANDING_ORG
|
|
|
|
|
if service.organisation:
|
|
|
|
|
logo = '{}{}{}'.format(
|
|
|
|
|
current_app.config['ADMIN_BASE_URL'],
|
|
|
|
|
current_app.config['BRANDING_PATH'],
|
|
|
|
|
service.organisation.logo
|
|
|
|
|
)
|
|
|
|
|
branding = {
|
|
|
|
|
'brand_colour': service.organisation.colour,
|
|
|
|
|
'brand_logo': logo,
|
|
|
|
|
'brand_name': service.organisation.name,
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
branding = {}
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
return dict(govuk_banner=govuk_banner, **branding)
|