Merge branch 'main' into 1006-new-api-failed-and-delivered-messages-7-days

This commit is contained in:
Anastasia Gradova
2024-06-20 08:35:12 -06:00
55 changed files with 253 additions and 5051 deletions

View File

@@ -117,7 +117,6 @@ def create_app(application):
document_download_client.init_app(application)
register_blueprint(application)
register_v2_blueprints(application)
# avoid circular imports by importing this file later
from app.commands import setup_commands
@@ -252,34 +251,6 @@ def register_blueprint(application):
application.register_blueprint(upload_blueprint)
def register_v2_blueprints(application):
from app.authentication.auth import requires_auth
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint
from app.v2.notifications import ( # noqa
get_notifications,
post_notifications,
v2_notification_blueprint,
)
from app.v2.template import ( # noqa
get_template,
post_template,
v2_template_blueprint,
)
from app.v2.templates.get_templates import v2_templates_blueprint
v2_notification_blueprint.before_request(requires_auth)
application.register_blueprint(v2_notification_blueprint)
v2_templates_blueprint.before_request(requires_auth)
application.register_blueprint(v2_templates_blueprint)
v2_template_blueprint.before_request(requires_auth)
application.register_blueprint(v2_template_blueprint)
v2_inbound_sms_blueprint.before_request(requires_auth)
application.register_blueprint(v2_inbound_sms_blueprint)
def init_app(app):
@app.before_request
def record_request_details():

View File

@@ -12,7 +12,7 @@ FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
# Temporarily extend cache to 7 days
ttl = 60 * 60 * 24 * 7
JOBS = ExpiringDict(max_len=1000, max_age_seconds=ttl)
JOBS = ExpiringDict(max_len=20000, max_age_seconds=ttl)
JOBS_CACHE_HITS = "JOBS_CACHE_HITS"

View File

@@ -19,12 +19,12 @@ from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.dao.templates_dao import dao_get_template_by_id
from app.enums import JobStatus, KeyType, NotificationType
from app.errors import TotalRequestsError
from app.notifications.process_notifications import persist_notification
from app.notifications.validators import check_service_over_total_message_limit
from app.serialised_models import SerialisedService, SerialisedTemplate
from app.service.utils import service_allowed_to_send_to
from app.utils import DATETIME_FORMAT, utc_now
from app.v2.errors import TotalRequestsError
from app.utils import DATETIME_FORMAT, hilite, scrub, utc_now
from notifications_utils.recipients import RecipientCSV
@@ -189,6 +189,11 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
# Return False when trial mode services try sending notifications
# to non-team and non-simulated recipients.
if not service_allowed_to_send_to(notification["to"], service, KeyType.NORMAL):
current_app.logger.info(
hilite(
scrub(f"service not allowed to send to {notification['to']}, aborting")
)
)
current_app.logger.debug(
"SMS {} failed as restricted service".format(notification_id)
)
@@ -219,6 +224,9 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
)
# Kick off sns process in provider_tasks.py
current_app.logger.info(
hilite(scrub(f"Going to deliver sms for recipient: {notification['to']}"))
)
provider_tasks.deliver_sms.apply_async(
[str(saved_notification.id)], queue=QueueNames.SEND_SMS
)

View File

@@ -9,7 +9,7 @@ from flask import current_app
from app.clients import AWS_CLIENT_CONFIG, Client
from app.cloudfoundry_config import cloud_config
from app.exceptions import NotificationTechnicalFailureException
from app.utils import utc_now
from app.utils import hilite, scrub, utc_now
class AwsCloudwatchClient(Client):
@@ -124,6 +124,7 @@ class AwsCloudwatchClient(Client):
self.warn_if_dev_is_opted_out(
message["delivery"]["providerResponse"], notification_id
)
current_app.logger.info(hilite(scrub(f"DELIVERED: {message}")))
return (
"success",
message["delivery"]["providerResponse"],
@@ -140,6 +141,8 @@ class AwsCloudwatchClient(Client):
self.warn_if_dev_is_opted_out(
message["delivery"]["providerResponse"], notification_id
)
current_app.logger.info(hilite(scrub(f"FAILED: {message}")))
return (
"failure",
message["delivery"]["providerResponse"],

View File

@@ -13,7 +13,7 @@ from app.dao.provider_details_dao import get_provider_details_by_notification_ty
from app.enums import BrandType, KeyType, NotificationStatus, NotificationType
from app.exceptions import NotificationTechnicalFailureException
from app.serialised_models import SerialisedService, SerialisedTemplate
from app.utils import utc_now
from app.utils import hilite, scrub, utc_now
from notifications_utils.template import (
HTMLEmailTemplate,
PlainTextEmailTemplate,
@@ -109,15 +109,19 @@ def send_sms_to_provider(notification):
"international": notification.international,
}
db.session.close() # no commit needed as no changes to objects have been made above
current_app.logger.info("sending to sms")
message_id = provider.send_sms(**send_sms_kwargs)
current_app.logger.info(f"got message_id {message_id}")
except Exception as e:
current_app.logger.error(e)
msg = f"FAILED sending message for this recipient: {recipient} to sms"
current_app.logger.error(hilite(scrub(f"{msg} {e}")))
notification.billable_units = template.fragment_count
dao_update_notification(notification)
raise e
else:
msg = f"Sending message for this recipient: {recipient} to sms"
current_app.logger.info(hilite(scrub(msg)))
notification.billable_units = template.fragment_count
update_notification_to_sending(notification, provider)
return message_id

View File

@@ -5,6 +5,7 @@ from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app.authentication.auth import AuthError
from app.enums import KeyType
from app.exceptions import ArchiveValidationError
from notifications_utils.recipients import InvalidEmailError
@@ -113,3 +114,45 @@ def register_errors(blueprint):
e = getattr(e, "original_exception", e)
current_app.logger.exception(e)
return jsonify(result="error", message="Internal server error"), 500
class TooManyRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded send limits ({}) for today"
def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)
class TotalRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded total application limits ({}) for today"
def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)
class RateLimitError(InvalidRequest):
status_code = 429
message_template = (
"Exceeded rate limit for key type {} of {} requests per {} seconds"
)
def __init__(self, sending_limit, interval, key_type):
# normal keys are spoken of as "live" in the documentation
# so using this in the error messaging
if key_type == KeyType.NORMAL:
key_type = "live"
self.message = self.message_template.format(
key_type.upper(), sending_limit, interval
)
class BadRequestError(InvalidRequest):
message = "An error occurred"
def __init__(self, fields=None, message=None, status_code=400):
self.status_code = status_code
self.fields = fields or []
self.message = message if message else self.message

View File

@@ -1215,7 +1215,6 @@ class Template(TemplateBase):
)
def get_link(self):
# TODO: use "/v2/" route once available
return url_for(
"template.get_template_by_id_and_service_id",
service_id=self.service_id,
@@ -1284,8 +1283,9 @@ class TemplateHistory(TemplateBase):
def get_link(self):
return url_for(
"v2_template.get_template_by_id",
"template.get_template_by_id_and_service_id",
template_id=self.id,
service_id=self.service.id,
version=self.version,
_external=True,
)

View File

@@ -10,9 +10,9 @@ from app.dao.notifications_dao import (
dao_delete_notifications_by_id,
)
from app.enums import KeyType, NotificationStatus, NotificationType
from app.errors import BadRequestError
from app.models import Notification
from app.utils import utc_now
from app.v2.errors import BadRequestError
from app.utils import hilite, scrub, utc_now
from notifications_utils.recipients import (
format_email_address,
get_international_phone_info,
@@ -110,6 +110,11 @@ def persist_notification(
formatted_recipient = validate_and_format_phone_number(
recipient, international=True
)
current_app.logger.info(
hilite(
scrub(f"Persisting notification with recipient {formatted_recipient}")
)
)
recipient_info = get_international_phone_info(formatted_recipient)
notification.normalised_to = formatted_recipient
notification.international = recipient_info.international

View File

@@ -6,12 +6,12 @@ from app.dao.notifications_dao import dao_get_notification_count_for_service
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.enums import KeyType, NotificationType, ServicePermissionType, TemplateType
from app.errors import BadRequestError, RateLimitError, TotalRequestsError
from app.models import ServicePermission
from app.notifications.process_notifications import create_content_for_notification
from app.serialised_models import SerialisedTemplate
from app.service.utils import service_allowed_to_send_to
from app.utils import get_public_notify_type_text
from app.v2.errors import BadRequestError, RateLimitError, TotalRequestsError
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.clients.redis import (
rate_limit_cache_key,

View File

@@ -569,11 +569,6 @@ def get_all_notifications_for_service(service_id):
)
current_app.logger.debug(f"number of notifications are {len(notifications)}")
if len(notifications) > 0:
current_app.logger.debug(f"first notification is {notifications[0]}")
else:
current_app.logger.debug("there are no notifications to show")
# We try and get the next page of results to work out if we need provide a pagination link to the next page
# in our response if it exists. Note, this could be done instead by changing `count_pages` in the previous
# call to be True which will enable us to use Flask-Sqlalchemy to tell if there is a next page of results but

View File

@@ -7,6 +7,7 @@ from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
from app.dao.users_dao import get_user_by_id
from app.enums import KeyType, NotificationType, TemplateProcessType
from app.errors import BadRequestError
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue,
@@ -16,7 +17,6 @@ from app.notifications.validators import (
validate_and_format_recipient,
validate_template,
)
from app.v2.errors import BadRequestError
def validate_created_by(service, created_by_id):

View File

@@ -1,3 +1,4 @@
import re
from datetime import datetime, timedelta, timezone
from flask import url_for
@@ -144,3 +145,15 @@ def naive_utcnow():
def utc_now():
return naive_utcnow()
def scrub(msg):
# Eventually we want to scrub all messages in all logs for phone numbers
# and email addresses, masking them. Ultimately this will probably get
# refactored into a 'SafeLogger' subclass or something, but let's start here
# with phones.
phones = re.findall("(?:\\+ *)?\\d[\\d\\- ]{7,}\\d", msg)
phones = [phone.replace("-", "").replace(" ", "") for phone in phones]
for phone in phones:
msg = msg.replace(phone, f"1XXXXX{phone[-5:]}")
return msg

View File

View File

@@ -1,125 +0,0 @@
import json
from flask import current_app, jsonify, request
from jsonschema import ValidationError as JsonSchemaValidationError
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app.authentication.auth import AuthError
from app.enums import KeyType
from app.errors import InvalidRequest
from notifications_utils.recipients import InvalidEmailError
class TooManyRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded send limits ({}) for today"
def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)
class TotalRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded total application limits ({}) for today"
def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)
class RateLimitError(InvalidRequest):
status_code = 429
message_template = (
"Exceeded rate limit for key type {} of {} requests per {} seconds"
)
def __init__(self, sending_limit, interval, key_type):
# normal keys are spoken of as "live" in the documentation
# so using this in the error messaging
if key_type == KeyType.NORMAL:
key_type = "live"
self.message = self.message_template.format(
key_type.upper(), sending_limit, interval
)
class BadRequestError(InvalidRequest):
message = "An error occurred"
def __init__(self, fields=None, message=None, status_code=400):
self.status_code = status_code
self.fields = fields or []
self.message = message if message else self.message
class ValidationError(InvalidRequest):
message = "Your notification has failed validation"
def __init__(self, fields=None, message=None, status_code=400):
self.status_code = status_code
self.fields = fields or []
self.message = message if message else self.message
def register_errors(blueprint):
@blueprint.errorhandler(InvalidEmailError)
def invalid_format(error):
# Please not that InvalidEmailError is re-raised for InvalidEmail or InvalidPhone,
# work should be done in the utils app to tidy up these errors.
current_app.logger.info(error)
return (
jsonify(
status_code=400,
errors=[{"error": error.__class__.__name__, "message": str(error)}],
),
400,
)
@blueprint.errorhandler(InvalidRequest)
def invalid_data(error):
current_app.logger.info(error)
response = jsonify(error.to_dict_v2()), error.status_code
return response
@blueprint.errorhandler(JsonSchemaValidationError)
def validation_error(error):
current_app.logger.info(error)
return jsonify(json.loads(error.message)), 400
@blueprint.errorhandler(NoResultFound)
@blueprint.errorhandler(DataError)
def no_result_found(e):
current_app.logger.info(e)
return (
jsonify(
status_code=404,
errors=[{"error": e.__class__.__name__, "message": "No result found"}],
),
404,
)
@blueprint.errorhandler(AuthError)
def auth_error(error):
current_app.logger.info(
"API AuthError, client: {} error: {}".format(
request.headers.get("User-Agent"), error
)
)
return jsonify(error.to_dict_v2()), error.code
@blueprint.errorhandler(Exception)
def internal_server_error(error):
current_app.logger.exception(error)
return (
jsonify(
status_code=500,
errors=[
{
"error": error.__class__.__name__,
"message": "Internal server error",
}
],
),
500,
)

View File

@@ -1,9 +0,0 @@
from flask import Blueprint
from app.v2.errors import register_errors
v2_inbound_sms_blueprint = Blueprint(
"v2_inbound_sms", __name__, url_prefix="/v2/received-text-messages"
)
register_errors(v2_inbound_sms_blueprint)

View File

@@ -1,46 +0,0 @@
from flask import current_app, jsonify, request, url_for
from app import authenticated_service
from app.dao import inbound_sms_dao
from app.schema_validation import validate
from app.v2.inbound_sms import v2_inbound_sms_blueprint
from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_request
@v2_inbound_sms_blueprint.route("", methods=["GET"])
def get_inbound_sms():
data = validate(request.args.to_dict(), get_inbound_sms_request)
paginated_inbound_sms = (
inbound_sms_dao.dao_get_paginated_inbound_sms_for_service_for_public_api(
authenticated_service.id,
older_than=data.get("older_than", None),
page_size=current_app.config.get("API_PAGE_SIZE"),
)
)
return (
jsonify(
received_text_messages=[i.serialize() for i in paginated_inbound_sms],
links=_build_links(paginated_inbound_sms),
),
200,
)
def _build_links(inbound_sms_list):
_links = {
"current": url_for(
"v2_inbound_sms.get_inbound_sms",
_external=True,
),
}
if inbound_sms_list:
_links["next"] = url_for(
"v2_inbound_sms.get_inbound_sms",
older_than=inbound_sms_list[-1].id,
_external=True,
)
return _links

View File

@@ -1,61 +0,0 @@
from app.schema_validation.definitions import uuid
get_inbound_sms_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "schema for query parameters allowed when getting list of received text messages",
"type": "object",
"properties": {
"older_than": uuid,
},
"additionalProperties": False,
}
get_inbound_sms_single_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET inbound sms schema response",
"type": "object",
"title": "GET response v2/inbound_sms",
"properties": {
"user_number": {"type": "string"},
"created_at": {
"format": "date-time",
"type": "string",
"description": "Date+time created at",
},
"service_id": uuid,
"id": uuid,
"notify_number": {"type": "string"},
"content": {"type": "string"},
},
"required": [
"id",
"user_number",
"created_at",
"service_id",
"notify_number",
"content",
],
"additionalProperties": False,
}
get_inbound_sms_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET list of inbound sms response schema",
"type": "object",
"properties": {
"received_text_messages": {
"type": "array",
"items": {"type": "object", "$ref": "#/definitions/inbound_sms"},
},
"links": {
"type": "object",
"properties": {"current": {"type": "string"}, "next": {"type": "string"}},
"additionalProperties": False,
"required": ["current"],
},
},
"required": ["received_text_messages", "links"],
"definitions": {"inbound_sms": get_inbound_sms_single_response},
"additionalProperties": False,
}

View File

@@ -1,9 +0,0 @@
from flask import Blueprint
from app.v2.errors import register_errors
v2_notification_blueprint = Blueprint(
"v2_notifications", __name__, url_prefix="/v2/notifications"
)
register_errors(v2_notification_blueprint)

View File

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

View File

@@ -1,88 +0,0 @@
from flask import current_app, jsonify, request, url_for
from app import api_user, authenticated_service
from app.aws.s3 import get_personalisation_from_s3
from app.dao import notifications_dao
from app.schema_validation import validate
from app.v2.notifications import v2_notification_blueprint
from app.v2.notifications.notification_schemas import (
get_notifications_request,
notification_by_id,
)
@v2_notification_blueprint.route("/<notification_id>", methods=["GET"])
def get_notification_by_id(notification_id):
_data = {"notification_id": notification_id}
validate(_data, notification_by_id)
notification = notifications_dao.get_notification_with_personalisation(
authenticated_service.id, notification_id, key_type=None
)
notification.personalisation = get_personalisation_from_s3(
notification.service_id,
notification.job_id,
notification.job_row_number,
)
return jsonify(notification.serialize()), 200
@v2_notification_blueprint.route("", methods=["GET"])
def get_notifications():
_data = request.args.to_dict(flat=False)
# flat=False makes everything a list, but we only ever allow one value for "older_than"
if "older_than" in _data:
_data["older_than"] = _data["older_than"][0]
# and client reference
if "reference" in _data:
_data["reference"] = _data["reference"][0]
if "include_jobs" in _data:
_data["include_jobs"] = _data["include_jobs"][0]
data = validate(_data, get_notifications_request)
paginated_notifications = notifications_dao.get_notifications_for_service(
str(authenticated_service.id),
filter_dict=data,
key_type=api_user.key_type,
personalisation=True,
older_than=data.get("older_than"),
client_reference=data.get("reference"),
page_size=current_app.config.get("API_PAGE_SIZE"),
include_jobs=data.get("include_jobs"),
count_pages=False,
)
for notification in paginated_notifications.items:
if notification.job_id is not None:
notification.personalisation = get_personalisation_from_s3(
notification.service_id,
notification.job_id,
notification.job_row_number,
)
def _build_links(notifications):
_links = {
"current": url_for(".get_notifications", _external=True, **data),
}
if len(notifications):
next_query_params = dict(data, older_than=notifications[-1].id)
_links["next"] = url_for(
".get_notifications", _external=True, **next_query_params
)
return _links
return (
jsonify(
notifications=[
notification.serialize()
for notification in paginated_notifications.items
],
links=_build_links(paginated_notifications.items),
),
200,
)

View File

@@ -1,211 +0,0 @@
from app.enums import NotificationStatus, TemplateType
from app.schema_validation.definitions import personalisation, uuid
template = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "template schema",
"type": "object",
"title": "notification content",
"properties": {
"id": uuid,
"version": {"type": "integer"},
"uri": {"type": "string", "format": "uri"},
},
"required": ["id", "version", "uri"],
}
notification_by_id = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET notification response schema",
"type": "object",
"title": "response v2/notification",
"properties": {"notification_id": uuid},
"required": ["notification_id"],
}
get_notification_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET notification response schema",
"type": "object",
"title": "response v2/notification",
"properties": {
"id": uuid,
"reference": {"type": ["string", "null"]},
"email_address": {"type": ["string", "null"]},
"phone_number": {"type": ["string", "null"]},
"line_1": {"type": ["string", "null"]},
"line_2": {"type": ["string", "null"]},
"line_3": {"type": ["string", "null"]},
"line_4": {"type": ["string", "null"]},
"line_5": {"type": ["string", "null"]},
"line_6": {"type": ["string", "null"]},
"postcode": {"type": ["string", "null"]},
"type": {"enum": list(TemplateType)},
"status": {"type": "string"},
"template": template,
"body": {"type": "string"},
"subject": {"type": ["string", "null"]},
"created_at": {"type": "string"},
"sent_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
"id",
"reference",
"email_address",
"phone_number",
"line_1",
"line_2",
"line_3",
"line_4",
"line_5",
"line_6",
"postcode",
"type",
"status",
"template",
"body",
"created_at",
"sent_at",
"completed_at",
],
}
get_notifications_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "schema for query parameters allowed when getting list of notifications",
"type": "object",
"properties": {
"reference": {"type": "string"},
"status": {
"type": "array",
"items": {"enum": list(NotificationStatus)},
},
"template_type": {
"type": "array",
"items": {"enum": list(TemplateType)},
},
"include_jobs": {"enum": ["true", "True"]},
"older_than": uuid,
},
"additionalProperties": False,
}
get_notifications_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET list of notifications response schema",
"type": "object",
"properties": {
"notifications": {
"type": "array",
"items": {"type": "object", "$ref": "#/definitions/notification"},
},
"links": {
"type": "object",
"properties": {"current": {"type": "string"}, "next": {"type": "string"}},
"additionalProperties": False,
"required": ["current"],
},
},
"additionalProperties": False,
"required": ["notifications", "links"],
"definitions": {"notification": get_notification_response},
}
post_sms_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST sms notification schema",
"type": "object",
"title": "POST v2/notifications/sms",
"properties": {
"reference": {"type": "string"},
"phone_number": {"type": "string", "format": "phone_number"},
"template_id": uuid,
"personalisation": personalisation,
"scheduled_for": {
"type": ["string", "null"],
"format": "datetime_within_next_day",
},
"sms_sender_id": uuid,
},
"required": ["phone_number", "template_id"],
"additionalProperties": False,
}
sms_content = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "content schema for SMS notification response schema",
"type": "object",
"title": "notification content",
"properties": {"body": {"type": "string"}, "from_number": {"type": "string"}},
"required": ["body", "from_number"],
}
post_sms_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "response v2/notifications/sms",
"properties": {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": sms_content,
"uri": {"type": "string", "format": "uri"},
"template": template,
"scheduled_for": {"type": ["string", "null"]},
},
"required": ["id", "content", "uri", "template"],
}
post_email_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST email notification schema",
"type": "object",
"title": "POST v2/notifications/email",
"properties": {
"reference": {"type": "string"},
"email_address": {"type": "string", "format": "email_address"},
"template_id": uuid,
"personalisation": personalisation,
"scheduled_for": {
"type": ["string", "null"],
"format": "datetime_within_next_day",
},
"email_reply_to_id": uuid,
},
"required": ["email_address", "template_id"],
"additionalProperties": False,
}
email_content = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Email content for POST email notification",
"type": "object",
"title": "notification email content",
"properties": {
"from_email": {"type": "string", "format": "email_address"},
"body": {"type": "string"},
"subject": {"type": "string"},
},
"required": ["body", "from_email", "subject"],
}
post_email_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST email notification response schema",
"type": "object",
"title": "response v2/notifications/email",
"properties": {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": email_content,
"uri": {"type": "string", "format": "uri"},
"template": template,
"scheduled_for": {"type": ["string", "null"]},
},
"required": ["id", "content", "uri", "template"],
}

View File

@@ -1,7 +0,0 @@
from flask import Blueprint
from app.v2.errors import register_errors
v2_template_blueprint = Blueprint("v2_template", __name__, url_prefix="/v2/template")
register_errors(v2_template_blueprint)

View File

@@ -1,22 +0,0 @@
from flask import jsonify
from app import authenticated_service
from app.dao import templates_dao
from app.schema_validation import validate
from app.v2.template import v2_template_blueprint
from app.v2.template.template_schemas import get_template_by_id_request
@v2_template_blueprint.route("/<template_id>", methods=["GET"])
@v2_template_blueprint.route("/<template_id>/version/<int:version>", methods=["GET"])
def get_template_by_id(template_id, version=None):
_data = {"id": template_id}
if version:
_data["version"] = version
data = validate(_data, get_template_by_id_request)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id, authenticated_service.id, data.get("version")
)
return jsonify(template.serialize_for_v2()), 200

View File

@@ -1,50 +0,0 @@
from flask import jsonify, request
from app import authenticated_service
from app.dao import templates_dao
from app.schema_validation import validate
from app.v2.errors import BadRequestError
from app.v2.template import v2_template_blueprint
from app.v2.template.template_schemas import (
create_post_template_preview_response,
post_template_preview_request,
)
from app.v2.utils import get_valid_json
@v2_template_blueprint.route("/<template_id>/preview", methods=["POST"])
def post_template_preview(template_id):
# The payload is empty when there are no place holders in the template.
_data = request.get_data(as_text=True)
if not _data:
_data = {}
else:
_data = get_valid_json()
_data["id"] = template_id
data = validate(_data, post_template_preview_request)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id, authenticated_service.id
)
template_object = template._as_utils_template_with_personalisation(
data.get("personalisation")
)
check_placeholders(template_object)
resp = create_post_template_preview_response(
template=template, template_object=template_object
)
return jsonify(resp), 200
def check_placeholders(template_object):
if template_object.missing_data:
message = "Missing personalisation: {}".format(
", ".join(template_object.missing_data)
)
raise BadRequestError(message=message, fields=[{"template": message}])

View File

@@ -1,84 +0,0 @@
from app.enums import TemplateType
from app.schema_validation.definitions import personalisation, uuid
get_template_by_id_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "schema for parameters allowed when getting template by id",
"type": "object",
"properties": {"id": uuid, "version": {"type": ["integer", "null"], "minimum": 1}},
"required": ["id"],
"additionalProperties": False,
}
get_template_by_id_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET template by id schema response",
"type": "object",
"title": "reponse v2/template",
"properties": {
"id": uuid,
"type": {"enum": list(TemplateType)},
"created_at": {
"format": "date-time",
"type": "string",
"description": "Date+time created",
},
"updated_at": {
"format": "date-time",
"type": ["string", "null"],
"description": "Date+time updated",
},
"created_by": {"type": "string"},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]},
"name": {"type": "string"},
},
"required": [
"id",
"type",
"created_at",
"updated_at",
"version",
"created_by",
"body",
"name",
],
}
post_template_preview_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST template schema",
"type": "object",
"title": "POST v2/template/{id}/preview",
"properties": {"id": uuid, "personalisation": personalisation},
"required": ["id"],
}
post_template_preview_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "POST template preview schema response",
"type": "object",
"title": "reponse v2/template/{id}/preview",
"properties": {
"id": uuid,
"type": {"enum": list(TemplateType)},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]},
"html": {"type": ["string", "null"]},
},
"required": ["id", "type", "version", "body"],
"additionalProperties": False,
}
def create_post_template_preview_response(template, template_object):
return {
"id": template.id,
"type": template.template_type,
"version": template.version,
"body": template_object.content_with_placeholders_filled_in,
"html": getattr(template_object, "html_body", None),
"subject": getattr(template_object, "subject", None),
}

View File

@@ -1,7 +0,0 @@
from flask import Blueprint
from app.v2.errors import register_errors
v2_templates_blueprint = Blueprint("v2_templates", __name__, url_prefix="/v2/templates")
register_errors(v2_templates_blueprint)

View File

@@ -1,21 +0,0 @@
from flask import jsonify, request
from app import authenticated_service
from app.dao import templates_dao
from app.schema_validation import validate
from app.v2.templates import v2_templates_blueprint
from app.v2.templates.templates_schemas import get_all_template_request
@v2_templates_blueprint.route("", methods=["GET"])
def get_templates():
data = validate(request.args.to_dict(), get_all_template_request)
templates = templates_dao.dao_get_all_templates_for_service(
authenticated_service.id, data.get("type")
)
return (
jsonify(templates=[template.serialize_for_v2() for template in templates]),
200,
)

View File

@@ -1,24 +0,0 @@
from app.enums import TemplateType
from app.v2.template.template_schemas import get_template_by_id_response as template
get_all_template_request = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "request schema for parameters allowed when getting all templates",
"type": "object",
"properties": {"type": {"enum": list(TemplateType)}},
"additionalProperties": False,
}
get_all_template_response = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET response schema when getting all templates",
"type": "object",
"properties": {
"templates": {
"type": "array",
"items": {"type": "object", "$ref": "#/definitions/template"},
}
},
"required": ["templates"],
"definitions": {"template": template},
}

View File

@@ -1,14 +0,0 @@
from flask import request
from werkzeug.exceptions import BadRequest
from app.v2.errors import BadRequestError
def get_valid_json():
try:
request_json = request.get_json(force=True)
except BadRequest:
raise BadRequestError(
message="Invalid JSON supplied in POST data", status_code=400
)
return request_json or {}