mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
notify-api-412 use black to enforce python style standards
This commit is contained in:
@@ -12,7 +12,7 @@ from app.errors import InvalidRequest
|
||||
|
||||
class TooManyRequestsError(InvalidRequest):
|
||||
status_code = 429
|
||||
message_template = 'Exceeded send limits ({}) for today'
|
||||
message_template = "Exceeded send limits ({}) for today"
|
||||
|
||||
def __init__(self, sending_limit):
|
||||
self.message = self.message_template.format(sending_limit)
|
||||
@@ -20,7 +20,7 @@ class TooManyRequestsError(InvalidRequest):
|
||||
|
||||
class TotalRequestsError(InvalidRequest):
|
||||
status_code = 429
|
||||
message_template = 'Exceeded total application limits ({}) for today'
|
||||
message_template = "Exceeded total application limits ({}) for today"
|
||||
|
||||
def __init__(self, sending_limit):
|
||||
self.message = self.message_template.format(sending_limit)
|
||||
@@ -28,15 +28,19 @@ class TotalRequestsError(InvalidRequest):
|
||||
|
||||
class RateLimitError(InvalidRequest):
|
||||
status_code = 429
|
||||
message_template = 'Exceeded rate limit for key type {} of {} requests per {} seconds'
|
||||
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 == 'normal':
|
||||
key_type = 'live'
|
||||
if key_type == "normal":
|
||||
key_type = "live"
|
||||
|
||||
self.message = self.message_template.format(key_type.upper(), sending_limit, interval)
|
||||
self.message = self.message_template.format(
|
||||
key_type.upper(), sending_limit, interval
|
||||
)
|
||||
|
||||
|
||||
class BadRequestError(InvalidRequest):
|
||||
@@ -63,8 +67,13 @@ def register_errors(blueprint):
|
||||
# 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
|
||||
return (
|
||||
jsonify(
|
||||
status_code=400,
|
||||
errors=[{"error": error.__class__.__name__, "message": str(error)}],
|
||||
),
|
||||
400,
|
||||
)
|
||||
|
||||
@blueprint.errorhandler(InvalidRequest)
|
||||
def invalid_data(error):
|
||||
@@ -81,16 +90,35 @@ def register_errors(blueprint):
|
||||
@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
|
||||
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))
|
||||
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
|
||||
return (
|
||||
jsonify(
|
||||
status_code=500,
|
||||
errors=[
|
||||
{
|
||||
"error": error.__class__.__name__,
|
||||
"message": "Internal server error",
|
||||
}
|
||||
],
|
||||
),
|
||||
500,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,8 @@ 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')
|
||||
v2_inbound_sms_blueprint = Blueprint(
|
||||
"v2_inbound_sms", __name__, url_prefix="/v2/received-text-messages"
|
||||
)
|
||||
|
||||
register_errors(v2_inbound_sms_blueprint)
|
||||
|
||||
@@ -7,32 +7,37 @@ 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'])
|
||||
@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')
|
||||
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
|
||||
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(
|
||||
"current": url_for(
|
||||
"v2_inbound_sms.get_inbound_sms",
|
||||
_external=True,
|
||||
),
|
||||
}
|
||||
|
||||
if inbound_sms_list:
|
||||
_links['next'] = url_for(
|
||||
_links["next"] = url_for(
|
||||
"v2_inbound_sms.get_inbound_sms",
|
||||
older_than=inbound_sms_list[-1].id,
|
||||
_external=True,
|
||||
|
||||
@@ -21,7 +21,7 @@ get_inbound_sms_single_response = {
|
||||
"created_at": {
|
||||
"format": "date-time",
|
||||
"type": "string",
|
||||
"description": "Date+time created at"
|
||||
"description": "Date+time created at",
|
||||
},
|
||||
"service_id": uuid,
|
||||
"id": uuid,
|
||||
@@ -29,8 +29,12 @@ get_inbound_sms_single_response = {
|
||||
"content": {"type": "string"},
|
||||
},
|
||||
"required": [
|
||||
"id", "user_number", "created_at", "service_id",
|
||||
"notify_number", "content"
|
||||
"id",
|
||||
"user_number",
|
||||
"created_at",
|
||||
"service_id",
|
||||
"notify_number",
|
||||
"content",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
@@ -42,28 +46,16 @@ get_inbound_sms_response = {
|
||||
"properties": {
|
||||
"received_text_messages": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"$ref": "#/definitions/inbound_sms"
|
||||
}
|
||||
"items": {"type": "object", "$ref": "#/definitions/inbound_sms"},
|
||||
},
|
||||
"links": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"current": {
|
||||
"type": "string"
|
||||
},
|
||||
"next": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"properties": {"current": {"type": "string"}, "next": {"type": "string"}},
|
||||
"additionalProperties": False,
|
||||
"required": ["current"]
|
||||
}
|
||||
"required": ["current"],
|
||||
},
|
||||
},
|
||||
"required": ["received_text_messages", "links"],
|
||||
"definitions": {
|
||||
"inbound_sms": get_inbound_sms_single_response
|
||||
},
|
||||
"definitions": {"inbound_sms": get_inbound_sms_single_response},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ from flask import Blueprint
|
||||
|
||||
from app.v2.errors import register_errors
|
||||
|
||||
v2_notification_blueprint = Blueprint("v2_notifications", __name__, url_prefix='/v2/notifications')
|
||||
v2_notification_blueprint = Blueprint(
|
||||
"v2_notifications", __name__, url_prefix="/v2/notifications"
|
||||
)
|
||||
|
||||
register_errors(v2_notification_blueprint)
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
|
||||
|
||||
def create_post_sms_response_from_notification(
|
||||
notification_id, client_reference, template_id, template_version, service_id,
|
||||
content, from_number, url_root
|
||||
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
|
||||
notification_id,
|
||||
client_reference,
|
||||
template_id,
|
||||
template_version,
|
||||
service_id,
|
||||
url_root,
|
||||
)
|
||||
resp['content'] = {
|
||||
'from_number': from_number,
|
||||
'body': content
|
||||
}
|
||||
resp["content"] = {"from_number": from_number, "body": content}
|
||||
return resp
|
||||
|
||||
|
||||
@@ -26,31 +32,35 @@ def create_post_email_response_from_notification(
|
||||
url_root,
|
||||
):
|
||||
resp = __create_notification_response(
|
||||
notification_id, client_reference, template_id, template_version, service_id, url_root
|
||||
notification_id,
|
||||
client_reference,
|
||||
template_id,
|
||||
template_version,
|
||||
service_id,
|
||||
url_root,
|
||||
)
|
||||
resp['content'] = {
|
||||
"from_email": email_from,
|
||||
"body": content,
|
||||
"subject": subject
|
||||
}
|
||||
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
|
||||
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': {
|
||||
"template": {
|
||||
"id": template_id,
|
||||
"version": template_version,
|
||||
"uri": "{}services/{}/templates/{}".format(
|
||||
url_root,
|
||||
str(service_id),
|
||||
str(template_id)
|
||||
)
|
||||
url_root, str(service_id), str(template_id)
|
||||
),
|
||||
},
|
||||
"scheduled_for": None
|
||||
"scheduled_for": None,
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ from app.v2.notifications.notification_schemas import (
|
||||
)
|
||||
|
||||
|
||||
@v2_notification_blueprint.route("/<notification_id>", methods=['GET'])
|
||||
@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)
|
||||
@@ -20,20 +20,20 @@ def get_notification_by_id(notification_id):
|
||||
return jsonify(notification.serialize()), 200
|
||||
|
||||
|
||||
@v2_notification_blueprint.route("", methods=['GET'])
|
||||
@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]
|
||||
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 "reference" in _data:
|
||||
_data["reference"] = _data["reference"][0]
|
||||
|
||||
if 'include_jobs' in _data:
|
||||
_data['include_jobs'] = _data['include_jobs'][0]
|
||||
if "include_jobs" in _data:
|
||||
_data["include_jobs"] = _data["include_jobs"][0]
|
||||
|
||||
data = validate(_data, get_notifications_request)
|
||||
|
||||
@@ -42,25 +42,33 @@ def get_notifications():
|
||||
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
|
||||
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,
|
||||
)
|
||||
|
||||
def _build_links(notifications):
|
||||
_links = {
|
||||
'current': url_for(".get_notifications", _external=True, **data),
|
||||
"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)
|
||||
_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
|
||||
return (
|
||||
jsonify(
|
||||
notifications=[
|
||||
notification.serialize()
|
||||
for notification in paginated_notifications.items
|
||||
],
|
||||
links=_build_links(paginated_notifications.items),
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
||||
@@ -9,9 +9,9 @@ template = {
|
||||
"properties": {
|
||||
"id": uuid,
|
||||
"version": {"type": "integer"},
|
||||
"uri": {"type": "string", "format": "uri"}
|
||||
"uri": {"type": "string", "format": "uri"},
|
||||
},
|
||||
"required": ["id", "version", "uri"]
|
||||
"required": ["id", "version", "uri"],
|
||||
}
|
||||
|
||||
notification_by_id = {
|
||||
@@ -19,10 +19,8 @@ notification_by_id = {
|
||||
"description": "GET notification response schema",
|
||||
"type": "object",
|
||||
"title": "response v2/notification",
|
||||
"properties": {
|
||||
"notification_id": uuid
|
||||
},
|
||||
"required": ["notification_id"]
|
||||
"properties": {"notification_id": uuid},
|
||||
"required": ["notification_id"],
|
||||
}
|
||||
|
||||
|
||||
@@ -51,14 +49,29 @@ get_notification_response = {
|
||||
"created_at": {"type": "string"},
|
||||
"sent_at": {"type": ["string", "null"]},
|
||||
"completed_at": {"type": ["string", "null"]},
|
||||
"scheduled_for": {"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"
|
||||
]
|
||||
"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 = {
|
||||
@@ -67,20 +80,10 @@ get_notifications_request = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"reference": {"type": "string"},
|
||||
"status": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"enum": NOTIFICATION_STATUS_TYPES
|
||||
}
|
||||
},
|
||||
"template_type": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"enum": NOTIFICATION_TYPES
|
||||
}
|
||||
},
|
||||
"status": {"type": "array", "items": {"enum": NOTIFICATION_STATUS_TYPES}},
|
||||
"template_type": {"type": "array", "items": {"enum": NOTIFICATION_TYPES}},
|
||||
"include_jobs": {"enum": ["true", "True"]},
|
||||
"older_than": uuid
|
||||
"older_than": uuid,
|
||||
},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
@@ -92,31 +95,18 @@ get_notifications_response = {
|
||||
"properties": {
|
||||
"notifications": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"$ref": "#/definitions/notification"
|
||||
}
|
||||
"items": {"type": "object", "$ref": "#/definitions/notification"},
|
||||
},
|
||||
"links": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"current": {
|
||||
"type": "string"
|
||||
},
|
||||
"next": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"properties": {"current": {"type": "string"}, "next": {"type": "string"}},
|
||||
"additionalProperties": False,
|
||||
"required": ["current"]
|
||||
}
|
||||
"required": ["current"],
|
||||
},
|
||||
},
|
||||
"additionalProperties": False,
|
||||
"required": ["notifications", "links"],
|
||||
"definitions": {
|
||||
"notification": get_notification_response
|
||||
},
|
||||
|
||||
"definitions": {"notification": get_notification_response},
|
||||
}
|
||||
|
||||
post_sms_request = {
|
||||
@@ -129,11 +119,14 @@ post_sms_request = {
|
||||
"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
|
||||
"scheduled_for": {
|
||||
"type": ["string", "null"],
|
||||
"format": "datetime_within_next_day",
|
||||
},
|
||||
"sms_sender_id": uuid,
|
||||
},
|
||||
"required": ["phone_number", "template_id"],
|
||||
"additionalProperties": False
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
sms_content = {
|
||||
@@ -141,11 +134,8 @@ sms_content = {
|
||||
"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"]
|
||||
"properties": {"body": {"type": "string"}, "from_number": {"type": "string"}},
|
||||
"required": ["body", "from_number"],
|
||||
}
|
||||
|
||||
post_sms_response = {
|
||||
@@ -159,9 +149,9 @@ post_sms_response = {
|
||||
"content": sms_content,
|
||||
"uri": {"type": "string", "format": "uri"},
|
||||
"template": template,
|
||||
"scheduled_for": {"type": ["string", "null"]}
|
||||
"scheduled_for": {"type": ["string", "null"]},
|
||||
},
|
||||
"required": ["id", "content", "uri", "template"]
|
||||
"required": ["id", "content", "uri", "template"],
|
||||
}
|
||||
|
||||
|
||||
@@ -175,11 +165,14 @@ post_email_request = {
|
||||
"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
|
||||
"scheduled_for": {
|
||||
"type": ["string", "null"],
|
||||
"format": "datetime_within_next_day",
|
||||
},
|
||||
"email_reply_to_id": uuid,
|
||||
},
|
||||
"required": ["email_address", "template_id"],
|
||||
"additionalProperties": False
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
email_content = {
|
||||
@@ -190,9 +183,9 @@ email_content = {
|
||||
"properties": {
|
||||
"from_email": {"type": "string", "format": "email_address"},
|
||||
"body": {"type": "string"},
|
||||
"subject": {"type": "string"}
|
||||
"subject": {"type": "string"},
|
||||
},
|
||||
"required": ["body", "from_email", "subject"]
|
||||
"required": ["body", "from_email", "subject"],
|
||||
}
|
||||
|
||||
post_email_response = {
|
||||
@@ -206,7 +199,7 @@ post_email_response = {
|
||||
"content": email_content,
|
||||
"uri": {"type": "string", "format": "uri"},
|
||||
"template": template,
|
||||
"scheduled_for": {"type": ["string", "null"]}
|
||||
"scheduled_for": {"type": ["string", "null"]},
|
||||
},
|
||||
"required": ["id", "content", "uri", "template"]
|
||||
"required": ["id", "content", "uri", "template"],
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ from app.v2.notifications.notification_schemas import (
|
||||
from app.v2.utils import get_valid_json
|
||||
|
||||
|
||||
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
|
||||
@v2_notification_blueprint.route("/<notification_type>", methods=["POST"])
|
||||
def post_notification(notification_type):
|
||||
request_json = get_valid_json()
|
||||
|
||||
@@ -69,11 +69,11 @@ def post_notification(notification_type):
|
||||
check_rate_limiting(authenticated_service, api_user)
|
||||
|
||||
template, template_with_content = validate_template(
|
||||
form['template_id'],
|
||||
form.get('personalisation', {}),
|
||||
form["template_id"],
|
||||
form.get("personalisation", {}),
|
||||
authenticated_service,
|
||||
notification_type,
|
||||
check_char_count=False
|
||||
check_char_count=False,
|
||||
)
|
||||
|
||||
reply_to = get_reply_to_text(notification_type, form, template)
|
||||
@@ -85,7 +85,7 @@ def post_notification(notification_type):
|
||||
template_with_content=template_with_content,
|
||||
template_process_type=template.process_type,
|
||||
service=authenticated_service,
|
||||
reply_to_text=reply_to
|
||||
reply_to_text=reply_to,
|
||||
)
|
||||
|
||||
return jsonify(notification), 201
|
||||
@@ -102,20 +102,24 @@ def process_sms_or_email_notification(
|
||||
reply_to_text=None,
|
||||
):
|
||||
notification_id = uuid.uuid4()
|
||||
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
|
||||
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_user.key_type,
|
||||
service=service,
|
||||
notification_type=notification_type)
|
||||
send_to = validate_and_format_recipient(
|
||||
send_to=form_send_to,
|
||||
key_type=api_user.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)
|
||||
|
||||
personalisation, document_download_count = process_document_uploads(
|
||||
form.get('personalisation'),
|
||||
service,
|
||||
simulated=simulated
|
||||
form.get("personalisation"), service, simulated=simulated
|
||||
)
|
||||
if document_download_count:
|
||||
# We changed personalisation which means we need to update the content
|
||||
@@ -126,18 +130,20 @@ def process_sms_or_email_notification(
|
||||
|
||||
resp = create_response_for_post_notification(
|
||||
notification_id=notification_id,
|
||||
client_reference=form.get('reference', None),
|
||||
client_reference=form.get("reference", None),
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
service_id=service.id,
|
||||
notification_type=notification_type,
|
||||
reply_to=reply_to_text,
|
||||
template_with_content=template_with_content
|
||||
template_with_content=template_with_content,
|
||||
)
|
||||
|
||||
if service.high_volume \
|
||||
and api_user.key_type == KEY_TYPE_NORMAL \
|
||||
and notification_type in [EMAIL_TYPE, SMS_TYPE]:
|
||||
if (
|
||||
service.high_volume
|
||||
and api_user.key_type == KEY_TYPE_NORMAL
|
||||
and notification_type in [EMAIL_TYPE, SMS_TYPE]
|
||||
):
|
||||
# Put service with high volumes of notifications onto a queue
|
||||
# To take the pressure off the db for API requests put the notification for our high volume service onto a queue
|
||||
# the task will then save the notification, then call send_notification_to_queue.
|
||||
@@ -153,7 +159,7 @@ def process_sms_or_email_notification(
|
||||
service_id=service.id,
|
||||
personalisation=personalisation,
|
||||
document_download_count=document_download_count,
|
||||
reply_to_text=reply_to_text
|
||||
reply_to_text=reply_to_text,
|
||||
)
|
||||
return resp
|
||||
except (botocore.exceptions.ClientError, botocore.parsers.ResponseParserError):
|
||||
@@ -162,7 +168,7 @@ def process_sms_or_email_notification(
|
||||
# the exception we get here isn't handled correctly by botocore - we get a ResponseParserError instead.
|
||||
# Hopefully this is no longer an issue with Redis as celery's backing store
|
||||
current_app.logger.info(
|
||||
f'Notification {notification_id} failed to save to high volume queue. Using normal flow instead'
|
||||
f"Notification {notification_id} failed to save to high volume queue. Using normal flow instead"
|
||||
)
|
||||
|
||||
persist_notification(
|
||||
@@ -175,10 +181,10 @@ def process_sms_or_email_notification(
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type,
|
||||
client_reference=form.get('reference', None),
|
||||
client_reference=form.get("reference", None),
|
||||
simulated=simulated,
|
||||
reply_to_text=reply_to_text,
|
||||
document_download_count=document_download_count
|
||||
document_download_count=document_download_count,
|
||||
)
|
||||
|
||||
if not simulated:
|
||||
@@ -188,10 +194,12 @@ def process_sms_or_email_notification(
|
||||
notification_type=notification_type,
|
||||
notification_id=notification_id,
|
||||
research_mode=service.research_mode, # research_mode is deprecated
|
||||
queue=queue_name
|
||||
queue=queue_name,
|
||||
)
|
||||
else:
|
||||
current_app.logger.debug("POST simulated notification for id: {}".format(notification_id))
|
||||
current_app.logger.debug(
|
||||
"POST simulated notification for id: {}".format(notification_id)
|
||||
)
|
||||
|
||||
return resp
|
||||
|
||||
@@ -206,27 +214,27 @@ def save_email_or_sms_to_queue(
|
||||
service_id,
|
||||
personalisation,
|
||||
document_download_count,
|
||||
reply_to_text=None
|
||||
reply_to_text=None,
|
||||
):
|
||||
data = {
|
||||
"id": notification_id,
|
||||
"template_id": str(template.id),
|
||||
"template_version": template.version,
|
||||
"to": form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number'],
|
||||
"to": form["email_address"]
|
||||
if notification_type == EMAIL_TYPE
|
||||
else form["phone_number"],
|
||||
"service_id": str(service_id),
|
||||
"personalisation": personalisation,
|
||||
"notification_type": notification_type,
|
||||
"api_key_id": str(api_key.id),
|
||||
"key_type": api_key.key_type,
|
||||
"client_reference": form.get('reference', None),
|
||||
"client_reference": form.get("reference", None),
|
||||
"reply_to_text": reply_to_text,
|
||||
"document_download_count": document_download_count,
|
||||
"status": NOTIFICATION_CREATED,
|
||||
"created_at": datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
}
|
||||
encrypted = encryption.encrypt(
|
||||
data
|
||||
)
|
||||
encrypted = encryption.encrypt(data)
|
||||
|
||||
if notification_type == EMAIL_TYPE:
|
||||
save_api_email.apply_async([encrypted], queue=QueueNames.SAVE_API_EMAIL)
|
||||
@@ -241,7 +249,11 @@ def process_document_uploads(personalisation_data, service, simulated=False):
|
||||
Returns modified personalisation dict and a count of document uploads. If there are no document uploads, returns
|
||||
a count of `None` rather than `0`.
|
||||
"""
|
||||
file_keys = [k for k, v in (personalisation_data or {}).items() if isinstance(v, dict) and 'file' in v]
|
||||
file_keys = [
|
||||
k
|
||||
for k, v in (personalisation_data or {}).items()
|
||||
if isinstance(v, dict) and "file" in v
|
||||
]
|
||||
if not file_keys:
|
||||
return personalisation_data, None
|
||||
|
||||
@@ -249,16 +261,20 @@ def process_document_uploads(personalisation_data, service, simulated=False):
|
||||
|
||||
check_if_service_can_send_files_by_email(
|
||||
service_contact_link=authenticated_service.contact_link,
|
||||
service_id=authenticated_service.id
|
||||
service_id=authenticated_service.id,
|
||||
)
|
||||
|
||||
for key in file_keys:
|
||||
if simulated:
|
||||
personalisation_data[key] = document_download_client.get_upload_url(service.id) + '/test-document'
|
||||
personalisation_data[key] = (
|
||||
document_download_client.get_upload_url(service.id) + "/test-document"
|
||||
)
|
||||
else:
|
||||
try:
|
||||
personalisation_data[key] = document_download_client.upload_document(
|
||||
service.id, personalisation_data[key]['file'], personalisation_data[key].get('is_csv')
|
||||
service.id,
|
||||
personalisation_data[key]["file"],
|
||||
personalisation_data[key].get("is_csv"),
|
||||
)
|
||||
except DocumentDownloadError as e:
|
||||
raise BadRequestError(message=e.message, status_code=e.status_code)
|
||||
@@ -270,9 +286,14 @@ def get_reply_to_text(notification_type, form, template):
|
||||
reply_to = None
|
||||
if notification_type == EMAIL_TYPE:
|
||||
service_email_reply_to_id = form.get("email_reply_to_id", None)
|
||||
reply_to = check_service_email_reply_to_id(
|
||||
str(authenticated_service.id), service_email_reply_to_id, notification_type
|
||||
) or template.reply_to_text
|
||||
reply_to = (
|
||||
check_service_email_reply_to_id(
|
||||
str(authenticated_service.id),
|
||||
service_email_reply_to_id,
|
||||
notification_type,
|
||||
)
|
||||
or template.reply_to_text
|
||||
)
|
||||
|
||||
elif notification_type == SMS_TYPE:
|
||||
service_sms_sender_id = form.get("sms_sender_id", None)
|
||||
@@ -295,7 +316,7 @@ def create_response_for_post_notification(
|
||||
service_id,
|
||||
notification_type,
|
||||
reply_to,
|
||||
template_with_content
|
||||
template_with_content,
|
||||
):
|
||||
if notification_type == SMS_TYPE:
|
||||
create_resp_partial = functools.partial(
|
||||
@@ -306,10 +327,17 @@ def create_response_for_post_notification(
|
||||
create_resp_partial = functools.partial(
|
||||
create_post_email_response_from_notification,
|
||||
subject=template_with_content.subject,
|
||||
email_from='{}@{}'.format(authenticated_service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
||||
email_from="{}@{}".format(
|
||||
authenticated_service.email_from,
|
||||
current_app.config["NOTIFY_EMAIL_DOMAIN"],
|
||||
),
|
||||
)
|
||||
resp = create_resp_partial(
|
||||
notification_id, client_reference, template_id, template_version, service_id,
|
||||
notification_id,
|
||||
client_reference,
|
||||
template_id,
|
||||
template_version,
|
||||
service_id,
|
||||
url_root=request.url_root,
|
||||
content=template_with_content.content_with_placeholders_filled_in,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,6 @@ from flask import Blueprint
|
||||
|
||||
from app.v2.errors import register_errors
|
||||
|
||||
v2_template_blueprint = Blueprint("v2_template", __name__, url_prefix='/v2/template')
|
||||
v2_template_blueprint = Blueprint("v2_template", __name__, url_prefix="/v2/template")
|
||||
|
||||
register_errors(v2_template_blueprint)
|
||||
|
||||
@@ -7,15 +7,16 @@ 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'])
|
||||
@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}
|
||||
_data = {"id": template_id}
|
||||
if version:
|
||||
_data['version'] = 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'))
|
||||
template_id, authenticated_service.id, data.get("version")
|
||||
)
|
||||
return jsonify(template.serialize_for_v2()), 200
|
||||
|
||||
@@ -12,7 +12,7 @@ from app.v2.template.template_schemas import (
|
||||
from app.v2.utils import get_valid_json
|
||||
|
||||
|
||||
@v2_template_blueprint.route("/<template_id>/preview", methods=['POST'])
|
||||
@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)
|
||||
@@ -21,26 +21,30 @@ def post_template_preview(template_id):
|
||||
else:
|
||||
_data = get_valid_json()
|
||||
|
||||
_data['id'] = template_id
|
||||
_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_id, authenticated_service.id
|
||||
)
|
||||
|
||||
template_object = template._as_utils_template_with_personalisation(
|
||||
data.get('personalisation')
|
||||
data.get("personalisation")
|
||||
)
|
||||
|
||||
check_placeholders(template_object)
|
||||
|
||||
resp = create_post_template_preview_response(template=template,
|
||||
template_object=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}])
|
||||
message = "Missing personalisation: {}".format(
|
||||
", ".join(template_object.missing_data)
|
||||
)
|
||||
raise BadRequestError(message=message, fields=[{"template": message}])
|
||||
|
||||
@@ -5,10 +5,7 @@ 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}
|
||||
},
|
||||
"properties": {"id": uuid, "version": {"type": ["integer", "null"], "minimum": 1}},
|
||||
"required": ["id"],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
@@ -24,12 +21,12 @@ get_template_by_id_response = {
|
||||
"created_at": {
|
||||
"format": "date-time",
|
||||
"type": "string",
|
||||
"description": "Date+time created"
|
||||
"description": "Date+time created",
|
||||
},
|
||||
"updated_at": {
|
||||
"format": "date-time",
|
||||
"type": ["string", "null"],
|
||||
"description": "Date+time updated"
|
||||
"description": "Date+time updated",
|
||||
},
|
||||
"created_by": {"type": "string"},
|
||||
"version": {"type": "integer"},
|
||||
@@ -37,7 +34,16 @@ get_template_by_id_response = {
|
||||
"subject": {"type": ["string", "null"]},
|
||||
"name": {"type": "string"},
|
||||
},
|
||||
"required": ["id", "type", "created_at", "updated_at", "version", "created_by", "body", "name"],
|
||||
"required": [
|
||||
"id",
|
||||
"type",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"version",
|
||||
"created_by",
|
||||
"body",
|
||||
"name",
|
||||
],
|
||||
}
|
||||
|
||||
post_template_preview_request = {
|
||||
@@ -45,11 +51,8 @@ post_template_preview_request = {
|
||||
"description": "POST template schema",
|
||||
"type": "object",
|
||||
"title": "POST v2/template/{id}/preview",
|
||||
"properties": {
|
||||
"id": uuid,
|
||||
"personalisation": personalisation
|
||||
},
|
||||
"required": ["id"]
|
||||
"properties": {"id": uuid, "personalisation": personalisation},
|
||||
"required": ["id"],
|
||||
}
|
||||
|
||||
post_template_preview_response = {
|
||||
@@ -76,6 +79,6 @@ def create_post_template_preview_response(template, template_object):
|
||||
"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),
|
||||
"html": getattr(template_object, "html_body", None),
|
||||
"subject": getattr(template_object, "subject", None),
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ from flask import Blueprint
|
||||
|
||||
from app.v2.errors import register_errors
|
||||
|
||||
v2_templates_blueprint = Blueprint("v2_templates", __name__, url_prefix='/v2/templates')
|
||||
v2_templates_blueprint = Blueprint("v2_templates", __name__, url_prefix="/v2/templates")
|
||||
|
||||
register_errors(v2_templates_blueprint)
|
||||
|
||||
@@ -7,12 +7,15 @@ 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'])
|
||||
@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'))
|
||||
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
|
||||
return (
|
||||
jsonify(templates=[template.serialize_for_v2() for template in templates]),
|
||||
200,
|
||||
)
|
||||
|
||||
@@ -7,9 +7,7 @@ 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": TEMPLATE_TYPES}
|
||||
},
|
||||
"properties": {"type": {"enum": TEMPLATE_TYPES}},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
@@ -20,14 +18,9 @@ get_all_template_response = {
|
||||
"properties": {
|
||||
"templates": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"$ref": "#/definitions/template"
|
||||
}
|
||||
"items": {"type": "object", "$ref": "#/definitions/template"},
|
||||
}
|
||||
},
|
||||
"required": ["templates"],
|
||||
"definitions": {
|
||||
"template": template
|
||||
}
|
||||
"definitions": {"template": template},
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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)
|
||||
raise BadRequestError(
|
||||
message="Invalid JSON supplied in POST data", status_code=400
|
||||
)
|
||||
return request_json or {}
|
||||
|
||||
Reference in New Issue
Block a user