KeyType implemented.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-28 12:43:33 -05:00
parent db3761609b
commit 7416de2a28
20 changed files with 90 additions and 109 deletions
+7 -8
View File
@@ -20,8 +20,7 @@ from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service 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.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.dao.templates_dao import dao_get_template_by_id
from app.enums import NotificationType, JobStatus from app.enums import NotificationType, JobStatus, KeyType
from app.models import KEY_TYPE_NORMAL
from app.notifications.process_notifications import persist_notification from app.notifications.process_notifications import persist_notification
from app.notifications.validators import check_service_over_total_message_limit from app.notifications.validators import check_service_over_total_message_limit
from app.serialised_models import SerialisedService, SerialisedTemplate from app.serialised_models import SerialisedService, SerialisedTemplate
@@ -145,7 +144,7 @@ def process_row(row, template, job, service, sender_id=None):
def __total_sending_limits_for_job_exceeded(service, job, job_id): def __total_sending_limits_for_job_exceeded(service, job, job_id):
try: try:
total_sent = check_service_over_total_message_limit(KEY_TYPE_NORMAL, service) total_sent = check_service_over_total_message_limit(KeyType.NORMAL, service)
if total_sent + job.notification_count > service.total_message_limit: if total_sent + job.notification_count > service.total_message_limit:
raise TotalRequestsError(service.total_message_limit) raise TotalRequestsError(service.total_message_limit)
else: else:
@@ -179,7 +178,7 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
else: else:
reply_to_text = template.reply_to_text reply_to_text = template.reply_to_text
if not service_allowed_to_send_to(notification["to"], service, KEY_TYPE_NORMAL): if not service_allowed_to_send_to(notification["to"], service, KeyType.NORMAL):
current_app.logger.debug( current_app.logger.debug(
"SMS {} failed as restricted service".format(notification_id) "SMS {} failed as restricted service".format(notification_id)
) )
@@ -200,7 +199,7 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
personalisation=notification.get("personalisation"), personalisation=notification.get("personalisation"),
notification_type=NotificationType.SMS, notification_type=NotificationType.SMS,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
created_at=datetime.utcnow(), created_at=datetime.utcnow(),
created_by_id=created_by_id, created_by_id=created_by_id,
job_id=notification.get("job", None), job_id=notification.get("job", None),
@@ -245,7 +244,7 @@ def save_email(
else: else:
reply_to_text = template.reply_to_text reply_to_text = template.reply_to_text
if not service_allowed_to_send_to(notification["to"], service, KEY_TYPE_NORMAL): if not service_allowed_to_send_to(notification["to"], service, KeyType.NORMAL):
current_app.logger.info( current_app.logger.info(
"Email {} failed as restricted service".format(notification_id) "Email {} failed as restricted service".format(notification_id)
) )
@@ -260,7 +259,7 @@ def save_email(
personalisation=notification.get("personalisation"), personalisation=notification.get("personalisation"),
notification_type=NotificationType.EMAIL, notification_type=NotificationType.EMAIL,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
created_at=datetime.utcnow(), created_at=datetime.utcnow(),
job_id=notification.get("job", None), job_id=notification.get("job", None),
job_row_number=notification.get("row_number", None), job_row_number=notification.get("row_number", None),
@@ -319,7 +318,7 @@ def save_api_email_or_sms(self, encrypted_notification):
notification_type=notification["notification_type"], notification_type=notification["notification_type"],
client_reference=notification["client_reference"], client_reference=notification["client_reference"],
api_key_id=notification.get("api_key_id"), api_key_id=notification.get("api_key_id"),
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
created_at=notification["created_at"], created_at=notification["created_at"],
reply_to_text=notification["reply_to_text"], reply_to_text=notification["reply_to_text"],
status=notification["status"], status=notification["status"],
+2 -3
View File
@@ -49,9 +49,8 @@ from app.dao.users_dao import (
delete_user_verify_codes, delete_user_verify_codes,
get_user_by_email, get_user_by_email,
) )
from app.enums import NotificationType, NotificationStatus from app.enums import NotificationType, NotificationStatus, KeyType
from app.models import ( from app.models import (
KEY_TYPE_TEST,
AnnualBilling, AnnualBilling,
Domain, Domain,
EmailBranding, EmailBranding,
@@ -522,7 +521,7 @@ def fix_billable_units():
Notification.status != NotificationStatus.CREATED, Notification.status != NotificationStatus.CREATED,
Notification.sent_at == None, # noqa Notification.sent_at == None, # noqa
Notification.billable_units == 0, Notification.billable_units == 0,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
) )
for notification in query.all(): for notification in query.all():
+3 -5
View File
@@ -8,10 +8,8 @@ from sqlalchemy.sql.expression import case, literal
from app import db from app import db
from app.dao.date_util import get_calendar_year_dates, get_calendar_year_for_datetime from app.dao.date_util import get_calendar_year_dates, get_calendar_year_for_datetime
from app.dao.organization_dao import dao_get_organization_live_services from app.dao.organization_dao import dao_get_organization_live_services
from app.enums import NotificationStatus, NotificationType from app.enums import NotificationStatus, NotificationType, KeyType
from app.models import ( from app.models import (
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
AnnualBilling, AnnualBilling,
FactBilling, FactBilling,
NotificationAllTimeView, NotificationAllTimeView,
@@ -411,7 +409,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
) )
.filter( .filter(
NotificationAllTimeView.status.in_(NotificationStatus.sent_emails), NotificationAllTimeView.status.in_(NotificationStatus.sent_emails),
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)), NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
NotificationAllTimeView.created_at >= start_date, NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date, NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type, NotificationAllTimeView.notification_type == notification_type,
@@ -443,7 +441,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
) )
.filter( .filter(
NotificationAllTimeView.status.in_(NotificationStatus.billable_sms), NotificationAllTimeView.status.in_(NotificationStatus.billable_sms),
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)), NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
NotificationAllTimeView.created_at >= start_date, NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date, NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type, NotificationAllTimeView.notification_type == notification_type,
+12 -15
View File
@@ -7,11 +7,8 @@ from sqlalchemy.types import DateTime, Integer
from app import db from app import db
from app.dao.dao_utils import autocommit from app.dao.dao_utils import autocommit
from app.enums import NotificationType, NotificationStatus from app.enums import NotificationType, NotificationStatus, KeyType
from app.models import ( from app.models import (
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
FactNotificationStatus, FactNotificationStatus,
Notification, Notification,
NotificationAllTimeView, NotificationAllTimeView,
@@ -55,7 +52,7 @@ def update_fact_notification_status(process_day, notification_type, service_id):
NotificationAllTimeView.created_at < end_date, NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type, NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service_id, NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)), NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
) )
.group_by( .group_by(
NotificationAllTimeView.template_id, NotificationAllTimeView.template_id,
@@ -95,7 +92,7 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service
FactNotificationStatus.service_id == service_id, FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date < end_date, FactNotificationStatus.local_date < end_date,
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
) )
.group_by( .group_by(
func.date_trunc("month", FactNotificationStatus.local_date).label("month"), func.date_trunc("month", FactNotificationStatus.local_date).label("month"),
@@ -120,7 +117,7 @@ def fetch_notification_status_for_service_for_day(fetch_day, service_id):
Notification.created_at Notification.created_at
< get_midnight_in_utc(fetch_day + timedelta(days=1)), < get_midnight_in_utc(fetch_day + timedelta(days=1)),
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
) )
.group_by(Notification.notification_type, Notification.status) .group_by(Notification.notification_type, Notification.status)
.all() .all()
@@ -144,7 +141,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
).filter( ).filter(
FactNotificationStatus.service_id == service_id, FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
) )
stats_for_today = ( stats_for_today = (
@@ -157,7 +154,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
.filter( .filter(
Notification.created_at >= get_midnight_in_utc(now), Notification.created_at >= get_midnight_in_utc(now),
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
) )
.group_by( .group_by(
Notification.notification_type, Notification.notification_type,
@@ -294,7 +291,7 @@ def fetch_stats_for_all_services_by_date_range(
) )
) )
if not include_from_test_key: if not include_from_test_key:
stats = stats.filter(FactNotificationStatus.key_type != KEY_TYPE_TEST) stats = stats.filter(FactNotificationStatus.key_type != KeyType.TEST)
if start_date <= datetime.utcnow().date() <= end_date: if start_date <= datetime.utcnow().date() <= end_date:
today = get_midnight_in_utc(datetime.utcnow()) today = get_midnight_in_utc(datetime.utcnow())
@@ -313,7 +310,7 @@ def fetch_stats_for_all_services_by_date_range(
) )
) )
if not include_from_test_key: if not include_from_test_key:
subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST) subquery = subquery.filter(Notification.key_type != KeyType.TEST)
subquery = subquery.subquery() subquery = subquery.subquery()
stats_for_today = db.session.query( stats_for_today = db.session.query(
@@ -375,7 +372,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
FactNotificationStatus.service_id == service_id, FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date, FactNotificationStatus.local_date <= end_date,
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
FactNotificationStatus.notification_status != NotificationStatus.CANCELLED, FactNotificationStatus.notification_status != NotificationStatus.CANCELLED,
) )
.group_by( .group_by(
@@ -412,7 +409,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
.filter( .filter(
Notification.created_at >= today, Notification.created_at >= today,
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
Notification.status != NotificationStatus.CANCELLED, Notification.status != NotificationStatus.CANCELLED,
) )
.group_by( .group_by(
@@ -480,7 +477,7 @@ def get_total_notifications_for_date_range(start_date, end_date):
).label("sms"), ).label("sms"),
) )
.filter( .filter(
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
) )
.group_by(FactNotificationStatus.local_date) .group_by(FactNotificationStatus.local_date)
.order_by(FactNotificationStatus.local_date) .order_by(FactNotificationStatus.local_date)
@@ -581,7 +578,7 @@ def fetch_monthly_notification_statuses_per_service(start_date, end_date):
.filter( .filter(
FactNotificationStatus.notification_status != NotificationStatus.CREATED, FactNotificationStatus.notification_status != NotificationStatus.CREATED,
Service.active.is_(True), Service.active.is_(True),
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
Service.restricted.is_(False), Service.restricted.is_(False),
FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date, FactNotificationStatus.local_date <= end_date,
+7 -8
View File
@@ -16,9 +16,8 @@ from werkzeug.datastructures import MultiDict
from app import create_uuid, db from app import create_uuid, db
from app.dao.dao_utils import autocommit from app.dao.dao_utils import autocommit
from app.enums import NotificationType, NotificationStatus from app.enums import NotificationType, NotificationStatus, KeyType
from app.models import ( from app.models import (
KEY_TYPE_TEST,
FactNotificationStatus, FactNotificationStatus,
Notification, Notification,
NotificationHistory, NotificationHistory,
@@ -36,7 +35,7 @@ def dao_get_last_date_template_was_used(template_id, service_id):
.filter( .filter(
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.template_id == template_id, Notification.template_id == template_id,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
) )
.scalar() .scalar()
) )
@@ -48,7 +47,7 @@ def dao_get_last_date_template_was_used(template_id, service_id):
db.session.query(functions.max(FactNotificationStatus.local_date)) db.session.query(functions.max(FactNotificationStatus.local_date))
.filter( .filter(
FactNotificationStatus.template_id == template_id, FactNotificationStatus.template_id == template_id,
FactNotificationStatus.key_type != KEY_TYPE_TEST, FactNotificationStatus.key_type != KeyType.TEST,
) )
.scalar() .scalar()
) )
@@ -269,7 +268,7 @@ def get_notifications_for_service(
if key_type is not None: if key_type is not None:
filters.append(Notification.key_type == key_type) filters.append(Notification.key_type == key_type)
elif not include_from_test_key: elif not include_from_test_key:
filters.append(Notification.key_type != KEY_TYPE_TEST) filters.append(Notification.key_type != KeyType.TEST)
if client_reference is not None: if client_reference is not None:
filters.append(Notification.client_reference == client_reference) filters.append(Notification.client_reference == client_reference)
@@ -409,7 +408,7 @@ def move_notifications_to_notification_history(
Notification.notification_type == notification_type, Notification.notification_type == notification_type,
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.created_at < timestamp_to_delete_backwards_from, Notification.created_at < timestamp_to_delete_backwards_from,
Notification.key_type == KEY_TYPE_TEST, Notification.key_type == KeyType.TEST,
).delete(synchronize_session=False) ).delete(synchronize_session=False)
db.session.commit() db.session.commit()
@@ -513,7 +512,7 @@ def dao_get_notifications_by_recipient_or_reference(
Notification.normalised_to.like("%{}%".format(normalised)), Notification.normalised_to.like("%{}%".format(normalised)),
Notification.client_reference.ilike("%{}%".format(search_term)), Notification.client_reference.ilike("%{}%".format(search_term)),
), ),
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
] ]
if statuses: if statuses:
@@ -576,7 +575,7 @@ def dao_get_notifications_processing_time_stats(start_date, end_date):
Notification.created_at >= start_date, Notification.created_at >= start_date,
Notification.created_at < end_date, Notification.created_at < end_date,
Notification.api_key_id.isnot(None), Notification.api_key_id.isnot(None),
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
) )
.one() .one()
) )
+6 -7
View File
@@ -13,9 +13,8 @@ from app.dao.organization_dao import dao_get_organization_by_email_address
from app.dao.service_sms_sender_dao import insert_service_sms_sender from app.dao.service_sms_sender_dao import insert_service_sms_sender
from app.dao.service_user_dao import dao_get_service_user from app.dao.service_user_dao import dao_get_service_user
from app.dao.template_folder_dao import dao_get_valid_template_folders_by_id from app.dao.template_folder_dao import dao_get_valid_template_folders_by_id
from app.enums import NotificationStatus, NotificationType, ServicePermissionType from app.enums import NotificationStatus, NotificationType, ServicePermissionType, KeyType
from app.models import ( from app.models import (
KEY_TYPE_TEST,
AnnualBilling, AnnualBilling,
ApiKey, ApiKey,
FactBilling, FactBilling,
@@ -405,7 +404,7 @@ def dao_fetch_todays_stats_for_service(service_id):
) )
.filter( .filter(
Notification.service_id == service_id, Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
Notification.created_at >= start_date, Notification.created_at >= start_date,
) )
.group_by( .group_by(
@@ -439,7 +438,7 @@ def dao_fetch_todays_stats_for_all_services(
) )
if not include_from_test_key: if not include_from_test_key:
subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST) subquery = subquery.filter(Notification.key_type != KeyType.TEST)
subquery = subquery.subquery() subquery = subquery.subquery()
@@ -510,7 +509,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
Notification.service_id == Service.id, Notification.service_id == Service.id,
Notification.created_at >= start_date, Notification.created_at >= start_date,
Notification.created_at <= end_date, Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
Notification.notification_type == NotificationType.SMS, Notification.notification_type == NotificationType.SMS,
func.substr(Notification.normalised_to, 3, 7) == "7700900", func.substr(Notification.normalised_to, 3, 7) == "7700900",
Service.restricted == False, # noqa Service.restricted == False, # noqa
@@ -534,7 +533,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
Notification.service_id == Service.id, Notification.service_id == Service.id,
Notification.created_at >= start_date, Notification.created_at >= start_date,
Notification.created_at <= end_date, Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
Notification.notification_type == NotificationType.SMS, Notification.notification_type == NotificationType.SMS,
Service.restricted == False, # noqa Service.restricted == False, # noqa
Service.active == True, # noqa Service.active == True, # noqa
@@ -562,7 +561,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
Notification.service_id == Service.id, Notification.service_id == Service.id,
Notification.created_at >= start_date, Notification.created_at >= start_date,
Notification.created_at <= end_date, Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST, Notification.key_type != KeyType.TEST,
Notification.notification_type == NotificationType.SMS, Notification.notification_type == NotificationType.SMS,
Notification.status == NotificationStatus.PERMANENT_FAILURE, Notification.status == NotificationStatus.PERMANENT_FAILURE,
Service.restricted == False, # noqa Service.restricted == False, # noqa
+3 -4
View File
@@ -15,12 +15,11 @@ from app.celery.test_key_tasks import send_email_response, send_sms_response
from app.dao.email_branding_dao import dao_get_email_branding_by_id from app.dao.email_branding_dao import dao_get_email_branding_by_id
from app.dao.notifications_dao import dao_update_notification from app.dao.notifications_dao import dao_update_notification
from app.dao.provider_details_dao import get_provider_details_by_notification_type from app.dao.provider_details_dao import get_provider_details_by_notification_type
from app.enums import NotificationStatus, NotificationType from app.enums import NotificationStatus, NotificationType, KeyType
from app.exceptions import NotificationTechnicalFailureException from app.exceptions import NotificationTechnicalFailureException
from app.models import ( from app.models import (
BRANDING_BOTH, BRANDING_BOTH,
BRANDING_ORG_BANNER, BRANDING_ORG_BANNER,
KEY_TYPE_TEST,
) )
from app.serialised_models import SerialisedService, SerialisedTemplate from app.serialised_models import SerialisedService, SerialisedTemplate
@@ -50,7 +49,7 @@ def send_sms_to_provider(notification):
prefix=service.name, prefix=service.name,
show_prefix=service.prefix_sms, show_prefix=service.prefix_sms,
) )
if notification.key_type == KEY_TYPE_TEST: if notification.key_type == KeyType.TEST:
update_notification_to_sending(notification, provider) update_notification_to_sending(notification, provider)
send_sms_response(provider.name, str(notification.id)) send_sms_response(provider.name, str(notification.id))
@@ -134,7 +133,7 @@ def send_email_to_provider(notification):
# Someone needs an email, possibly new registration # Someone needs an email, possibly new registration
recipient = redis_store.get(f"email-address-{notification.id}") recipient = redis_store.get(f"email-address-{notification.id}")
recipient = recipient.decode("utf-8") recipient = recipient.decode("utf-8")
if notification.key_type == KEY_TYPE_TEST: if notification.key_type == KeyType.TEST:
notification.reference = str(create_uuid()) notification.reference = str(create_uuid())
update_notification_to_sending(notification, provider) update_notification_to_sending(notification, provider)
send_email_response(notification.reference, recipient) send_email_response(notification.reference, recipient)
-9
View File
@@ -1287,15 +1287,6 @@ class TemplateHistory(TemplateBase):
) )
SNS_PROVIDER = "sns"
SES_PROVIDER = "ses"
SMS_PROVIDERS = [SNS_PROVIDER]
EMAIL_PROVIDERS = [SES_PROVIDER]
PROVIDERS = SMS_PROVIDERS + EMAIL_PROVIDERS
# TODO: What about these?
class ProviderDetails(db.Model): class ProviderDetails(db.Model):
__tablename__ = "provider_details" __tablename__ = "provider_details"
+4 -4
View File
@@ -16,8 +16,8 @@ from app.dao.notifications_dao import (
dao_create_notification, dao_create_notification,
dao_delete_notifications_by_id, dao_delete_notifications_by_id,
) )
from app.enums import NotificationType, NotificationStatus from app.enums import NotificationType, NotificationStatus, KeyType
from app.models import KEY_TYPE_TEST, Notification from app.models import Notification
from app.v2.errors import BadRequestError from app.v2.errors import BadRequestError
@@ -131,7 +131,7 @@ def persist_notification(
if not simulated: if not simulated:
current_app.logger.info("Firing dao_create_notification") current_app.logger.info("Firing dao_create_notification")
dao_create_notification(notification) dao_create_notification(notification)
if key_type != KEY_TYPE_TEST and current_app.config["REDIS_ENABLED"]: if key_type != KeyType.TEST and current_app.config["REDIS_ENABLED"]:
current_app.logger.info( current_app.logger.info(
"Redis enabled, querying cache key for service id: {}".format( "Redis enabled, querying cache key for service id: {}".format(
service.id service.id
@@ -147,7 +147,7 @@ def persist_notification(
def send_notification_to_queue_detached( def send_notification_to_queue_detached(
key_type, notification_type, notification_id, queue=None key_type, notification_type, notification_id, queue=None
): ):
if key_type == KEY_TYPE_TEST: if key_type == KeyType.TEST:
print("send_notification_to_queue_detached key is test key") print("send_notification_to_queue_detached key is test key")
if notification_type == NotificationType.SMS: if notification_type == NotificationType.SMS:
+3 -4
View File
@@ -4,9 +4,8 @@ from notifications_utils import SMS_CHAR_COUNT_LIMIT
from app import api_user, authenticated_service from app import api_user, authenticated_service
from app.config import QueueNames from app.config import QueueNames
from app.dao import notifications_dao from app.dao import notifications_dao
from app.enums import NotificationType from app.enums import NotificationType, KeyType, TemplateProcessType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import KEY_TYPE_TEAM, PRIORITY
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -137,7 +136,7 @@ def send_notification(notification_type):
reply_to_text=template.reply_to_text, reply_to_text=template.reply_to_text,
) )
if not simulated: 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(notification=notification_model, queue=queue_name) send_notification_to_queue(notification=notification_model, queue=queue_name)
else: else:
@@ -171,7 +170,7 @@ def get_notification_return_data(notification_id, notification, template):
def _service_allowed_to_send_to(notification, service): def _service_allowed_to_send_to(notification, service):
if not service_allowed_to_send_to(notification["to"], service, api_user.key_type): if not service_allowed_to_send_to(notification["to"], service, api_user.key_type):
if api_user.key_type == KEY_TYPE_TEAM: if api_user.key_type == KeyType.TEAM:
message = "Cant send to this recipient using a team-only API key" message = "Cant send to this recipient using a team-only API key"
else: else:
message = ( message = (
+5 -5
View File
@@ -15,8 +15,8 @@ from app import redis_store
from app.dao.notifications_dao import dao_get_notification_count_for_service 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_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.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.enums import NotificationType, ServicePermissionType, TemplateType from app.enums import NotificationType, ServicePermissionType, TemplateType, KeyType
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST, ServicePermission from app.models import ServicePermission
from app.notifications.process_notifications import create_content_for_notification from app.notifications.process_notifications import create_content_for_notification
from app.serialised_models import SerialisedTemplate from app.serialised_models import SerialisedTemplate
from app.service.utils import service_allowed_to_send_to from app.service.utils import service_allowed_to_send_to
@@ -40,7 +40,7 @@ def check_service_over_api_rate_limit(service, api_key):
def check_service_over_total_message_limit(key_type, service): def check_service_over_total_message_limit(key_type, service):
if key_type == KEY_TYPE_TEST or not current_app.config["REDIS_ENABLED"]: if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
return 0 return 0
cache_key = total_limit_cache_key(service.id) cache_key = total_limit_cache_key(service.id)
@@ -61,7 +61,7 @@ def check_service_over_total_message_limit(key_type, service):
def check_application_over_retention_limit(key_type, service): def check_application_over_retention_limit(key_type, service):
if key_type == KEY_TYPE_TEST or not current_app.config["REDIS_ENABLED"]: if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
return 0 return 0
total_stats = dao_get_notification_count_for_service(service_id=service.id) total_stats = dao_get_notification_count_for_service(service_id=service.id)
@@ -104,7 +104,7 @@ def service_can_send_to_recipient(
if not service_allowed_to_send_to( if not service_allowed_to_send_to(
send_to, service, key_type, allow_guest_list_recipients send_to, service, key_type, allow_guest_list_recipients
): ):
if key_type == KEY_TYPE_TEAM: if key_type == KeyType.TEAM:
message = "Cant send to this recipient using a team-only API key" message = "Cant send to this recipient using a team-only API key"
else: else:
message = ( message = (
+3 -3
View File
@@ -12,9 +12,9 @@ from app.dao.invited_org_user_dao import (
save_invited_org_user, save_invited_org_user,
) )
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.enums import NotificationType from app.enums import NotificationType, KeyType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import KEY_TYPE_NORMAL, InvitedOrganizationUser from app.models import InvitedOrganizationUser
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -67,7 +67,7 @@ def invite_user_to_org(organization_id):
}, },
notification_type=NotificationType.EMAIL, notification_type=NotificationType.EMAIL,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=invited_org_user.invited_by.email_address, reply_to_text=invited_org_user.invited_by.email_address,
) )
+3 -2
View File
@@ -20,8 +20,9 @@ from app.dao.organization_dao import (
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.dao.users_dao import get_user_by_id from app.dao.users_dao import get_user_by_id
from app.enums import KeyType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import KEY_TYPE_NORMAL, Organization from app.models import Organization
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -205,7 +206,7 @@ def send_notifications_on_mou_signed(organization_id):
personalisation=personalisation, personalisation=personalisation,
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=notify_service.get_default_reply_to_email_address(), reply_to_text=notify_service.get_default_reply_to_email_address(),
) )
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY) send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
+3 -2
View File
@@ -73,8 +73,9 @@ from app.dao.services_dao import (
) )
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.dao.users_dao import get_user_by_id from app.dao.users_dao import get_user_by_id
from app.enums import KeyType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import KEY_TYPE_NORMAL, EmailBranding, Permission, Service from app.models import EmailBranding, Permission, Service
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -782,7 +783,7 @@ def verify_reply_to_email_address(service_id):
personalisation="", personalisation="",
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=notify_service.get_default_reply_to_email_address(), reply_to_text=notify_service.get_default_reply_to_email_address(),
) )
+5 -5
View File
@@ -6,8 +6,8 @@ from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.dao.services_dao import dao_fetch_service_by_id 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.templates_dao import dao_get_template_by_id_and_service_id
from app.dao.users_dao import get_user_by_id from app.dao.users_dao import get_user_by_id
from app.enums import NotificationType from app.enums import NotificationType, KeyType
from app.models import KEY_TYPE_NORMAL, PRIORITY from app.models import PRIORITY
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -46,11 +46,11 @@ def send_one_off_notification(service_id, post_data):
validate_template(template.id, personalisation, service, template.template_type) validate_template(template.id, personalisation, service, template.template_type)
check_service_over_total_message_limit(KEY_TYPE_NORMAL, service) check_service_over_total_message_limit(KeyType.NORMAL, service)
validate_and_format_recipient( validate_and_format_recipient(
send_to=post_data["to"], send_to=post_data["to"],
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
service=service, service=service,
notification_type=template.template_type, notification_type=template.template_type,
allow_guest_list_recipients=False, allow_guest_list_recipients=False,
@@ -74,7 +74,7 @@ def send_one_off_notification(service_id, post_data):
personalisation=personalisation, personalisation=personalisation,
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
created_by_id=post_data["created_by"], created_by_id=post_data["created_by"],
reply_to_text=reply_to, reply_to_text=reply_to,
reference=create_one_off_reference(template.template_type), reference=create_one_off_reference(template.template_type),
+2 -3
View File
@@ -6,8 +6,7 @@ from app.dao.services_dao import (
dao_fetch_service_by_id, dao_fetch_service_by_id,
) )
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.enums import TemplateType from app.enums import TemplateType, KeyType
from app.models import KEY_TYPE_NORMAL
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -36,7 +35,7 @@ def send_notification_to_service_users(
personalisation=personalisation, personalisation=personalisation,
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=notify_service.get_default_reply_to_email_address(), reply_to_text=notify_service.get_default_reply_to_email_address(),
) )
send_notification_to_queue(notification, queue=QueueNames.NOTIFY) send_notification_to_queue(notification, queue=QueueNames.NOTIFY)
+6 -6
View File
@@ -3,8 +3,8 @@ import itertools
from notifications_utils.recipients import allowed_to_send_to from notifications_utils.recipients import allowed_to_send_to
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.enums import RecipientType from app.enums import RecipientType, KeyType
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, ServiceGuestList from app.models import ServiceGuestList
def get_recipients_from_request(request_json, key, type): def get_recipients_from_request(request_json, key, type):
@@ -28,10 +28,10 @@ def get_guest_list_objects(service_id, request_json):
def service_allowed_to_send_to( def service_allowed_to_send_to(
recipient, service, key_type, allow_guest_list_recipients=True recipient, service, key_type, allow_guest_list_recipients=True
): ):
if key_type == KEY_TYPE_TEST: if key_type == KeyType.TEST:
return True return True
if key_type == KEY_TYPE_NORMAL and not service.restricted: if key_type == KeyType.NORMAL and not service.restricted:
return True return True
# Revert back to the ORM model here so we can get some things which # Revert back to the ORM model here so we can get some things which
@@ -45,8 +45,8 @@ def service_allowed_to_send_to(
member.recipient for member in service.guest_list if allow_guest_list_recipients member.recipient for member in service.guest_list if allow_guest_list_recipients
] ]
if (key_type == KEY_TYPE_NORMAL and service.restricted) or ( if (key_type == KeyType.NORMAL and service.restricted) or (
key_type == KEY_TYPE_TEAM key_type == KeyType.TEAM
): ):
return allowed_to_send_to( return allowed_to_send_to(
recipient, itertools.chain(team_members, guest_list_members) recipient, itertools.chain(team_members, guest_list_members)
+4 -4
View File
@@ -14,9 +14,9 @@ from app.dao.invited_user_dao import (
save_invited_user, save_invited_user,
) )
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.enums import NotificationType from app.enums import InvitedUserStatus, NotificationType, KeyType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import INVITE_PENDING, KEY_TYPE_NORMAL, Service from app.models import Service
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -47,7 +47,7 @@ def _create_service_invite(invited_user, invite_link_host):
}, },
notification_type=NotificationType.EMAIL, notification_type=NotificationType.EMAIL,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=invited_user.from_user.email_address, reply_to_text=invited_user.from_user.email_address,
) )
@@ -116,7 +116,7 @@ def resend_service_invite(service_id, invited_user_id):
) )
fetched.created_at = datetime.utcnow() fetched.created_at = datetime.utcnow()
fetched.status = INVITE_PENDING fetched.status = InvitedUserStatus.PENDING
current_data = {k: v for k, v in invited_user_schema.dump(fetched).items()} current_data = {k: v for k, v in invited_user_schema.dump(fetched).items()}
update_dict = invited_user_schema.load(current_data) update_dict = invited_user_schema.load(current_data)
+8 -8
View File
@@ -32,9 +32,9 @@ from app.dao.users_dao import (
update_user_password, update_user_password,
use_user_code, use_user_code,
) )
from app.enums import CodeType, NotificationType, TemplateType from app.enums import CodeType, NotificationType, TemplateType, KeyType
from app.errors import InvalidRequest, register_errors from app.errors import InvalidRequest, register_errors
from app.models import KEY_TYPE_NORMAL, Permission, Service from app.models import Permission, Service
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue, send_notification_to_queue,
@@ -134,7 +134,7 @@ def update_user_attribute(user_id):
}, },
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=reply_to, reply_to_text=reply_to,
) )
@@ -348,7 +348,7 @@ def create_2fa_code(
personalisation=personalisation, personalisation=personalisation,
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=reply_to, reply_to_text=reply_to,
) )
@@ -387,7 +387,7 @@ def send_user_confirm_new_email(user_id):
}, },
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=service.get_default_reply_to_email_address(), reply_to_text=service.get_default_reply_to_email_address(),
) )
@@ -425,7 +425,7 @@ def send_new_user_email_verification(user_id):
}, },
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=service.get_default_reply_to_email_address(), reply_to_text=service.get_default_reply_to_email_address(),
) )
@@ -471,7 +471,7 @@ def send_already_registered_email(user_id):
}, },
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=service.get_default_reply_to_email_address(), reply_to_text=service.get_default_reply_to_email_address(),
) )
@@ -588,7 +588,7 @@ def send_user_reset_password():
}, },
notification_type=template.template_type, notification_type=template.template_type,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL, key_type=KeyType.NORMAL,
reply_to_text=service.get_default_reply_to_email_address(), reply_to_text=service.get_default_reply_to_email_address(),
) )
+4 -4
View File
@@ -10,8 +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.celery.tasks import save_api_email, save_api_sms
from app.clients.document_download import DocumentDownloadError from app.clients.document_download import DocumentDownloadError
from app.config import QueueNames from app.config import QueueNames
from app.enums import NotificationType, NotificationStatus from app.enums import NotificationType, NotificationStatus, KeyType, TemplateProcessType
from app.models import KEY_TYPE_NORMAL, PRIORITY, Notification from app.models import Notification
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue_detached, send_notification_to_queue_detached,
@@ -130,7 +130,7 @@ def process_sms_or_email_notification(
if ( if (
service.high_volume service.high_volume
and api_user.key_type == KEY_TYPE_NORMAL and api_user.key_type == KeyType.NORMAL
and notification_type in {NotificationType.EMAIL, NotificationType.SMS} and notification_type in {NotificationType.EMAIL, NotificationType.SMS}
): ):
# Put service with high volumes of notifications onto a queue # Put service with high volumes of notifications onto a queue
@@ -177,7 +177,7 @@ def process_sms_or_email_notification(
) )
if not simulated: 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( send_notification_to_queue_detached(
key_type=api_user.key_type, key_type=api_user.key_type,
notification_type=notification_type, notification_type=notification_type,