mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Cleaning up a lot of things, getting Enums used everywhere.
Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
@@ -17,17 +17,16 @@ from app.dao.notifications_dao import (
|
||||
dao_delete_notifications_by_id,
|
||||
)
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_CREATED,
|
||||
SMS_TYPE,
|
||||
Notification,
|
||||
NotificationType,
|
||||
)
|
||||
from app.v2.errors import BadRequestError
|
||||
|
||||
|
||||
def create_content_for_notification(template, personalisation):
|
||||
if template.template_type == EMAIL_TYPE:
|
||||
if template.template_type == NotificationType.EMAIL:
|
||||
template_object = PlainTextEmailTemplate(
|
||||
{
|
||||
"content": template.content,
|
||||
@@ -36,7 +35,7 @@ def create_content_for_notification(template, personalisation):
|
||||
},
|
||||
personalisation,
|
||||
)
|
||||
if template.template_type == SMS_TYPE:
|
||||
if template.template_type == NotificationType.SMS:
|
||||
template_object = SMSMessageTemplate(
|
||||
{
|
||||
"content": template.content,
|
||||
@@ -113,7 +112,7 @@ def persist_notification(
|
||||
updated_at=updated_at,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
formatted_recipient = validate_and_format_phone_number(
|
||||
recipient, international=True
|
||||
)
|
||||
@@ -122,8 +121,8 @@ def persist_notification(
|
||||
notification.international = recipient_info.international
|
||||
notification.phone_prefix = recipient_info.country_prefix
|
||||
notification.rate_multiplier = recipient_info.billable_units
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
current_app.logger.info(f"Persisting notification with type: {EMAIL_TYPE}")
|
||||
elif notification_type == NotificationType.EMAIL:
|
||||
current_app.logger.info(f"Persisting notification with type: {NotificationType.EMAIL}")
|
||||
redis_store.set(
|
||||
f"email-address-{notification.id}",
|
||||
format_email_address(notification.to),
|
||||
@@ -153,11 +152,11 @@ def send_notification_to_queue_detached(
|
||||
if key_type == KEY_TYPE_TEST:
|
||||
print("send_notification_to_queue_detached key is test key")
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
if not queue:
|
||||
queue = QueueNames.SEND_SMS
|
||||
deliver_task = provider_tasks.deliver_sms
|
||||
if notification_type == EMAIL_TYPE:
|
||||
if notification_type == NotificationType.EMAIL:
|
||||
if not queue:
|
||||
queue = QueueNames.SEND_EMAIL
|
||||
deliver_task = provider_tasks.deliver_email
|
||||
@@ -183,7 +182,7 @@ def send_notification_to_queue(notification, queue=None):
|
||||
|
||||
|
||||
def simulated_recipient(to_address, notification_type):
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
formatted_simulated_numbers = [
|
||||
validate_and_format_phone_number(number)
|
||||
for number in current_app.config["SIMULATED_SMS_NUMBERS"]
|
||||
|
||||
@@ -6,7 +6,7 @@ from app.config import QueueNames
|
||||
from app.dao.inbound_sms_dao import dao_create_inbound_sms
|
||||
from app.dao.services_dao import dao_fetch_service_by_inbound_number
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import INBOUND_SMS_TYPE, SMS_TYPE, InboundSms
|
||||
from app.models import InboundSms, ServicePermissionType
|
||||
from app.notifications.sns_handlers import sns_notification_handler
|
||||
|
||||
receive_notifications_blueprint = Blueprint("receive_notifications", __name__)
|
||||
@@ -125,4 +125,4 @@ def fetch_potential_service(inbound_number, provider_name):
|
||||
|
||||
def has_inbound_sms_permissions(permissions):
|
||||
str_permissions = [p.permission for p in permissions]
|
||||
return set([INBOUND_SMS_TYPE, SMS_TYPE]).issubset(set(str_permissions))
|
||||
return {ServicePermissionType.INBOUND_SMS, ServicePermissionType.SMS}.issubset(set(str_permissions))
|
||||
|
||||
@@ -5,7 +5,7 @@ from app import api_user, authenticated_service
|
||||
from app.config import QueueNames
|
||||
from app.dao import notifications_dao
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_TEAM, PRIORITY, SMS_TYPE
|
||||
from app.models import KEY_TYPE_TEAM, PRIORITY, NotificationType
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
@@ -84,13 +84,13 @@ def get_all_notifications():
|
||||
|
||||
@notifications.route("/notifications/<string:notification_type>", methods=["POST"])
|
||||
def send_notification(notification_type):
|
||||
if notification_type not in [SMS_TYPE, EMAIL_TYPE]:
|
||||
msg = "{} notification type is not supported".format(notification_type)
|
||||
if notification_type not in {NotificationType.SMS, NotificationType.EMAIL}:
|
||||
msg = f"{notification_type} notification type is not supported"
|
||||
raise InvalidRequest(msg, 400)
|
||||
|
||||
notification_form = (
|
||||
sms_template_notification_schema
|
||||
if notification_type == SMS_TYPE
|
||||
if notification_type == NotificationType.SMS
|
||||
else email_notification_schema
|
||||
).load(request.get_json())
|
||||
|
||||
@@ -116,7 +116,7 @@ def send_notification(notification_type):
|
||||
status_code=400,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
check_if_service_can_send_to_number(
|
||||
authenticated_service, notification_form["to"]
|
||||
)
|
||||
@@ -191,7 +191,7 @@ def create_template_object_for_notification(template, personalisation):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if (
|
||||
template_object.template_type == SMS_TYPE
|
||||
template_object.template_type == NotificationType.SMS
|
||||
and template_object.is_message_too_long()
|
||||
):
|
||||
message = "Content has a character count greater than the limit of {}".format(
|
||||
|
||||
@@ -16,12 +16,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.models import (
|
||||
EMAIL_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
NotificationType,
|
||||
ServicePermission,
|
||||
ServicePermissionType,
|
||||
TemplateType,
|
||||
)
|
||||
from app.notifications.process_notifications import create_content_for_notification
|
||||
from app.serialised_models import SerialisedTemplate
|
||||
@@ -151,13 +151,13 @@ def validate_and_format_recipient(
|
||||
send_to, key_type, service, allow_guest_list_recipients
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
international_phone_info = check_if_service_can_send_to_number(service, send_to)
|
||||
|
||||
return validate_and_format_phone_number(
|
||||
number=send_to, international=international_phone_info.international
|
||||
)
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
elif notification_type == NotificationType.EMAIL:
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ def check_if_service_can_send_to_number(service, number):
|
||||
|
||||
if (
|
||||
international_phone_info.international
|
||||
and INTERNATIONAL_SMS_TYPE not in permissions
|
||||
and ServicePermissionType.INTERNATIONAL_SMS not in permissions
|
||||
):
|
||||
raise BadRequestError(message="Cannot send to international mobile numbers")
|
||||
else:
|
||||
@@ -181,12 +181,12 @@ def check_if_service_can_send_to_number(service, number):
|
||||
def check_is_message_too_long(template_with_content):
|
||||
if template_with_content.is_message_too_long():
|
||||
message = "Your message is too long. "
|
||||
if template_with_content.template_type == SMS_TYPE:
|
||||
if template_with_content.template_type == TemplateType.SMS:
|
||||
message += (
|
||||
f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. "
|
||||
f"Your message is {template_with_content.content_count_without_prefix} characters long."
|
||||
)
|
||||
elif template_with_content.template_type == EMAIL_TYPE:
|
||||
elif template_with_content.template_type == TemplateType.EMAIL:
|
||||
message += (
|
||||
f"Emails cannot be longer than 2000000 bytes. "
|
||||
f"Your message is {template_with_content.content_size_in_bytes} bytes."
|
||||
@@ -226,9 +226,9 @@ def validate_template(
|
||||
|
||||
|
||||
def check_reply_to(service_id, reply_to_id, type_):
|
||||
if type_ == EMAIL_TYPE:
|
||||
if type_ == NotificationType.EMAIL:
|
||||
return check_service_email_reply_to_id(service_id, reply_to_id, type_)
|
||||
elif type_ == SMS_TYPE:
|
||||
elif type_ == NotificationType.SMS:
|
||||
return check_service_sms_sender_id(service_id, reply_to_id, type_)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user