2016-05-24 13:08:38 +01:00
|
|
|
from datetime import datetime
|
2016-04-06 14:58:13 +01:00
|
|
|
import itertools
|
2016-01-19 11:23:09 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
2016-02-22 17:17:29 +00:00
|
|
|
request,
|
2016-03-01 13:30:10 +00:00
|
|
|
current_app,
|
2016-03-10 17:29:17 +00:00
|
|
|
json
|
2016-02-16 11:22:44 +00:00
|
|
|
)
|
2016-04-13 15:31:08 +01:00
|
|
|
from notifications_utils.recipients import allowed_to_send_to, first_column_heading
|
|
|
|
|
from notifications_utils.template import Template
|
2016-07-07 15:59:50 +01:00
|
|
|
from notifications_utils.renderers import PassThrough
|
2016-04-06 16:34:45 +01:00
|
|
|
from app.clients.email.aws_ses import get_aws_responses
|
2016-05-13 17:15:39 +01:00
|
|
|
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT, statsd_client
|
2016-06-30 13:21:35 +01:00
|
|
|
from app.models import KEY_TYPE_TEAM
|
2016-02-22 17:17:29 +00:00
|
|
|
from app.dao import (
|
|
|
|
|
templates_dao,
|
|
|
|
|
services_dao,
|
2016-02-25 11:35:32 +00:00
|
|
|
notifications_dao
|
2016-02-22 17:17:29 +00:00
|
|
|
)
|
2016-07-26 14:33:14 +01:00
|
|
|
from app.models import SMS_TYPE
|
2016-04-06 14:31:33 +01:00
|
|
|
from app.notifications.process_client_response import (
|
|
|
|
|
validate_callback_data,
|
|
|
|
|
process_sms_client_response
|
|
|
|
|
)
|
2016-02-03 13:16:19 +00:00
|
|
|
from app.schemas import (
|
2016-02-16 11:22:44 +00:00
|
|
|
email_notification_schema,
|
|
|
|
|
sms_template_notification_schema,
|
2016-07-26 14:33:14 +01:00
|
|
|
notification_with_personalisation_schema,
|
2016-05-19 16:42:21 +01:00
|
|
|
notifications_filter_schema,
|
2016-05-20 15:16:53 +01:00
|
|
|
notifications_statistics_schema,
|
|
|
|
|
day_schema,
|
2016-05-24 13:08:38 +01:00
|
|
|
unarchived_template_schema
|
2016-02-16 11:22:44 +00:00
|
|
|
)
|
2016-06-13 16:16:56 +01:00
|
|
|
from app.celery.tasks import send_sms, send_email
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.utils import pagination_links
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
notifications = Blueprint('notifications', __name__)
|
|
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
register_errors(notifications)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
2016-03-10 17:29:17 +00:00
|
|
|
@notifications.route('/notifications/email/ses', methods=['POST'])
|
|
|
|
|
def process_ses_response():
|
2016-04-06 16:34:45 +01:00
|
|
|
client_name = 'SES'
|
2016-03-10 17:29:17 +00:00
|
|
|
try:
|
|
|
|
|
ses_request = json.loads(request.data)
|
2016-04-06 16:34:45 +01:00
|
|
|
errors = validate_callback_data(data=ses_request, fields=['Message'], client_name=client_name)
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-03-14 14:49:02 +00:00
|
|
|
ses_message = json.loads(ses_request['Message'])
|
2016-04-06 16:34:45 +01:00
|
|
|
errors = validate_callback_data(data=ses_message, fields=['notificationType'], client_name=client_name)
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
notification_type = ses_message['notificationType']
|
2016-05-17 15:38:49 +01:00
|
|
|
if notification_type == 'Bounce':
|
|
|
|
|
if ses_message['bounce']['bounceType'] == 'Permanent':
|
|
|
|
|
notification_type = ses_message['bounce']['bounceType'] # permanent or not
|
|
|
|
|
else:
|
|
|
|
|
notification_type = 'Temporary'
|
2016-03-21 13:24:37 +00:00
|
|
|
try:
|
2016-04-06 16:34:45 +01:00
|
|
|
aws_response_dict = get_aws_responses(notification_type)
|
2016-03-21 13:24:37 +00:00
|
|
|
except KeyError:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = "{} callback failed: status {} not found".format(client_name, notification_type)
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
notification_status = aws_response_dict['notification_status']
|
|
|
|
|
notification_statistics_status = aws_response_dict['notification_statistics_status']
|
2016-03-21 13:24:37 +00:00
|
|
|
|
2016-03-10 17:29:17 +00:00
|
|
|
try:
|
2016-03-14 14:49:02 +00:00
|
|
|
source = ses_message['mail']['source']
|
|
|
|
|
if is_not_a_notification(source):
|
2016-03-11 10:19:40 +00:00
|
|
|
current_app.logger.info(
|
2016-03-21 13:24:37 +00:00
|
|
|
"SES callback for notify success:. source {} status {}".format(source, notification_status)
|
2016-03-11 10:19:40 +00:00
|
|
|
)
|
|
|
|
|
return jsonify(
|
|
|
|
|
result="success", message="SES callback succeeded"
|
|
|
|
|
), 200
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-03-14 14:49:02 +00:00
|
|
|
reference = ses_message['mail']['messageId']
|
2016-05-31 10:17:15 +01:00
|
|
|
if not notifications_dao.update_notification_status_by_reference(
|
2016-03-21 13:24:37 +00:00
|
|
|
reference,
|
|
|
|
|
notification_status,
|
|
|
|
|
notification_statistics_status
|
2016-05-31 10:17:15 +01:00
|
|
|
):
|
2016-06-14 15:07:23 +01:00
|
|
|
error = "SES callback failed: notification either not found or already updated " \
|
|
|
|
|
"from sending. Status {}".format(notification_status)
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
if not aws_response_dict['success']:
|
2016-03-21 13:24:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"SES delivery failed: notification {} has error found. Status {}".format(
|
|
|
|
|
reference,
|
2016-04-06 16:34:45 +01:00
|
|
|
aws_response_dict['message']
|
2016-03-21 13:24:37 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2016-08-08 11:23:58 +01:00
|
|
|
statsd_client.incr('callback.ses.{}'.format(notification_statistics_status))
|
2016-03-10 17:29:17 +00:00
|
|
|
return jsonify(
|
|
|
|
|
result="success", message="SES callback succeeded"
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
except KeyError:
|
2016-06-14 15:07:23 +01:00
|
|
|
message = "SES callback failed: messageId missing"
|
|
|
|
|
raise InvalidRequest(message, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-03-11 09:06:22 +00:00
|
|
|
except ValueError as ex:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = "{} callback failed: invalid json".format(client_name)
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 10:19:40 +00:00
|
|
|
def is_not_a_notification(source):
|
|
|
|
|
invite_email = "{}@{}".format(
|
|
|
|
|
current_app.config['INVITATION_EMAIL_FROM'],
|
|
|
|
|
current_app.config['NOTIFY_EMAIL_DOMAIN']
|
|
|
|
|
)
|
|
|
|
|
if current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'] == source:
|
|
|
|
|
return True
|
|
|
|
|
if invite_email == source:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2016-04-04 15:02:21 +01:00
|
|
|
@notifications.route('/notifications/sms/mmg', methods=['POST'])
|
|
|
|
|
def process_mmg_response():
|
2016-04-06 14:31:33 +01:00
|
|
|
client_name = 'MMG'
|
|
|
|
|
data = json.loads(request.data)
|
2016-06-14 15:07:23 +01:00
|
|
|
errors = validate_callback_data(data=data,
|
|
|
|
|
fields=['status', 'CID'],
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
2016-04-20 09:45:13 +01:00
|
|
|
success, errors = process_sms_client_response(status=str(data.get('status')),
|
2016-04-06 14:31:33 +01:00
|
|
|
reference=data.get('CID'),
|
2016-04-21 11:37:38 +01:00
|
|
|
client_name=client_name)
|
2016-04-06 14:31:33 +01:00
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-04 18:08:37 +01:00
|
|
|
else:
|
2016-04-06 14:31:33 +01:00
|
|
|
return jsonify(result='success', message=success), 200
|
2016-04-04 18:08:37 +01:00
|
|
|
|
2016-03-10 15:40:41 +00:00
|
|
|
|
2016-04-04 18:08:37 +01:00
|
|
|
@notifications.route('/notifications/sms/firetext', methods=['POST'])
|
|
|
|
|
def process_firetext_response():
|
2016-04-06 14:31:33 +01:00
|
|
|
client_name = 'Firetext'
|
2016-06-14 15:07:23 +01:00
|
|
|
errors = validate_callback_data(data=request.form,
|
|
|
|
|
fields=['status', 'reference'],
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
2016-06-09 10:25:27 +01:00
|
|
|
response_code = request.form.get('code')
|
|
|
|
|
status = request.form.get('status')
|
|
|
|
|
current_app.logger.info('Firetext status: {}, extended error code: {}'.format(status, response_code))
|
2016-06-02 15:53:04 +01:00
|
|
|
|
2016-06-09 10:25:27 +01:00
|
|
|
success, errors = process_sms_client_response(status=status,
|
2016-04-06 14:31:33 +01:00
|
|
|
reference=request.form.get('reference'),
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
else:
|
|
|
|
|
return jsonify(result='success', message=success), 200
|
2016-03-10 15:40:41 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
2016-01-19 13:45:57 +00:00
|
|
|
def get_notifications(notification_id):
|
2016-06-30 18:43:15 +01:00
|
|
|
notification = notifications_dao.get_notification(str(api_user.service_id),
|
|
|
|
|
notification_id,
|
|
|
|
|
key_type=api_user.key_type)
|
2016-07-26 14:33:14 +01:00
|
|
|
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
@notifications.route('/notifications', methods=['GET'])
|
|
|
|
|
def get_all_notifications():
|
2016-06-14 15:07:23 +01:00
|
|
|
data = notifications_filter_schema.load(request.args).data
|
2016-03-21 12:37:34 +00:00
|
|
|
page = data['page'] if 'page' in data else 1
|
2016-04-19 10:52:52 +01:00
|
|
|
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
2016-04-28 16:10:35 +01:00
|
|
|
limit_days = data.get('limit_days')
|
2016-03-01 13:30:10 +00:00
|
|
|
|
2016-03-21 12:37:34 +00:00
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
2016-06-29 16:33:02 +01:00
|
|
|
str(api_user.service_id),
|
2016-03-21 12:37:34 +00:00
|
|
|
filter_dict=data,
|
2016-04-19 10:52:52 +01:00
|
|
|
page=page,
|
2016-04-28 16:10:35 +01:00
|
|
|
page_size=page_size,
|
2016-06-30 18:43:15 +01:00
|
|
|
limit_days=limit_days,
|
|
|
|
|
key_type=api_user.key_type)
|
2016-03-01 13:30:10 +00:00
|
|
|
return jsonify(
|
2016-07-26 14:33:14 +01:00
|
|
|
notifications=notification_with_personalisation_schema.dump(pagination.items, many=True).data,
|
2016-04-19 10:52:52 +01:00
|
|
|
page_size=page_size,
|
|
|
|
|
total=pagination.total,
|
2016-03-01 13:30:10 +00:00
|
|
|
links=pagination_links(
|
2016-03-21 12:37:34 +00:00
|
|
|
pagination,
|
2016-03-01 13:30:10 +00:00
|
|
|
'.get_all_notifications',
|
2016-03-16 16:47:18 +00:00
|
|
|
**request.args.to_dict()
|
2016-03-01 13:30:10 +00:00
|
|
|
)
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
@notifications.route('/notifications/<string:notification_type>', methods=['POST'])
|
2016-02-25 11:35:32 +00:00
|
|
|
def send_notification(notification_type):
|
2016-02-29 11:03:48 +00:00
|
|
|
if notification_type not in ['sms', 'email']:
|
|
|
|
|
assert False
|
2016-02-24 09:55:05 +00:00
|
|
|
|
2016-06-29 16:33:02 +01:00
|
|
|
service_id = str(api_user.service_id)
|
|
|
|
|
service = services_dao.dao_fetch_service_by_id(service_id)
|
2016-03-09 11:06:37 +00:00
|
|
|
|
|
|
|
|
service_stats = notifications_dao.dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
service_id,
|
2016-06-20 16:23:56 +01:00
|
|
|
datetime.today().strftime(DATE_FORMAT)
|
2016-03-09 11:06:37 +00:00
|
|
|
)
|
2016-03-10 09:48:29 +00:00
|
|
|
|
2016-03-09 11:06:37 +00:00
|
|
|
if service_stats:
|
|
|
|
|
total_sms_count = service_stats.sms_requested
|
|
|
|
|
total_email_count = service_stats.emails_requested
|
|
|
|
|
|
2016-07-14 11:25:45 +01:00
|
|
|
if (total_email_count + total_sms_count >= service.message_limit):
|
2016-06-14 15:07:23 +01:00
|
|
|
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
|
|
|
|
|
raise InvalidRequest(error, status_code=429)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
notification, errors = (
|
2016-07-21 12:20:50 +01:00
|
|
|
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
2016-02-29 11:03:48 +00:00
|
|
|
).load(request.get_json())
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-06-15 16:19:28 +01:00
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2016-02-29 11:23:34 +00:00
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
2016-02-23 17:30:50 +00:00
|
|
|
template_id=notification['template'],
|
|
|
|
|
service_id=service_id
|
2016-02-29 11:23:34 +00:00
|
|
|
)
|
2016-02-23 17:30:50 +00:00
|
|
|
|
2016-05-24 13:08:38 +01:00
|
|
|
errors = unarchived_template_schema.validate({'archived': template.archived})
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-05-24 13:08:38 +01:00
|
|
|
|
2016-07-05 16:21:31 +01:00
|
|
|
template_object = Template(
|
|
|
|
|
template.__dict__,
|
|
|
|
|
notification.get('personalisation', {}),
|
2016-07-07 15:59:50 +01:00
|
|
|
renderer=PassThrough()
|
2016-07-05 16:21:31 +01:00
|
|
|
)
|
2016-02-29 11:23:34 +00:00
|
|
|
if template_object.missing_data:
|
2016-06-14 15:07:23 +01:00
|
|
|
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
|
|
|
|
errors = {'template': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2016-02-29 11:23:34 +00:00
|
|
|
if template_object.additional_data:
|
2016-06-14 15:07:23 +01:00
|
|
|
message = 'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
|
|
|
|
|
errors = {'template': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-02-29 11:23:34 +00:00
|
|
|
|
2016-07-21 12:20:50 +01:00
|
|
|
if (
|
|
|
|
|
template_object.template_type == SMS_TYPE and
|
|
|
|
|
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
):
|
2016-06-14 15:07:23 +01:00
|
|
|
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(char_count)
|
|
|
|
|
errors = {'content': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-29 10:36:59 +01:00
|
|
|
|
2016-06-30 13:21:35 +01:00
|
|
|
if (service.restricted or api_user.key_type == KEY_TYPE_TEAM) and not allowed_to_send_to(
|
2016-04-04 15:02:25 +01:00
|
|
|
notification['to'],
|
|
|
|
|
itertools.chain.from_iterable(
|
|
|
|
|
[user.mobile_number, user.email_address] for user in service.users
|
|
|
|
|
)
|
|
|
|
|
):
|
2016-06-14 15:07:23 +01:00
|
|
|
message = 'Invalid {} for restricted service'.format(first_column_heading[notification_type])
|
|
|
|
|
errors = {'to': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
notification_id = create_uuid()
|
2016-05-13 16:25:05 +01:00
|
|
|
notification.update({"template_version": template.version})
|
2016-06-30 15:41:51 +01:00
|
|
|
if notification_type == SMS_TYPE:
|
2016-06-30 17:32:49 +01:00
|
|
|
send_sms.apply_async(
|
|
|
|
|
(
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
|
|
|
|
encryption.encrypt(notification),
|
|
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)
|
|
|
|
|
),
|
|
|
|
|
kwargs={
|
|
|
|
|
'api_key_id': str(api_user.id),
|
|
|
|
|
'key_type': api_user.key_type
|
|
|
|
|
},
|
|
|
|
|
queue='sms'
|
|
|
|
|
)
|
2016-02-24 09:55:05 +00:00
|
|
|
else:
|
2016-06-30 17:32:49 +01:00
|
|
|
send_email.apply_async(
|
|
|
|
|
(
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
|
|
|
|
encryption.encrypt(notification),
|
|
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)
|
|
|
|
|
),
|
|
|
|
|
kwargs={
|
|
|
|
|
'api_key_id': str(api_user.id),
|
|
|
|
|
'key_type': api_user.key_type
|
|
|
|
|
},
|
|
|
|
|
queue='email'
|
|
|
|
|
)
|
2016-03-24 13:34:45 +00:00
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
return jsonify(
|
|
|
|
|
data=get_notification_return_data(
|
|
|
|
|
notification_id,
|
|
|
|
|
notification,
|
|
|
|
|
template_object)
|
|
|
|
|
), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_notification_return_data(notification_id, notification, template):
|
|
|
|
|
output = {
|
|
|
|
|
'body': template.replaced,
|
|
|
|
|
'template_version': notification['template_version'],
|
|
|
|
|
'notification': {'id': notification_id}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if template.template_type == 'email':
|
2016-06-21 15:03:33 +01:00
|
|
|
output.update({'subject': template.replaced_subject})
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
return output
|
2016-05-19 16:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notifications.route('/notifications/statistics')
|
2016-05-20 11:31:35 +01:00
|
|
|
def get_notification_statistics_for_day():
|
2016-06-14 15:07:23 +01:00
|
|
|
data = day_schema.load(request.args).data
|
2016-05-20 15:16:53 +01:00
|
|
|
statistics = notifications_dao.dao_get_potential_notification_statistics_for_day(
|
|
|
|
|
day=data['day']
|
2016-05-20 11:31:35 +01:00
|
|
|
)
|
2016-05-19 16:42:21 +01:00
|
|
|
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
2016-05-20 11:31:35 +01:00
|
|
|
return jsonify(data=data), 200
|