merge from main

This commit is contained in:
Kenneth Kehl
2024-03-01 13:50:09 -08:00
140 changed files with 8031 additions and 5017 deletions

View File

@@ -7,6 +7,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.errors import InvalidRequest
@@ -35,7 +36,7 @@ class RateLimitError(InvalidRequest):
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":
if key_type == KeyType.NORMAL:
key_type = "live"
self.message = self.message_template.format(

View File

@@ -1,4 +1,4 @@
from app.models import NOTIFICATION_STATUS_TYPES, NOTIFICATION_TYPES
from app.enums import NotificationStatus, TemplateType
from app.schema_validation.definitions import personalisation, uuid
template = {
@@ -41,7 +41,7 @@ get_notification_response = {
"line_5": {"type": ["string", "null"]},
"line_6": {"type": ["string", "null"]},
"postcode": {"type": ["string", "null"]},
"type": {"enum": ["sms", "email"]},
"type": {"enum": list(TemplateType)},
"status": {"type": "string"},
"template": template,
"body": {"type": "string"},
@@ -80,8 +80,14 @@ 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": list(NotificationStatus)},
},
"template_type": {
"type": "array",
"items": {"enum": list(TemplateType)},
},
"include_jobs": {"enum": ["true", "True"]},
"older_than": uuid,
},

View File

@@ -10,14 +10,8 @@ from app import api_user, authenticated_service, document_download_client, encry
from app.celery.tasks import save_api_email, save_api_sms
from app.clients.document_download import DocumentDownloadError
from app.config import QueueNames
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
NOTIFICATION_CREATED,
PRIORITY,
SMS_TYPE,
Notification,
)
from app.enums import KeyType, NotificationStatus, NotificationType, TemplateProcessType
from app.models import Notification
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue_detached,
@@ -52,9 +46,9 @@ from app.v2.utils import get_valid_json
def post_notification(notification_type):
request_json = get_valid_json()
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
form = validate(request_json, post_email_request)
elif notification_type == SMS_TYPE:
elif notification_type == NotificationType.SMS:
form = validate(request_json, post_sms_request)
else:
abort(404)
@@ -99,7 +93,7 @@ def process_sms_or_email_notification(
notification_id = uuid.uuid4()
form_send_to = (
form["email_address"]
if notification_type == EMAIL_TYPE
if notification_type == NotificationType.EMAIL
else form["phone_number"]
)
@@ -136,8 +130,8 @@ def process_sms_or_email_notification(
if (
service.high_volume
and api_user.key_type == KEY_TYPE_NORMAL
and notification_type in [EMAIL_TYPE, SMS_TYPE]
and api_user.key_type == KeyType.NORMAL
and notification_type in {NotificationType.EMAIL, NotificationType.SMS}
):
# 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
@@ -183,7 +177,11 @@ def process_sms_or_email_notification(
)
if not simulated:
queue_name = QueueNames.PRIORITY if template_process_type == PRIORITY else None
queue_name = (
QueueNames.PRIORITY
if template_process_type == TemplateProcessType.PRIORITY
else None
)
send_notification_to_queue_detached(
key_type=api_user.key_type,
notification_type=notification_type,
@@ -215,7 +213,7 @@ def save_email_or_sms_to_queue(
"template_id": str(template.id),
"template_version": template.version,
"to": form["email_address"]
if notification_type == EMAIL_TYPE
if notification_type == NotificationType.EMAIL
else form["phone_number"],
"service_id": str(service_id),
"personalisation": personalisation,
@@ -225,14 +223,14 @@ def save_email_or_sms_to_queue(
"client_reference": form.get("reference", None),
"reply_to_text": reply_to_text,
"document_download_count": document_download_count,
"status": NOTIFICATION_CREATED,
"status": NotificationStatus.CREATED,
"created_at": datetime.utcnow().strftime(DATETIME_FORMAT),
}
encrypted = encryption.encrypt(data)
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
save_api_email.apply_async([encrypted], queue=QueueNames.SAVE_API_EMAIL)
elif notification_type == SMS_TYPE:
elif notification_type == NotificationType.SMS:
save_api_sms.apply_async([encrypted], queue=QueueNames.SAVE_API_SMS)
return Notification(**data)
@@ -278,7 +276,7 @@ def process_document_uploads(personalisation_data, service, simulated=False):
def get_reply_to_text(notification_type, form, template):
reply_to = None
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
service_email_reply_to_id = form.get("email_reply_to_id", None)
reply_to = (
check_service_email_reply_to_id(
@@ -289,7 +287,7 @@ def get_reply_to_text(notification_type, form, template):
or template.reply_to_text
)
elif notification_type == SMS_TYPE:
elif notification_type == NotificationType.SMS:
service_sms_sender_id = form.get("sms_sender_id", None)
sms_sender_id = check_service_sms_sender_id(
str(authenticated_service.id), service_sms_sender_id, notification_type
@@ -312,12 +310,12 @@ def create_response_for_post_notification(
reply_to,
template_with_content,
):
if notification_type == SMS_TYPE:
if notification_type == NotificationType.SMS:
create_resp_partial = functools.partial(
create_post_sms_response_from_notification,
from_number=reply_to,
)
elif notification_type == EMAIL_TYPE:
elif notification_type == NotificationType.EMAIL:
create_resp_partial = functools.partial(
create_post_email_response_from_notification,
subject=template_with_content.subject,

View File

@@ -1,4 +1,4 @@
from app.models import TEMPLATE_TYPES
from app.enums import TemplateType
from app.schema_validation.definitions import personalisation, uuid
get_template_by_id_request = {
@@ -17,7 +17,7 @@ get_template_by_id_response = {
"title": "reponse v2/template",
"properties": {
"id": uuid,
"type": {"enum": TEMPLATE_TYPES},
"type": {"enum": list(TemplateType)},
"created_at": {
"format": "date-time",
"type": "string",
@@ -62,7 +62,7 @@ post_template_preview_response = {
"title": "reponse v2/template/{id}/preview",
"properties": {
"id": uuid,
"type": {"enum": TEMPLATE_TYPES},
"type": {"enum": list(TemplateType)},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]},

View File

@@ -1,11 +1,11 @@
from app.models import TEMPLATE_TYPES
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": TEMPLATE_TYPES}},
"properties": {"type": {"enum": list(TemplateType)}},
"additionalProperties": False,
}