2016-04-05 14:51:55 +01:00
|
|
|
|
import itertools
|
2016-03-31 15:57:50 +01:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
|
|
2016-02-17 17:48:23 +00:00
|
|
|
|
from app.clients.email.aws_ses import AwsSesClientException
|
2016-02-17 13:59:01 +00:00
|
|
|
|
from app.clients.sms.firetext import FiretextClientException
|
2016-04-06 15:56:34 +01:00
|
|
|
|
from app.clients.sms.mmg import MMGClientException
|
2016-03-01 08:48:27 +00:00
|
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2016-02-24 11:51:02 +00:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
|
|
from utils.template import Template
|
|
|
|
|
|
|
|
|
|
|
|
from utils.recipients import (
|
|
|
|
|
|
RecipientCSV,
|
2016-04-05 14:51:55 +01:00
|
|
|
|
validate_and_format_phone_number,
|
|
|
|
|
|
allowed_to_send_to
|
2016-03-31 15:57:50 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from app import (
|
|
|
|
|
|
create_uuid,
|
|
|
|
|
|
DATETIME_FORMAT,
|
|
|
|
|
|
DATE_FORMAT,
|
|
|
|
|
|
notify_celery,
|
|
|
|
|
|
encryption,
|
|
|
|
|
|
firetext_client,
|
2016-04-01 16:42:31 +01:00
|
|
|
|
aws_ses_client,
|
|
|
|
|
|
mmg_client
|
2016-03-31 15:57:50 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from app.aws import s3
|
|
|
|
|
|
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
|
|
|
|
|
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
|
|
|
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
|
from app.dao.notifications_dao import (
|
|
|
|
|
|
dao_create_notification,
|
|
|
|
|
|
dao_update_notification,
|
2016-04-05 11:07:21 +01:00
|
|
|
|
delete_notifications_created_more_than_a_week_ago,
|
2016-03-11 09:40:35 +00:00
|
|
|
|
dao_get_notification_statistics_for_service_and_day,
|
|
|
|
|
|
update_notification_reference_by_id
|
2016-03-09 17:46:01 +00:00
|
|
|
|
)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
|
dao_update_job,
|
|
|
|
|
|
dao_get_job_by_id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
|
from app.models import (
|
|
|
|
|
|
Notification,
|
|
|
|
|
|
TEMPLATE_TYPE_EMAIL,
|
|
|
|
|
|
TEMPLATE_TYPE_SMS
|
|
|
|
|
|
)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
2016-03-09 14:41:36 +00:00
|
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
|
@notify_celery.task(name="delete-verify-codes")
|
|
|
|
|
|
def delete_verify_codes():
|
|
|
|
|
|
try:
|
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
|
deleted = delete_codes_older_created_more_than_a_day_ago()
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Delete job started {} finished {} deleted {} verify codes".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
|
)
|
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
|
current_app.logger.info("Failed to delete verify codes")
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-successful-notifications")
|
|
|
|
|
|
def delete_successful_notifications():
|
|
|
|
|
|
try:
|
|
|
|
|
|
start = datetime.utcnow()
|
2016-04-08 16:13:10 +01:00
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('sending')
|
2016-03-09 17:46:01 +00:00
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Delete job started {} finished {} deleted {} successful notifications".format(
|
|
|
|
|
|
start,
|
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
|
deleted
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
|
current_app.logger.info("Failed to delete successful notifications")
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-failed-notifications")
|
|
|
|
|
|
def delete_failed_notifications():
|
|
|
|
|
|
try:
|
|
|
|
|
|
start = datetime.utcnow()
|
2016-04-05 11:07:21 +01:00
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('failed')
|
2016-03-09 17:46:01 +00:00
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Delete job started {} finished {} deleted {} failed notifications".format(
|
|
|
|
|
|
start,
|
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
|
deleted
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
|
current_app.logger.info("Failed to delete failed notifications")
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-invitations")
|
|
|
|
|
|
def delete_invitations():
|
|
|
|
|
|
try:
|
|
|
|
|
|
start = datetime.utcnow()
|
2016-03-10 09:34:27 +00:00
|
|
|
|
deleted = delete_invitations_created_more_than_two_days_ago()
|
2016-03-09 17:46:01 +00:00
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
|
)
|
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
|
current_app.logger.info("Failed to delete invitations")
|
|
|
|
|
|
raise
|
2016-02-24 17:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="process-job")
|
|
|
|
|
|
def process_job(job_id):
|
2016-02-25 11:23:04 +00:00
|
|
|
|
start = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
|
job = dao_get_job_by_id(job_id)
|
2016-03-09 11:28:52 +00:00
|
|
|
|
|
|
|
|
|
|
service = job.service
|
|
|
|
|
|
|
|
|
|
|
|
stats = dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
|
service_id=service.id,
|
|
|
|
|
|
day=job.created_at.strftime(DATE_FORMAT)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-03-09 13:57:53 +00:00
|
|
|
|
total_sent = 0
|
2016-03-09 11:28:52 +00:00
|
|
|
|
if stats:
|
|
|
|
|
|
total_sent = stats.emails_requested + stats.sms_requested
|
|
|
|
|
|
|
2016-04-08 16:13:10 +01:00
|
|
|
|
if total_sent + job.notification_count > service.message_limit:
|
2016-03-09 13:57:53 +00:00
|
|
|
|
job.status = 'sending limits exceeded'
|
|
|
|
|
|
job.processing_finished = datetime.utcnow()
|
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
current_app.logger.info(
|
2016-04-08 16:13:10 +01:00
|
|
|
|
"Job {} size {} error. Sending limits {} exceeded".format(
|
|
|
|
|
|
job_id, job.notification_count, service.message_limit)
|
2016-03-09 13:57:53 +00:00
|
|
|
|
)
|
|
|
|
|
|
return
|
2016-03-09 11:28:52 +00:00
|
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
|
job.status = 'in progress'
|
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
|
template = Template(
|
|
|
|
|
|
dao_get_template_by_id(job.template_id).__dict__
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-03-06 12:51:45 +00:00
|
|
|
|
for recipient, personalisation in RecipientCSV(
|
2016-04-07 13:44:04 +01:00
|
|
|
|
s3.get_job_from_s3(str(service.id), str(job_id)),
|
2016-03-09 14:41:36 +00:00
|
|
|
|
template_type=template.template_type,
|
|
|
|
|
|
placeholders=template.placeholders
|
2016-03-06 12:51:45 +00:00
|
|
|
|
).recipients_and_personalisation:
|
2016-02-29 14:43:44 +00:00
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
|
encrypted = encryption.encrypt({
|
2016-04-08 16:13:10 +01:00
|
|
|
|
'template': str(template.id),
|
2016-02-24 17:12:30 +00:00
|
|
|
|
'job': str(job.id),
|
2016-03-06 12:51:45 +00:00
|
|
|
|
'to': recipient,
|
|
|
|
|
|
'personalisation': personalisation
|
2016-02-24 17:12:30 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
|
if template.template_type == 'sms':
|
2016-02-25 09:59:50 +00:00
|
|
|
|
send_sms.apply_async((
|
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
|
str(create_uuid()),
|
2016-02-25 11:23:04 +00:00
|
|
|
|
encrypted,
|
2016-03-08 17:45:37 +00:00
|
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
2016-02-25 09:59:50 +00:00
|
|
|
|
queue='bulk-sms'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
|
if template.template_type == 'email':
|
2016-02-25 09:59:50 +00:00
|
|
|
|
send_email.apply_async((
|
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
|
str(create_uuid()),
|
2016-03-09 07:27:26 +00:00
|
|
|
|
template.subject,
|
2016-02-25 09:59:50 +00:00
|
|
|
|
"{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
2016-02-25 11:23:04 +00:00
|
|
|
|
encrypted,
|
2016-03-08 17:45:37 +00:00
|
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
2016-02-25 09:59:50 +00:00
|
|
|
|
queue='bulk-email')
|
2016-02-24 17:12:30 +00:00
|
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
|
finished = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
|
job.status = 'finished'
|
2016-02-25 11:23:04 +00:00
|
|
|
|
job.processing_started = start
|
|
|
|
|
|
job.processing_finished = finished
|
2016-02-24 17:12:30 +00:00
|
|
|
|
dao_update_job(job)
|
2016-04-05 14:28:19 +01:00
|
|
|
|
remove_job.apply_async((str(job_id),), queue='remove-job')
|
2016-02-25 11:23:04 +00:00
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Job {} created at {} started at {} finished at {}".format(job_id, job.created_at, start, finished)
|
|
|
|
|
|
)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:28:19 +01:00
|
|
|
|
@notify_celery.task(name="remove-job")
|
|
|
|
|
|
def remove_job(job_id):
|
|
|
|
|
|
job = dao_get_job_by_id(job_id)
|
2016-04-07 13:44:04 +01:00
|
|
|
|
s3.remove_job_from_s3(job.service.id, str(job_id))
|
2016-04-05 14:28:19 +01:00
|
|
|
|
current_app.logger.info("Job {} has been removed from s3.".format(job_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
|
@notify_celery.task(name="send-sms")
|
2016-02-25 09:59:50 +00:00
|
|
|
|
def send_sms(service_id, notification_id, encrypted_notification, created_at):
|
2016-02-16 15:28:30 +00:00
|
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-03-01 08:48:27 +00:00
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-04-06 15:56:34 +01:00
|
|
|
|
client = mmg_client
|
2016-02-25 11:23:04 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
restricted = False
|
2016-03-03 12:05:18 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
if not service_allowed_to_send_to(notification['to'], service):
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"SMS {} failed as restricted service".format(notification_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
restricted = True
|
2016-03-03 12:05:18 +00:00
|
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
|
sent_at = datetime.utcnow()
|
2016-02-16 17:42:04 +00:00
|
|
|
|
notification_db_object = Notification(
|
|
|
|
|
|
id=notification_id,
|
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
|
to=notification['to'],
|
|
|
|
|
|
service_id=service_id,
|
2016-02-23 17:39:08 +00:00
|
|
|
|
job_id=notification.get('job', None),
|
2016-04-08 16:13:10 +01:00
|
|
|
|
status='failed' if restricted else 'sending',
|
2016-03-08 17:45:37 +00:00
|
|
|
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
2016-02-25 11:23:04 +00:00
|
|
|
|
sent_at=sent_at,
|
|
|
|
|
|
sent_by=client.get_name()
|
2016-02-16 17:42:04 +00:00
|
|
|
|
)
|
2016-03-03 12:05:18 +00:00
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
|
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
if restricted:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2016-04-04 15:02:25 +01:00
|
|
|
|
try:
|
|
|
|
|
|
template = Template(
|
|
|
|
|
|
dao_get_template_by_id(notification['template']).__dict__,
|
|
|
|
|
|
values=notification.get('personalisation', {}),
|
|
|
|
|
|
prefix=service.name
|
2016-02-29 14:43:44 +00:00
|
|
|
|
)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
|
|
|
|
|
client.send_sms(
|
|
|
|
|
|
to=validate_and_format_phone_number(notification['to']),
|
|
|
|
|
|
content=template.replaced,
|
|
|
|
|
|
reference=str(notification_id)
|
|
|
|
|
|
)
|
2016-04-06 15:56:34 +01:00
|
|
|
|
except MMGClientException as e:
|
2016-04-04 15:02:25 +01:00
|
|
|
|
current_app.logger.error(
|
|
|
|
|
|
"SMS notification {} failed".format(notification_id)
|
2016-02-29 14:43:44 +00:00
|
|
|
|
)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
|
notification_db_object.status = 'failed'
|
|
|
|
|
|
dao_update_notification(notification_db_object)
|
|
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"SMS {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="send-email")
|
2016-02-25 09:59:50 +00:00
|
|
|
|
def send_email(service_id, notification_id, subject, from_address, encrypted_notification, created_at):
|
2016-02-22 17:17:29 +00:00
|
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-02-25 11:23:04 +00:00
|
|
|
|
client = aws_ses_client
|
2016-04-05 14:51:55 +01:00
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-02-25 11:23:04 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
restricted = False
|
2016-03-03 12:05:18 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
if not service_allowed_to_send_to(notification['to'], service):
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Email {} failed as restricted service".format(notification_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
restricted = True
|
2016-03-03 12:05:18 +00:00
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
|
sent_at = datetime.utcnow()
|
2016-02-22 17:17:29 +00:00
|
|
|
|
notification_db_object = Notification(
|
|
|
|
|
|
id=notification_id,
|
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
|
to=notification['to'],
|
|
|
|
|
|
service_id=service_id,
|
2016-02-24 09:55:05 +00:00
|
|
|
|
job_id=notification.get('job', None),
|
2016-04-08 16:13:10 +01:00
|
|
|
|
status='failed' if restricted else 'sending',
|
2016-03-08 17:45:37 +00:00
|
|
|
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
2016-02-25 11:23:04 +00:00
|
|
|
|
sent_at=sent_at,
|
|
|
|
|
|
sent_by=client.get_name()
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2016-03-08 17:45:37 +00:00
|
|
|
|
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
|
if restricted:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2016-04-04 15:02:25 +01:00
|
|
|
|
try:
|
|
|
|
|
|
template = Template(
|
|
|
|
|
|
dao_get_template_by_id(notification['template']).__dict__,
|
|
|
|
|
|
values=notification.get('personalisation', {})
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
|
|
|
|
|
reference = client.send_email(
|
|
|
|
|
|
from_address,
|
|
|
|
|
|
notification['to'],
|
|
|
|
|
|
subject,
|
|
|
|
|
|
body=template.replaced,
|
|
|
|
|
|
html_body=template.as_HTML_email,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
update_notification_reference_by_id(notification_id, reference)
|
|
|
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
|
notification_db_object.status = 'failed'
|
|
|
|
|
|
dao_update_notification(notification_db_object)
|
|
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
|
"Email {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
|
)
|
2016-02-16 17:42:04 +00:00
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
|
current_app.logger.debug(e)
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-sms-code')
|
2016-02-18 09:59:18 +00:00
|
|
|
|
def send_sms_code(encrypted_verification):
|
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification)
|
2016-02-17 15:41:33 +00:00
|
|
|
|
try:
|
2016-04-01 16:42:31 +01:00
|
|
|
|
mmg_client.send_sms(validate_and_format_phone_number(verification_message['to']),
|
|
|
|
|
|
verification_message['secret_code'],
|
|
|
|
|
|
'send-sms-code')
|
2016-04-06 17:35:14 +01:00
|
|
|
|
except MMGClientException as e:
|
2016-04-01 16:42:31 +01:00
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_sms_via_firetext(to, content, reference):
|
|
|
|
|
|
try:
|
|
|
|
|
|
firetext_client.send_sms(to=to, content=content, reference=reference)
|
2016-02-17 15:52:09 +00:00
|
|
|
|
except FiretextClientException as e:
|
2016-03-08 15:47:35 +00:00
|
|
|
|
current_app.logger.exception(e)
|
2016-02-17 17:48:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-email-code')
|
2016-02-18 09:52:27 +00:00
|
|
|
|
def send_email_code(encrypted_verification_message):
|
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification_message)
|
2016-02-17 17:48:23 +00:00
|
|
|
|
try:
|
2016-02-22 13:02:09 +00:00
|
|
|
|
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
|
|
|
|
|
verification_message['to'],
|
|
|
|
|
|
"Verification code",
|
|
|
|
|
|
verification_message['secret_code'])
|
2016-02-17 17:48:23 +00:00
|
|
|
|
except AwsSesClientException as e:
|
2016-03-08 15:47:35 +00:00
|
|
|
|
current_app.logger.exception(e)
|
2016-02-29 13:21:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: when placeholders in templates work, this will be a real template
|
|
|
|
|
|
def invitation_template(user_name, service_name, url, expiry_date):
|
|
|
|
|
|
from string import Template
|
2016-02-29 15:56:00 +00:00
|
|
|
|
t = Template(
|
|
|
|
|
|
'$user_name has invited you to collaborate on $service_name on GOV.UK Notify.\n\n'
|
2016-02-29 16:12:12 +00:00
|
|
|
|
'GOV.UK Notify makes it easy to keep people updated by helping you send text messages, emails and letters.\n\n'
|
2016-02-29 15:56:00 +00:00
|
|
|
|
'Click this link to create an account on GOV.UK Notify:\n$url\n\n'
|
|
|
|
|
|
'This invitation will stop working at midnight tomorrow. This is to keep $service_name secure.')
|
2016-02-29 13:21:12 +00:00
|
|
|
|
return t.substitute(user_name=user_name, service_name=service_name, url=url, expiry_date=expiry_date)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 15:56:00 +00:00
|
|
|
|
def invitation_subject_line(user_name, service_name):
|
|
|
|
|
|
from string import Template
|
|
|
|
|
|
t = Template('$user_name has invited you to collaborate on $service_name on GOV.UK Notify')
|
|
|
|
|
|
return t.substitute(user_name=user_name, service_name=service_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 13:21:12 +00:00
|
|
|
|
def invited_user_url(base_url, token):
|
|
|
|
|
|
return '{0}/invitation/{1}'.format(base_url, token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='email-invited-user')
|
|
|
|
|
|
def email_invited_user(encrypted_invitation):
|
|
|
|
|
|
invitation = encryption.decrypt(encrypted_invitation)
|
|
|
|
|
|
url = invited_user_url(current_app.config['ADMIN_BASE_URL'],
|
|
|
|
|
|
invitation['token'])
|
|
|
|
|
|
invitation_content = invitation_template(invitation['user_name'],
|
|
|
|
|
|
invitation['service_name'],
|
|
|
|
|
|
url,
|
|
|
|
|
|
invitation['expiry_date'])
|
|
|
|
|
|
try:
|
2016-02-29 15:56:00 +00:00
|
|
|
|
email_from = "{}@{}".format(current_app.config['INVITATION_EMAIL_FROM'],
|
|
|
|
|
|
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
2016-02-29 16:12:12 +00:00
|
|
|
|
subject_line = invitation_subject_line(invitation['user_name'], invitation['service_name'])
|
2016-02-29 15:56:00 +00:00
|
|
|
|
aws_ses_client.send_email(email_from,
|
2016-02-29 13:21:12 +00:00
|
|
|
|
invitation['to'],
|
2016-02-29 16:12:12 +00:00
|
|
|
|
subject_line,
|
2016-02-29 13:21:12 +00:00
|
|
|
|
invitation_content)
|
|
|
|
|
|
except AwsSesClientException as e:
|
2016-03-08 15:47:35 +00:00
|
|
|
|
current_app.logger.exception(e)
|
2016-03-07 14:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 14:33:06 +00:00
|
|
|
|
def password_reset_message(name, url):
|
|
|
|
|
|
from string import Template
|
|
|
|
|
|
t = Template("Hi $user_name,\n\n"
|
|
|
|
|
|
"We received a request to reset your password on GOV.UK Notify.\n\n"
|
|
|
|
|
|
"If you didn't request this email, you can ignore it – your password has not been changed.\n\n"
|
|
|
|
|
|
"To reset your password, click this link:\n\n"
|
|
|
|
|
|
"$url")
|
|
|
|
|
|
return t.substitute(user_name=name, url=url)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-07 18:20:20 +00:00
|
|
|
|
@notify_celery.task(name='email-reset-password')
|
2016-03-07 14:34:53 +00:00
|
|
|
|
def email_reset_password(encrypted_reset_password_message):
|
2016-03-07 18:20:20 +00:00
|
|
|
|
reset_password_message = encryption.decrypt(encrypted_reset_password_message)
|
2016-03-07 14:34:53 +00:00
|
|
|
|
try:
|
|
|
|
|
|
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
|
|
|
|
|
reset_password_message['to'],
|
2016-03-08 14:33:06 +00:00
|
|
|
|
"Reset your GOV.UK Notify password",
|
|
|
|
|
|
password_reset_message(name=reset_password_message['name'],
|
|
|
|
|
|
url=reset_password_message['reset_password_url']))
|
2016-03-07 14:34:53 +00:00
|
|
|
|
except AwsSesClientException as e:
|
2016-03-08 15:47:35 +00:00
|
|
|
|
current_app.logger.exception(e)
|
2016-03-17 13:06:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def registration_verification_template(name, url):
|
|
|
|
|
|
from string import Template
|
|
|
|
|
|
t = Template("Hi $name,\n\n"
|
|
|
|
|
|
"To complete your registration for GOV.UK Notify please click the link below\n\n $url")
|
|
|
|
|
|
return t.substitute(name=name, url=url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='email-registration-verification')
|
|
|
|
|
|
def email_registration_verification(encrypted_verification_message):
|
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification_message)
|
|
|
|
|
|
try:
|
|
|
|
|
|
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
|
|
|
|
|
verification_message['to'],
|
|
|
|
|
|
"Confirm GOV.UK Notify registration",
|
|
|
|
|
|
registration_verification_template(name=verification_message['name'],
|
|
|
|
|
|
url=verification_message['url']))
|
|
|
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
|
current_app.logger.exception(e)
|
2016-04-05 14:51:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def service_allowed_to_send_to(recipient, service):
|
|
|
|
|
|
|
|
|
|
|
|
if not service.restricted:
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
return allowed_to_send_to(
|
|
|
|
|
|
recipient,
|
|
|
|
|
|
itertools.chain.from_iterable(
|
|
|
|
|
|
[user.mobile_number, user.email_address] for user in service.users
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|