Merge pull request #1075 from alphagov/letter-api-refactor

Letter api step 1 - refactor
This commit is contained in:
Leo Hemsted
2017-07-25 14:53:39 +01:00
committed by GitHub
9 changed files with 402 additions and 100 deletions

View File

@@ -36,6 +36,7 @@ def check_placeholders(template_object):
def persist_notification(
*,
template_id,
template_version,
recipient,

View File

@@ -9,7 +9,7 @@ from notifications_utils.clients.redis import rate_limit_cache_key, daily_limit_
from app.dao import services_dao, templates_dao
from app.models import (
INTERNATIONAL_SMS_TYPE, SMS_TYPE,
INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE,
KEY_TYPE_TEST, KEY_TYPE_TEAM, SCHEDULE_NOTIFICATIONS
)
from app.service.utils import service_allowed_to_send_to
@@ -104,7 +104,7 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type)
number=send_to,
international=international_phone_info.international
)
else:
elif notification_type == EMAIL_TYPE:
return validate_and_format_email_address(email_address=send_to)

View File

@@ -20,6 +20,9 @@ personalisation = {
}
letter_personalisation = dict(personalisation, required=["address_line_1", "postcode"])
https_url = {
"type": "string",
"format": "uri",

View File

@@ -0,0 +1,45 @@
def create_post_sms_response_from_notification(notification, content, from_number, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
'from_number': from_number,
'body': content
}
return noti
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
"from_email": email_from,
"body": content,
"subject": subject
}
return noti
def create_post_letter_response_from_notification(notification, content, subject, url_root, scheduled_for):
noti = __create_notification_response(notification, url_root, scheduled_for)
noti['content'] = {
"body": content,
"subject": subject
}
return noti
def __create_notification_response(notification, url_root, scheduled_for):
return {
"id": notification.id,
"reference": notification.client_reference,
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
'template': {
"id": notification.template_id,
"version": notification.template_version,
"uri": "{}services/{}/templates/{}".format(
url_root,
str(notification.service_id),
str(notification.template_id)
)
},
"scheduled_for": scheduled_for if scheduled_for else None
}

View File

@@ -1,5 +1,5 @@
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
from app.schema_validation.definitions import (uuid, personalisation)
from app.schema_validation.definitions import (uuid, personalisation, letter_personalisation)
template = {
@@ -192,40 +192,43 @@ post_email_response = {
}
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,
'from_number': 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),
"scheduled_for": scheduled_for if scheduled_for else None
}
post_letter_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST letter notification schema",
"type": "object",
"title": "POST v2/notifications/letter",
"properties": {
"reference": {"type": "string"},
"template_id": uuid,
"personalisation": letter_personalisation
},
"required": ["template_id", "personalisation"]
}
letter_content = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Letter content for POST letter notification",
"type": "object",
"title": "notification letter content",
"properties": {
"body": {"type": "string"},
"subject": {"type": "string"}
},
"required": ["body", "subject"]
}
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,
"content": {
"from_email": email_from,
"body": content,
"subject": 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),
"scheduled_for": scheduled_for if scheduled_for else None
}
def __create_template_from_notification(notification, url_root, service_id):
return {
"id": notification.template_id,
"version": notification.template_version,
"uri": "{}services/{}/templates/{}".format(url_root, str(service_id), str(notification.template_id))
}
post_letter_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "response v2/notifications/letter",
"properties": {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": letter_content,
"uri": {"type": "string", "format": "uri"},
"template": template,
"scheduled_for": {"type": ["string", "null"]}
},
"required": ["id", "content", "uri", "template"]
}

View File

@@ -1,8 +1,10 @@
from flask import request, jsonify, current_app
import functools
from flask import request, jsonify, current_app, abort
from app import api_user, authenticated_service
from app.config import QueueNames
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY, SCHEDULE_NOTIFICATIONS
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, PRIORITY
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue,
@@ -11,28 +13,34 @@ from app.notifications.process_notifications import (
from app.notifications.validators import (
validate_and_format_recipient,
check_rate_limiting,
service_has_permission,
check_service_can_schedule_notification,
check_service_has_permission,
validate_template
)
from app.schema_validation import validate
from app.utils import get_public_notify_type_text
from app.v2.notifications import v2_notification_blueprint
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)
from app.v2.errors import BadRequestError
post_letter_request
)
from app.v2.notifications.create_response import (
create_post_sms_response_from_notification,
create_post_email_response_from_notification,
create_post_letter_response_from_notification
)
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
def post_notification(notification_type):
if notification_type == EMAIL_TYPE:
form = validate(request.get_json(), post_email_request)
else:
elif notification_type == SMS_TYPE:
form = validate(request.get_json(), post_sms_request)
elif notification_type == LETTER_TYPE:
form = validate(request.get_json(), post_letter_request)
else:
abort(404)
check_service_has_permission(notification_type, authenticated_service.permissions)
@@ -41,12 +49,6 @@ def post_notification(notification_type):
check_rate_limiting(authenticated_service, api_user)
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,
service=authenticated_service,
notification_type=notification_type)
template, template_with_content = validate_template(
form['template_id'],
form.get('personalisation', {}),
@@ -54,20 +56,74 @@ def post_notification(notification_type):
notification_type,
)
if notification_type == LETTER_TYPE:
notification = process_letter_notification(
form=form,
api_key=api_user,
template=template,
service=authenticated_service,
)
else:
notification = process_sms_or_email_notification(
form=form,
notification_type=notification_type,
api_key=api_user,
template=template,
service=authenticated_service
)
if notification_type == SMS_TYPE:
sms_sender = authenticated_service.sms_sender or current_app.config.get('FROM_NUMBER')
create_resp_partial = functools.partial(
create_post_sms_response_from_notification,
from_number=sms_sender
)
elif notification_type == EMAIL_TYPE:
create_resp_partial = functools.partial(
create_post_email_response_from_notification,
subject=template_with_content.subject,
email_from=authenticated_service.email_from
)
elif notification_type == LETTER_TYPE:
create_resp_partial = functools.partial(
create_post_letter_response_from_notification,
subject=template_with_content.subject,
)
resp = create_resp_partial(
notification=notification,
content=str(template_with_content),
url_root=request.url_root,
scheduled_for=scheduled_for
)
return jsonify(resp), 201
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service):
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
send_to = validate_and_format_recipient(send_to=form_send_to,
key_type=api_key.key_type,
service=service,
notification_type=notification_type)
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(send_to, notification_type)
notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=form_send_to,
service=authenticated_service,
personalisation=form.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_user.id,
key_type=api_user.key_type,
client_reference=form.get('reference', None),
simulated=simulated)
notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=form_send_to,
service=service,
personalisation=form.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_key.id,
key_type=api_key.key_type,
client_reference=form.get('reference', None),
simulated=simulated
)
scheduled_for = form.get("scheduled_for", None)
if scheduled_for:
persist_scheduled_notification(notification.id, form["scheduled_for"])
else:
@@ -75,26 +131,19 @@ def post_notification(notification_type):
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
send_notification_to_queue(
notification=notification,
research_mode=authenticated_service.research_mode,
research_mode=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,
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,
scheduled_for=scheduled_for)
return jsonify(resp), 201
return notification
def process_letter_notification(*, form, api_key, template, service):
# create job
# create notification
# trigger build_dvla_file task
raise NotImplementedError