2019-10-14 15:01:08 +01:00
|
|
|
import random
|
2016-12-21 16:15:18 +00:00
|
|
|
from urllib import parse
|
2019-11-20 17:23:39 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-12-23 15:33:27 +00:00
|
|
|
from cachetools import TTLCache, cached
|
2016-09-20 17:24:28 +01:00
|
|
|
from flask import current_app
|
2016-12-09 15:56:25 +00:00
|
|
|
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2020-11-17 12:35:18 +00:00
|
|
|
from app import notification_provider_clients, statsd_client, create_uuid
|
2021-02-16 14:24:40 +00:00
|
|
|
from app.dao.email_branding_dao import dao_get_email_branding_by_id
|
2017-10-06 17:08:06 +01:00
|
|
|
from app.dao.notifications_dao import (
|
2017-11-29 14:38:38 +00:00
|
|
|
dao_update_notification
|
|
|
|
|
)
|
2017-01-20 16:14:29 +00:00
|
|
|
from app.dao.provider_details_dao import (
|
|
|
|
|
get_provider_details_by_notification_type,
|
2019-11-11 16:14:14 +00:00
|
|
|
dao_reduce_sms_provider_priority
|
2017-01-20 16:14:29 +00:00
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
2018-03-16 17:18:44 +00:00
|
|
|
from app.exceptions import NotificationTechnicalFailureException
|
2017-09-14 16:26:46 +01:00
|
|
|
from app.models import (
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
KEY_TYPE_TEST,
|
2018-08-28 14:25:16 +01:00
|
|
|
BRANDING_BOTH,
|
2017-09-19 13:18:59 +01:00
|
|
|
BRANDING_ORG_BANNER,
|
2017-09-14 16:26:46 +01:00
|
|
|
EMAIL_TYPE,
|
|
|
|
|
NOTIFICATION_TECHNICAL_FAILURE,
|
|
|
|
|
NOTIFICATION_SENT,
|
2020-07-02 18:22:06 +01:00
|
|
|
NOTIFICATION_SENDING,
|
|
|
|
|
NOTIFICATION_STATUS_TYPES_COMPLETED
|
2017-09-14 16:26:46 +01:00
|
|
|
)
|
2021-02-16 14:24:40 +00:00
|
|
|
from app.serialised_models import SerialisedTemplate, SerialisedService
|
2017-05-09 18:17:55 +01:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2016-09-21 13:27:32 +01:00
|
|
|
def send_sms_to_provider(notification):
|
2021-02-16 14:24:40 +00:00
|
|
|
service = SerialisedService.from_id(notification.service_id)
|
2021-02-16 10:19:53 +00:00
|
|
|
|
2017-02-06 16:20:44 +00:00
|
|
|
if not service.active:
|
|
|
|
|
technical_failure(notification=notification)
|
2017-01-31 13:53:13 +00:00
|
|
|
return
|
|
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
if notification.status == 'created':
|
2019-10-14 15:01:08 +01:00
|
|
|
provider = provider_to_use(SMS_TYPE, notification.international)
|
2019-05-08 16:31:51 +01:00
|
|
|
|
2021-02-16 14:24:40 +00:00
|
|
|
template_model = SerialisedTemplate.from_id_and_service_id(
|
|
|
|
|
template_id=notification.template_id, service_id=service.id, version=notification.template_version
|
|
|
|
|
)
|
2017-11-03 10:31:28 +00:00
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
template = SMSMessageTemplate(
|
2021-02-16 10:19:53 +00:00
|
|
|
template_model.__dict__,
|
2016-12-09 15:56:25 +00:00
|
|
|
values=notification.personalisation,
|
|
|
|
|
prefix=service.name,
|
2017-11-16 13:24:33 +00:00
|
|
|
show_prefix=service.prefix_sms,
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
2021-02-16 14:24:40 +00:00
|
|
|
created_at = notification.created_at
|
|
|
|
|
key_type = notification.key_type
|
2016-09-20 17:24:28 +01:00
|
|
|
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
2019-05-08 10:54:01 +01:00
|
|
|
update_notification_to_sending(notification, provider)
|
2019-05-23 16:40:17 +01:00
|
|
|
send_sms_response(provider.get_name(), str(notification.id), notification.to)
|
2021-02-16 10:19:53 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
else:
|
2017-01-20 16:14:29 +00:00
|
|
|
try:
|
|
|
|
|
provider.send_sms(
|
2021-02-16 14:24:40 +00:00
|
|
|
to=notification.normalised_to,
|
2017-01-20 16:14:29 +00:00
|
|
|
content=str(template),
|
|
|
|
|
reference=str(notification.id),
|
2017-11-27 15:24:16 +00:00
|
|
|
sender=notification.reply_to_text
|
2017-01-20 16:14:29 +00:00
|
|
|
)
|
|
|
|
|
except Exception as e:
|
2019-05-08 10:54:01 +01:00
|
|
|
notification.billable_units = template.fragment_count
|
|
|
|
|
dao_update_notification(notification)
|
2019-11-20 17:23:39 +00:00
|
|
|
dao_reduce_sms_provider_priority(provider.get_name(), time_threshold=timedelta(minutes=1))
|
2017-01-20 16:14:29 +00:00
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
notification.billable_units = template.fragment_count
|
2019-05-08 15:45:19 +01:00
|
|
|
update_notification_to_sending(notification, provider)
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2021-02-16 14:24:40 +00:00
|
|
|
delta_seconds = (datetime.utcnow() - created_at).total_seconds()
|
2020-06-11 15:01:15 +01:00
|
|
|
statsd_client.timing("sms.total-time", delta_seconds)
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2021-02-16 14:24:40 +00:00
|
|
|
if key_type == KEY_TYPE_TEST:
|
2020-11-30 12:07:32 +00:00
|
|
|
statsd_client.timing("sms.test-key.total-time", delta_seconds)
|
|
|
|
|
else:
|
|
|
|
|
statsd_client.timing("sms.live-key.total-time", delta_seconds)
|
2021-02-16 10:19:53 +00:00
|
|
|
if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE'):
|
2020-11-30 15:03:52 +00:00
|
|
|
statsd_client.timing("sms.live-key.high-volume.total-time", delta_seconds)
|
|
|
|
|
else:
|
|
|
|
|
statsd_client.timing("sms.live-key.not-high-volume.total-time", delta_seconds)
|
2020-11-30 12:07:32 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2016-09-22 09:16:58 +01:00
|
|
|
def send_email_to_provider(notification):
|
2021-02-16 14:24:40 +00:00
|
|
|
service = SerialisedService.from_id(notification.service_id)
|
|
|
|
|
|
2017-02-06 16:20:44 +00:00
|
|
|
if not service.active:
|
|
|
|
|
technical_failure(notification=notification)
|
2017-01-31 13:53:13 +00:00
|
|
|
return
|
2016-09-20 17:24:28 +01:00
|
|
|
if notification.status == 'created':
|
2019-10-14 15:01:08 +01:00
|
|
|
provider = provider_to_use(EMAIL_TYPE)
|
2019-05-08 15:55:45 +01:00
|
|
|
|
2021-02-16 14:24:40 +00:00
|
|
|
template_dict = SerialisedTemplate.from_id_and_service_id(
|
|
|
|
|
template_id=notification.template_id, service_id=service.id, version=notification.template_version
|
|
|
|
|
).__dict__
|
2016-09-20 17:24:28 +01:00
|
|
|
|
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
|
|
|
)
|
2021-02-16 14:24:40 +00:00
|
|
|
created_at = notification.created_at
|
|
|
|
|
key_type = notification.key_type
|
2016-09-20 17:24:28 +01:00
|
|
|
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
2019-05-08 15:55:45 +01:00
|
|
|
notification.reference = str(create_uuid())
|
2019-05-08 10:54:01 +01:00
|
|
|
update_notification_to_sending(notification, provider)
|
2019-05-08 15:55:45 +01:00
|
|
|
send_email_response(notification.reference, notification.to)
|
2016-09-20 17:24:28 +01:00
|
|
|
else:
|
|
|
|
|
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
|
|
|
|
|
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
2021-02-16 10:19:53 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
reference = provider.send_email(
|
|
|
|
|
from_address,
|
2021-02-16 14:24:40 +00:00
|
|
|
notification.normalised_to,
|
2016-12-09 15:56:25 +00:00
|
|
|
plain_text_email.subject,
|
|
|
|
|
body=str(plain_text_email),
|
|
|
|
|
html_body=str(html_email),
|
2021-02-16 14:24:40 +00:00
|
|
|
reply_to_address=notification.reply_to_text
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
2017-01-24 14:24:58 +00:00
|
|
|
notification.reference = reference
|
2019-05-08 10:54:01 +01:00
|
|
|
update_notification_to_sending(notification, provider)
|
2021-02-16 14:24:40 +00:00
|
|
|
delta_seconds = (datetime.utcnow() - created_at).total_seconds()
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2021-02-16 14:24:40 +00:00
|
|
|
if key_type == KEY_TYPE_TEST:
|
2020-11-30 12:07:32 +00:00
|
|
|
statsd_client.timing("email.test-key.total-time", delta_seconds)
|
|
|
|
|
else:
|
|
|
|
|
statsd_client.timing("email.live-key.total-time", delta_seconds)
|
2021-02-16 10:19:53 +00:00
|
|
|
if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE'):
|
2020-11-30 15:03:52 +00:00
|
|
|
statsd_client.timing("email.live-key.high-volume.total-time", delta_seconds)
|
|
|
|
|
else:
|
|
|
|
|
statsd_client.timing("email.live-key.not-high-volume.total-time", delta_seconds)
|
2020-11-30 12:07:32 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2019-05-08 15:45:19 +01:00
|
|
|
def update_notification_to_sending(notification, provider):
|
2017-01-24 14:24:58 +00:00
|
|
|
notification.sent_at = datetime.utcnow()
|
|
|
|
|
notification.sent_by = provider.get_name()
|
2020-07-02 18:22:06 +01:00
|
|
|
if notification.status not in NOTIFICATION_STATUS_TYPES_COMPLETED:
|
|
|
|
|
notification.status = NOTIFICATION_SENT if notification.international else NOTIFICATION_SENDING
|
2017-01-24 14:24:58 +00:00
|
|
|
dao_update_notification(notification)
|
|
|
|
|
|
|
|
|
|
|
2020-12-31 09:36:55 +00:00
|
|
|
provider_cache = TTLCache(maxsize=8, ttl=10)
|
2020-12-23 15:33:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@cached(cache=provider_cache)
|
2019-10-14 15:01:08 +01:00
|
|
|
def provider_to_use(notification_type, international=False):
|
|
|
|
|
active_providers = [
|
2017-04-27 12:14:22 +01:00
|
|
|
p for p in get_provider_details_by_notification_type(notification_type, international) if p.active
|
|
|
|
|
]
|
|
|
|
|
|
2019-10-14 15:01:08 +01:00
|
|
|
if not active_providers:
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.error(
|
2019-10-14 15:01:08 +01:00
|
|
|
"{} failed as no active providers".format(notification_type)
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
|
|
|
|
raise Exception("No active {} providers".format(notification_type))
|
|
|
|
|
|
2019-10-14 15:01:08 +01:00
|
|
|
chosen_provider = random.choices(active_providers, weights=[p.priority for p in active_providers])[0]
|
|
|
|
|
|
2020-11-17 12:35:18 +00:00
|
|
|
return notification_provider_clients.get_client_by_name_and_type(chosen_provider.identifier, notification_type)
|
2016-09-20 17:24:28 +01:00
|
|
|
|
|
|
|
|
|
2017-07-21 16:06:12 +01:00
|
|
|
def get_logo_url(base_url, logo_file):
|
2016-12-21 16:15:18 +00:00
|
|
|
base_url = parse.urlparse(base_url)
|
|
|
|
|
netloc = base_url.netloc
|
|
|
|
|
|
2017-07-21 16:06:12 +01:00
|
|
|
if base_url.netloc.startswith('localhost'):
|
|
|
|
|
netloc = 'notify.tools'
|
|
|
|
|
elif base_url.netloc.startswith('www'):
|
|
|
|
|
# strip "www."
|
|
|
|
|
netloc = base_url.netloc[4:]
|
2016-12-21 16:15:18 +00:00
|
|
|
|
|
|
|
|
logo_url = parse.ParseResult(
|
|
|
|
|
scheme=base_url.scheme,
|
2017-07-21 16:37:26 +01:00
|
|
|
netloc='static-logos.' + netloc,
|
2017-07-21 16:06:12 +01:00
|
|
|
path=logo_file,
|
2016-12-21 16:15:18 +00:00
|
|
|
params=base_url.params,
|
|
|
|
|
query=base_url.query,
|
|
|
|
|
fragment=base_url.fragment
|
|
|
|
|
)
|
|
|
|
|
return parse.urlunparse(logo_url)
|
|
|
|
|
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
def get_html_email_options(service):
|
2018-08-30 16:22:59 +01:00
|
|
|
if service.email_branding is None:
|
2018-08-28 14:25:16 +01:00
|
|
|
return {
|
|
|
|
|
'govuk_banner': True,
|
|
|
|
|
'brand_banner': False,
|
2016-09-20 17:24:28 +01:00
|
|
|
}
|
2021-02-18 12:54:22 +00:00
|
|
|
if isinstance(service, SerialisedService):
|
2021-02-16 14:24:40 +00:00
|
|
|
branding = dao_get_email_branding_by_id(service.email_branding)
|
|
|
|
|
else:
|
|
|
|
|
branding = service.email_branding
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2018-08-28 14:25:16 +01:00
|
|
|
logo_url = get_logo_url(
|
|
|
|
|
current_app.config['ADMIN_BASE_URL'],
|
2021-02-16 14:24:40 +00:00
|
|
|
branding.logo
|
|
|
|
|
) if branding.logo else None
|
2018-08-28 14:25:16 +01:00
|
|
|
|
|
|
|
|
return {
|
2021-02-16 14:24:40 +00:00
|
|
|
'govuk_banner': branding.brand_type == BRANDING_BOTH,
|
|
|
|
|
'brand_banner': branding.brand_type == BRANDING_ORG_BANNER,
|
|
|
|
|
'brand_colour': branding.colour,
|
2018-08-28 14:25:16 +01:00
|
|
|
'brand_logo': logo_url,
|
2021-02-16 14:24:40 +00:00
|
|
|
'brand_text': branding.text,
|
|
|
|
|
'brand_name': branding.name,
|
2018-08-28 14:25:16 +01:00
|
|
|
}
|
2017-02-03 14:27:04 +00:00
|
|
|
|
|
|
|
|
|
2017-02-06 16:20:44 +00:00
|
|
|
def technical_failure(notification):
|
|
|
|
|
notification.status = NOTIFICATION_TECHNICAL_FAILURE
|
|
|
|
|
dao_update_notification(notification)
|
2018-03-16 17:18:44 +00:00
|
|
|
raise NotificationTechnicalFailureException(
|
2017-02-06 16:20:44 +00:00
|
|
|
"Send {} for notification id {} to provider is not allowed: service {} is inactive".format(
|
|
|
|
|
notification.notification_type,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.service_id))
|