Cleaning up a lot of things, getting Enums used everywhere.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-28 12:40:52 -05:00
parent 0368307802
commit 820ee5a942
30 changed files with 209 additions and 180 deletions

View File

@@ -9,12 +9,11 @@ from app import db
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.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
NOTIFICATION_STATUS_TYPES_BILLABLE_SMS,
NOTIFICATION_STATUS_TYPES_SENT_EMAILS,
SMS_TYPE,
NotificationType,
AnnualBilling,
FactBilling,
NotificationAllTimeView,
@@ -53,7 +52,7 @@ def fetch_sms_free_allowance_remainder_until_date(end_date):
AnnualBilling.service_id == FactBilling.service_id,
FactBilling.local_date >= start_of_year,
FactBilling.local_date < end_date,
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
),
)
.filter(
@@ -117,7 +116,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
.filter(
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
)
.group_by(
Organization.name,
@@ -269,7 +268,7 @@ def query_service_email_usage_for_year(service_id, year):
FactBilling.service_id == service_id,
FactBilling.local_date >= year_start,
FactBilling.local_date <= year_end,
FactBilling.notification_type == EMAIL_TYPE,
FactBilling.notification_type == NotificationType.EMAIL,
)
@@ -356,7 +355,7 @@ def query_service_sms_usage_for_year(service_id, year):
FactBilling.service_id == service_id,
FactBilling.local_date >= year_start,
FactBilling.local_date <= year_end,
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
AnnualBilling.financial_year_start == year,
)
)
@@ -386,7 +385,7 @@ def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=F
services = [Service.query.get(service_id)]
for service in services:
for notification_type in (SMS_TYPE, EMAIL_TYPE):
for notification_type in (NotificationType.SMS, NotificationType.EMAIL):
if (not check_permissions) or service.has_permission(notification_type):
results = _query_for_billing_data(
notification_type=notification_type,
@@ -465,8 +464,8 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
)
query_funcs = {
SMS_TYPE: _sms_query,
EMAIL_TYPE: _email_query,
NotificationType.SMS: _sms_query,
NotificationType.EMAIL: _email_query,
}
query = query_funcs[notification_type]()
@@ -484,7 +483,7 @@ def get_service_ids_that_need_billing_populated(start_date, end_date):
.filter(
NotificationHistory.created_at >= start_date,
NotificationHistory.created_at <= end_date,
NotificationHistory.notification_type.in_([SMS_TYPE, EMAIL_TYPE]),
NotificationHistory.notification_type.in_([NotificationType.SMS, NotificationType.EMAIL]),
NotificationHistory.billable_units != 0,
)
.distinct()
@@ -495,7 +494,7 @@ def get_service_ids_that_need_billing_populated(start_date, end_date):
def get_rate(rates, notification_type, date):
start_of_day = get_midnight_in_utc(date)
if notification_type == SMS_TYPE:
if notification_type == NotificationType.SMS:
return next(
r.rate
for r in rates
@@ -576,7 +575,7 @@ def fetch_email_usage_for_organization(organization_id, start_date, end_date):
.filter(
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
FactBilling.notification_type == EMAIL_TYPE,
FactBilling.notification_type == NotificationType.EMAIL,
Service.organization_id == organization_id,
Service.restricted.is_(False),
)
@@ -690,7 +689,7 @@ def query_organization_sms_usage_for_year(organization_id, year):
Service.id == FactBilling.service_id,
FactBilling.local_date >= year_start,
FactBilling.local_date <= year_end,
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
),
)
.filter(
@@ -784,7 +783,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date):
case(
[
(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.notifications_sent,
)
],
@@ -795,7 +794,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date):
case(
[
(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.billable_units,
)
],
@@ -806,7 +805,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date):
case(
[
(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.billable_units * FactBilling.rate_multiplier,
)
],
@@ -817,7 +816,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date):
case(
[
(
FactBilling.notification_type == EMAIL_TYPE,
FactBilling.notification_type == NotificationType.EMAIL,
FactBilling.notifications_sent,
)
],
@@ -871,7 +870,7 @@ def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date):
).label("sms_cost"),
)
.filter(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
)
@@ -902,7 +901,7 @@ def fetch_volumes_by_service(start_date, end_date):
case(
[
(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.notifications_sent,
)
],
@@ -913,7 +912,7 @@ def fetch_volumes_by_service(start_date, end_date):
case(
[
(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.billable_units * FactBilling.rate_multiplier,
)
],
@@ -924,7 +923,7 @@ def fetch_volumes_by_service(start_date, end_date):
case(
[
(
FactBilling.notification_type == EMAIL_TYPE,
FactBilling.notification_type == NotificationType.EMAIL,
FactBilling.notifications_sent,
)
],

View File

@@ -6,7 +6,7 @@ from sqlalchemy.orm import aliased
from app import db
from app.dao.dao_utils import autocommit
from app.models import (
SMS_TYPE,
NotificationType,
InboundSms,
InboundSmsHistory,
Service,
@@ -130,7 +130,7 @@ def delete_inbound_sms_older_than_retention():
ServiceDataRetention.query.join(
ServiceDataRetention.service, Service.inbound_number
)
.filter(ServiceDataRetention.notification_type == SMS_TYPE)
.filter(ServiceDataRetention.notification_type == NotificationType.SMS)
.all()
)

View File

@@ -17,7 +17,6 @@ from werkzeug.datastructures import MultiDict
from app import create_uuid, db
from app.dao.dao_utils import autocommit
from app.models import (
EMAIL_TYPE,
KEY_TYPE_TEST,
NOTIFICATION_CREATED,
NOTIFICATION_FAILED,
@@ -27,7 +26,7 @@ from app.models import (
NOTIFICATION_SENDING,
NOTIFICATION_SENT,
NOTIFICATION_TEMPORARY_FAILURE,
SMS_TYPE,
NotificationType,
FactNotificationStatus,
Notification,
NotificationHistory,
@@ -138,7 +137,7 @@ def update_notification_status_by_id(
return None
if (
notification.notification_type == SMS_TYPE
notification.notification_type == NotificationType.SMS
and notification.international
and not country_records_delivery(notification.phone_prefix)
):
@@ -445,7 +444,7 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
Notification.query.filter(
Notification.created_at < cutoff_time,
Notification.status.in_(current_statuses),
Notification.notification_type.in_([SMS_TYPE, EMAIL_TYPE]),
Notification.notification_type.in_([NotificationType.SMS, NotificationType.EMAIL]),
)
.limit(limit)
.all()
@@ -485,7 +484,7 @@ def dao_get_notifications_by_recipient_or_reference(
page_size=None,
error_out=True,
):
if notification_type == SMS_TYPE:
if notification_type == NotificationType.SMS:
normalised = try_validate_and_format_phone_number(search_term)
for character in {"(", ")", " ", "-"}:
@@ -493,7 +492,7 @@ def dao_get_notifications_by_recipient_or_reference(
normalised = normalised.lstrip("+0")
elif notification_type == EMAIL_TYPE:
elif notification_type == NotificationType.EMAIL:
try:
normalised = validate_and_format_email_address(search_term)
except InvalidEmailError:
@@ -507,7 +506,7 @@ def dao_get_notifications_by_recipient_or_reference(
normalised = "".join(search_term.split()).lower()
else:
raise TypeError(f"Notification type must be {EMAIL_TYPE}, {SMS_TYPE}, or None")
raise TypeError(f"Notification type must be {NotificationType.EMAIL}, {NotificationType.SMS}, or None")
normalised = escape_special_characters(normalised)
search_term = escape_special_characters(search_term)

View File

@@ -6,7 +6,7 @@ from sqlalchemy import asc, desc, func
from app import db
from app.dao.dao_utils import autocommit
from app.models import (
SMS_TYPE,
NotificationType,
FactBilling,
ProviderDetails,
ProviderDetailsHistory,
@@ -126,7 +126,7 @@ def dao_get_provider_stats():
),
)
.filter(
FactBilling.notification_type == SMS_TYPE,
FactBilling.notification_type == NotificationType.SMS,
FactBilling.local_date >= first_day_of_the_month,
)
.group_by(FactBilling.provider)

View File

@@ -14,11 +14,8 @@ 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.template_folder_dao import dao_get_valid_template_folders_by_id
from app.models import (
EMAIL_TYPE,
INTERNATIONAL_SMS_TYPE,
KEY_TYPE_TEST,
NOTIFICATION_PERMANENT_FAILURE,
SMS_TYPE,
AnnualBilling,
ApiKey,
FactBilling,
@@ -27,11 +24,13 @@ from app.models import (
Job,
Notification,
NotificationHistory,
NotificationType,
Organization,
Permission,
Service,
ServiceEmailReplyTo,
ServicePermission,
ServicePermissionType,
ServiceSmsSender,
Template,
TemplateHistory,
@@ -46,9 +45,9 @@ from app.utils import (
)
DEFAULT_SERVICE_PERMISSIONS = [
SMS_TYPE,
EMAIL_TYPE,
INTERNATIONAL_SMS_TYPE,
ServicePermissionType.SMS,
ServicePermissionType.EMAIL,
ServicePermissionType.INTERNATIONAL_SMS,
]
@@ -518,7 +517,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
Notification.created_at >= start_date,
Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST,
Notification.notification_type == SMS_TYPE,
Notification.notification_type == NotificationType.SMS,
func.substr(Notification.normalised_to, 3, 7) == "7700900",
Service.restricted == False, # noqa
Service.active == True, # noqa
@@ -542,7 +541,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
Notification.created_at >= start_date,
Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST,
Notification.notification_type == SMS_TYPE,
Notification.notification_type == NotificationType.SMS,
Service.restricted == False, # noqa
Service.active == True, # noqa
)
@@ -570,7 +569,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
Notification.created_at >= start_date,
Notification.created_at <= end_date,
Notification.key_type != KEY_TYPE_TEST,
Notification.notification_type == SMS_TYPE,
Notification.notification_type == NotificationType.SMS,
Notification.status == NOTIFICATION_PERMANENT_FAILURE,
Service.restricted == False, # noqa
Service.active == True, # noqa

View File

@@ -8,7 +8,7 @@ from app import db
from app.models import (
JOB_STATUS_CANCELLED,
JOB_STATUS_SCHEDULED,
LETTER_TYPE,
NotificationType,
NOTIFICATION_CANCELLED,
Job,
Notification,
@@ -90,7 +90,7 @@ def dao_get_uploads_by_service_id(service_id, limit_days=None, page=1, page_size
letters_query_filter = [
Notification.service_id == service_id,
Notification.notification_type == LETTER_TYPE,
Notification.notification_type == NotificationType.LETTER,
Notification.api_key_id == None, # noqa
Notification.status != NOTIFICATION_CANCELLED,
Template.hidden == True, # noqa