2016-12-21 16:15:18 +00:00
|
|
|
from urllib import parse
|
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
|
2017-10-06 17:08:06 +01:00
|
|
|
from app.dao.notifications_dao import (
|
|
|
|
|
dao_update_notification,
|
|
|
|
|
dao_get_notification_email_reply_for_notification
|
|
|
|
|
)
|
2017-01-20 16:14:29 +00:00
|
|
|
from app.dao.provider_details_dao import (
|
|
|
|
|
get_provider_details_by_notification_type,
|
|
|
|
|
dao_toggle_sms_provider
|
|
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2017-09-14 16:26:46 +01:00
|
|
|
from app.models import (
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
BRANDING_ORG,
|
2017-09-19 13:18:59 +01:00
|
|
|
BRANDING_ORG_BANNER,
|
2017-09-14 16:26:46 +01:00
|
|
|
BRANDING_GOVUK,
|
|
|
|
|
EMAIL_TYPE,
|
|
|
|
|
NOTIFICATION_TECHNICAL_FAILURE,
|
|
|
|
|
NOTIFICATION_SENT,
|
|
|
|
|
NOTIFICATION_SENDING
|
|
|
|
|
)
|
2017-07-18 18:21:35 +01:00
|
|
|
from app.celery.statistics_tasks import create_initial_notification_statistic_tasks
|
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):
|
2017-01-31 13:53:13 +00:00
|
|
|
service = notification.service
|
2017-06-28 12:14:36 +01: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':
|
2017-04-27 10:18:28 +01:00
|
|
|
provider = provider_to_use(SMS_TYPE, notification.id, notification.international)
|
2017-01-31 13:53:13 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Starting sending SMS {} to provider at {}".format(notification.id, datetime.utcnow())
|
|
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
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,
|
2017-05-22 15:58:19 +01:00
|
|
|
sender=service.sms_sender not in {None, current_app.config['FROM_NUMBER']}
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
2017-01-24 14:24:58 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
|
|
|
|
notification.billable_units = 0
|
2017-01-24 14:24:58 +00:00
|
|
|
send_sms_response(provider.get_name(), str(notification.id), notification.to)
|
2017-10-16 13:24:06 +01:00
|
|
|
update_notification(notification, provider)
|
2016-09-20 17:24:28 +01:00
|
|
|
else:
|
2017-01-20 16:14:29 +00:00
|
|
|
try:
|
|
|
|
|
provider.send_sms(
|
2017-04-27 16:57:04 +01:00
|
|
|
to=validate_and_format_phone_number(notification.to, international=notification.international),
|
2017-01-20 16:14:29 +00:00
|
|
|
content=str(template),
|
|
|
|
|
reference=str(notification.id),
|
2017-08-14 19:47:09 +01:00
|
|
|
sender=service.get_inbound_number()
|
2017-01-20 16:14:29 +00:00
|
|
|
)
|
|
|
|
|
except Exception as e:
|
2017-01-23 13:36:04 +00:00
|
|
|
dao_toggle_sms_provider(provider.name)
|
2017-01-20 16:14:29 +00:00
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
notification.billable_units = template.fragment_count
|
2017-04-27 13:22:17 +01:00
|
|
|
update_notification(notification, provider, notification.international)
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2017-05-09 18:17:55 +01:00
|
|
|
create_initial_notification_statistic_tasks(notification)
|
|
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.info(
|
2016-12-20 13:24:08 +00:00
|
|
|
"SMS {} sent to provider {} at {}".format(notification.id, provider.get_name(), 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):
|
2017-01-31 13:53:13 +00:00
|
|
|
service = notification.service
|
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':
|
2017-01-31 13:53:13 +00:00
|
|
|
provider = provider_to_use(EMAIL_TYPE, notification.id)
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Starting sending EMAIL {} to provider at {}".format(notification.id, datetime.utcnow())
|
|
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
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())
|
2016-09-23 09:54:53 +01:00
|
|
|
notification.billable_units = 0
|
2017-01-24 14:24:58 +00:00
|
|
|
notification.reference = reference
|
|
|
|
|
update_notification(notification, provider)
|
|
|
|
|
send_email_response(provider.get_name(), 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'])
|
2017-09-20 10:45:35 +01:00
|
|
|
|
2017-10-06 16:58:32 +01:00
|
|
|
email_reply_to = dao_get_notification_email_reply_for_notification(notification.id)
|
|
|
|
|
|
|
|
|
|
if not email_reply_to:
|
|
|
|
|
email_reply_to = service.get_default_reply_to_email_address()
|
|
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
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),
|
2017-10-06 16:58:32 +01:00
|
|
|
reply_to_address=email_reply_to,
|
2016-09-20 17:24:28 +01:00
|
|
|
)
|
2017-01-24 14:24:58 +00:00
|
|
|
notification.reference = reference
|
|
|
|
|
update_notification(notification, provider)
|
2016-09-20 17:24:28 +01:00
|
|
|
|
2017-05-09 18:17:55 +01:00
|
|
|
create_initial_notification_statistic_tasks(notification)
|
|
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 13:22:17 +01:00
|
|
|
def update_notification(notification, provider, international=False):
|
2017-01-24 14:24:58 +00:00
|
|
|
notification.sent_at = datetime.utcnow()
|
|
|
|
|
notification.sent_by = provider.get_name()
|
2017-04-27 13:22:17 +01:00
|
|
|
if international:
|
|
|
|
|
notification.status = NOTIFICATION_SENT
|
|
|
|
|
else:
|
|
|
|
|
notification.status = NOTIFICATION_SENDING
|
2017-01-24 14:24:58 +00:00
|
|
|
dao_update_notification(notification)
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 10:18:28 +01:00
|
|
|
def provider_to_use(notification_type, notification_id, international=False):
|
2017-04-27 12:14:22 +01:00
|
|
|
active_providers_in_order = [
|
|
|
|
|
p for p in get_provider_details_by_notification_type(notification_type, international) if p.active
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if not active_providers_in_order:
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
"{} {} failed as no active providers".format(notification_type, notification_id)
|
|
|
|
|
)
|
|
|
|
|
raise Exception("No active {} providers".format(notification_type))
|
|
|
|
|
|
2017-04-27 12:14:22 +01:00
|
|
|
return clients.get_client_by_name_and_type(active_providers_in_order[0].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):
|
2017-09-19 13:18:59 +01:00
|
|
|
govuk_banner = service.branding not in (BRANDING_ORG, BRANDING_ORG_BANNER)
|
|
|
|
|
brand_banner = service.branding == BRANDING_ORG_BANNER
|
2017-09-14 16:26:46 +01:00
|
|
|
if service.organisation and service.branding != BRANDING_GOVUK:
|
2017-09-21 13:37:57 +01:00
|
|
|
|
2016-12-21 16:15:18 +00:00
|
|
|
logo_url = get_logo_url(
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.config['ADMIN_BASE_URL'],
|
|
|
|
|
service.organisation.logo
|
2017-09-21 13:37:57 +01:00
|
|
|
) if service.organisation.logo else None
|
2016-12-21 16:15:18 +00:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
branding = {
|
|
|
|
|
'brand_colour': service.organisation.colour,
|
2016-12-21 16:15:18 +00:00
|
|
|
'brand_logo': logo_url,
|
2016-09-20 17:24:28 +01:00
|
|
|
'brand_name': service.organisation.name,
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
branding = {}
|
|
|
|
|
|
2017-09-19 13:18:59 +01:00
|
|
|
return dict(govuk_banner=govuk_banner, brand_banner=brand_banner, **branding)
|
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)
|
|
|
|
|
current_app.logger.warn(
|
|
|
|
|
"Send {} for notification id {} to provider is not allowed: service {} is inactive".format(
|
|
|
|
|
notification.notification_type,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.service_id))
|