Merge branch 'master' into redo-queue-visibitlity-timeout

Conflicts:
	app/notifications/process_notifications.py
	app/v2/notifications/post_notifications.py
This commit is contained in:
Martyn Inglis
2017-05-30 10:18:18 +01:00
30 changed files with 1473 additions and 345 deletions

View File

@@ -1,7 +1,7 @@
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
from app.schema_validation.definitions import (uuid, personalisation)
# this may belong in a templates module
template = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "template schema",
@@ -39,7 +39,8 @@ get_notification_response = {
"subject": {"type": ["string", "null"]},
"created_at": {"type": "string"},
"sent_at": {"type": ["string", "null"]},
"completed_at": {"type": ["string", "null"]}
"completed_at": {"type": ["string", "null"]},
"scheduled_for": {"type": ["string", "null"]}
},
"required": [
# technically, all keys are required since we always have all of them
@@ -111,7 +112,8 @@ post_sms_request = {
"reference": {"type": "string"},
"phone_number": {"type": "string", "format": "phone_number"},
"template_id": uuid,
"personalisation": personalisation
"personalisation": personalisation,
"scheduled_for": {"type": "string", "format": "datetime"}
},
"required": ["phone_number", "template_id"]
}
@@ -138,7 +140,8 @@ post_sms_response = {
"reference": {"type": ["string", "null"]},
"content": sms_content,
"uri": {"type": "string", "format": "uri"},
"template": template
"template": template,
"scheduled_for": {"type": "string"}
},
"required": ["id", "content", "uri", "template"]
}
@@ -153,7 +156,8 @@ post_email_request = {
"reference": {"type": "string"},
"email_address": {"type": "string", "format": "email_address"},
"template_id": uuid,
"personalisation": personalisation
"personalisation": personalisation,
"scheduled_for": {"type": "string", "format": "datetime"}
},
"required": ["email_address", "template_id"]
}
@@ -181,13 +185,14 @@ post_email_response = {
"reference": {"type": ["string", "null"]},
"content": email_content,
"uri": {"type": "string", "format": "uri"},
"template": template
"template": template,
"scheduled_for": {"type": "string"}
},
"required": ["id", "content", "uri", "template"]
}
def create_post_sms_response_from_notification(notification, body, from_number, url_root, service_id):
def create_post_sms_response_from_notification(notification, body, from_number, url_root, service_id, scheduled_for):
return {"id": notification.id,
"reference": notification.client_reference,
"content": {'body': body,
@@ -195,11 +200,13 @@ def create_post_sms_response_from_notification(notification, body, from_number,
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification,
url_root=url_root,
service_id=service_id)
service_id=service_id),
"scheduled_for": scheduled_for if scheduled_for else None
}
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, service_id):
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, service_id,
scheduled_for):
return {
"id": notification.id,
"reference": notification.client_reference,
@@ -211,7 +218,8 @@ def create_post_email_response_from_notification(notification, content, subject,
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification,
url_root=url_root,
service_id=service_id)
service_id=service_id),
"scheduled_for": scheduled_for if scheduled_for else None
}

View File

@@ -3,19 +3,20 @@ from sqlalchemy.orm.exc import NoResultFound
from app import api_user, authenticated_service
from app.config import QueueNames
from app.dao import services_dao, templates_dao
from app.dao import templates_dao
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
from app.notifications.process_notifications import (
create_content_for_notification,
persist_notification,
send_notification_to_queue,
simulated_recipient)
simulated_recipient,
persist_scheduled_notification)
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)
check_rate_limiting, service_can_schedule_notification)
from app.schema_validation import validate
from app.v2.errors import BadRequestError
from app.v2.notifications import v2_notification_blueprint
@@ -33,6 +34,10 @@ def post_notification(notification_type):
else:
form = validate(request.get_json(), post_sms_request)
scheduled_for = form.get("scheduled_for", None)
if scheduled_for:
if not service_can_schedule_notification(authenticated_service):
return
check_rate_limiting(authenticated_service, api_user)
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
@@ -57,31 +62,35 @@ def post_notification(notification_type):
client_reference=form.get('reference', None),
simulated=simulated)
if not simulated:
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
send_notification_to_queue(
notification=notification,
research_mode=authenticated_service.research_mode,
queue=queue_name
)
if scheduled_for:
persist_scheduled_notification(notification.id, form["scheduled_for"])
else:
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
if not simulated:
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
send_notification_to_queue(
notification=notification,
research_mode=authenticated_service.research_mode,
queue=queue_name
)
else:
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
if notification_type == SMS_TYPE:
sms_sender = authenticated_service.sms_sender or current_app.config.get('FROM_NUMBER')
resp = create_post_sms_response_from_notification(notification=notification,
body=str(template_with_content),
from_number=sms_sender,
url_root=request.url_root,
service_id=authenticated_service.id)
service_id=authenticated_service.id,
scheduled_for=scheduled_for)
else:
resp = create_post_email_response_from_notification(notification=notification,
content=str(template_with_content),
subject=template_with_content.subject,
email_from=authenticated_service.email_from,
url_root=request.url_root,
service_id=authenticated_service.id)
service_id=authenticated_service.id,
scheduled_for=scheduled_for)
return jsonify(resp), 201