2016-12-12 15:07:03 +00:00
|
|
|
from flask import request, jsonify, current_app
|
2016-10-31 12:22:26 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
from app import api_user, authenticated_service
|
2017-05-25 10:51:49 +01:00
|
|
|
from app.config import QueueNames
|
2016-10-27 11:46:37 +01:00
|
|
|
from app.dao import services_dao, templates_dao
|
2017-03-30 10:46:23 +01:00
|
|
|
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
|
2017-04-24 14:15:08 +01:00
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
create_content_for_notification,
|
|
|
|
|
persist_notification,
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
simulated_recipient)
|
|
|
|
|
from app.notifications.validators import (
|
|
|
|
|
check_template_is_for_notification_type,
|
|
|
|
|
check_template_is_active,
|
|
|
|
|
check_sms_content_char_count,
|
|
|
|
|
validate_and_format_recipient,
|
|
|
|
|
check_rate_limiting)
|
2016-10-27 11:46:37 +01:00
|
|
|
from app.schema_validation import validate
|
2016-10-31 12:22:26 +00:00
|
|
|
from app.v2.errors import BadRequestError
|
2017-03-16 18:15:49 +00:00
|
|
|
from app.v2.notifications import v2_notification_blueprint
|
2017-04-24 14:15:08 +01:00
|
|
|
from app.v2.notifications.notification_schemas import (
|
|
|
|
|
post_sms_request,
|
|
|
|
|
create_post_sms_response_from_notification,
|
|
|
|
|
post_email_request,
|
|
|
|
|
create_post_email_response_from_notification)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
|
2017-01-17 12:08:24 +00:00
|
|
|
def post_notification(notification_type):
|
|
|
|
|
if notification_type == EMAIL_TYPE:
|
|
|
|
|
form = validate(request.get_json(), post_email_request)
|
|
|
|
|
else:
|
|
|
|
|
form = validate(request.get_json(), post_sms_request)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
check_rate_limiting(authenticated_service, api_user)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
|
|
|
|
|
send_to = validate_and_format_recipient(send_to=form_send_to,
|
|
|
|
|
key_type=api_user.key_type,
|
2017-05-05 15:23:06 +01:00
|
|
|
service=authenticated_service,
|
2017-01-17 12:08:24 +00:00
|
|
|
notification_type=notification_type)
|
2016-11-14 13:56:09 +00:00
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
template, template_with_content = __validate_template(form, authenticated_service, notification_type)
|
2016-11-14 13:56:09 +00:00
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
|
|
|
|
simulated = simulated_recipient(send_to, notification_type)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
2017-03-30 10:46:23 +01:00
|
|
|
notification = persist_notification(template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
2017-04-26 15:56:45 +01:00
|
|
|
recipient=form_send_to,
|
2017-05-05 15:23:06 +01:00
|
|
|
service=authenticated_service,
|
2017-03-30 10:46:23 +01:00
|
|
|
personalisation=form.get('personalisation', None),
|
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
api_key_id=api_user.id,
|
|
|
|
|
key_type=api_user.key_type,
|
2017-04-12 17:56:55 +01:00
|
|
|
client_reference=form.get('reference', None),
|
2017-03-30 10:46:23 +01:00
|
|
|
simulated=simulated)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
if not simulated:
|
2017-05-25 10:51:49 +01:00
|
|
|
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
2017-05-05 15:23:06 +01:00
|
|
|
send_notification_to_queue(
|
|
|
|
|
notification=notification,
|
|
|
|
|
research_mode=authenticated_service.research_mode,
|
|
|
|
|
queue=queue_name
|
|
|
|
|
)
|
|
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
else:
|
|
|
|
|
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
|
|
|
|
if notification_type == SMS_TYPE:
|
2017-05-16 12:55:12 +01:00
|
|
|
sms_sender = authenticated_service.sms_sender or current_app.config.get('FROM_NUMBER')
|
2017-01-17 12:08:24 +00:00
|
|
|
resp = create_post_sms_response_from_notification(notification=notification,
|
|
|
|
|
body=str(template_with_content),
|
|
|
|
|
from_number=sms_sender,
|
|
|
|
|
url_root=request.url_root,
|
2017-05-05 15:23:06 +01:00
|
|
|
service_id=authenticated_service.id)
|
2017-01-17 12:08:24 +00:00
|
|
|
else:
|
|
|
|
|
resp = create_post_email_response_from_notification(notification=notification,
|
|
|
|
|
content=str(template_with_content),
|
|
|
|
|
subject=template_with_content.subject,
|
2017-05-05 15:23:06 +01:00
|
|
|
email_from=authenticated_service.email_from,
|
2017-01-17 12:08:24 +00:00
|
|
|
url_root=request.url_root,
|
2017-05-05 15:23:06 +01:00
|
|
|
service_id=authenticated_service.id)
|
|
|
|
|
|
2016-11-14 13:56:09 +00:00
|
|
|
return jsonify(resp), 201
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
|
|
|
|
2016-11-14 13:56:09 +00:00
|
|
|
def __validate_template(form, service, notification_type):
|
2016-10-31 12:22:26 +00:00
|
|
|
try:
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
|
|
|
|
|
service_id=service.id)
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
message = 'Template not found'
|
|
|
|
|
raise BadRequestError(message=message,
|
|
|
|
|
fields=[{'template': message}])
|
|
|
|
|
|
2016-11-14 13:56:09 +00:00
|
|
|
check_template_is_for_notification_type(notification_type, template.template_type)
|
2016-10-28 17:10:00 +01:00
|
|
|
check_template_is_active(template)
|
|
|
|
|
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
2016-12-09 15:56:25 +00:00
|
|
|
if template.template_type == SMS_TYPE:
|
|
|
|
|
check_sms_content_char_count(template_with_content.content_count)
|
2016-11-14 13:56:09 +00:00
|
|
|
return template, template_with_content
|