more code review feedback

This commit is contained in:
Kenneth Kehl
2024-12-20 08:09:19 -08:00
parent a77343ebc7
commit 3c0621f472
22 changed files with 125 additions and 133 deletions

View File

@@ -43,7 +43,7 @@ def dao_update_annual_billing_for_future_years(
):
stmt = (
update(AnnualBilling)
.filter(
.where(
AnnualBilling.service_id == service_id,
AnnualBilling.financial_year_start > financial_year_start,
)

View File

@@ -46,6 +46,6 @@ def fetch_count_of_complaints(start_date, end_date):
stmt = (
select(func.count())
.select_from(Complaint)
.filter(Complaint.created_at >= start_date, Complaint.created_at < end_date)
.where(Complaint.created_at >= start_date, Complaint.created_at < end_date)
)
return db.session.execute(stmt).scalar() or 0

View File

@@ -52,7 +52,7 @@ def fetch_sms_free_allowance_remainder_until_date(end_date):
FactBilling.notification_type == NotificationType.SMS,
),
)
.filter(
.where(
AnnualBilling.financial_year_start == billing_year,
)
.group_by(
@@ -110,7 +110,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
FactBilling,
FactBilling.service_id == Service.id,
)
.filter(
.where(
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
FactBilling.notification_type == NotificationType.SMS,
@@ -250,7 +250,7 @@ def query_service_email_usage_for_year(service_id, year):
FactBilling.billable_units.label("charged_units"),
)
.select_from(FactBilling)
.filter(
.where(
FactBilling.service_id == service_id,
FactBilling.local_date >= year_start,
FactBilling.local_date <= year_end,
@@ -338,7 +338,7 @@ def query_service_sms_usage_for_year(service_id, year):
)
.select_from(FactBilling)
.join(AnnualBilling, AnnualBilling.service_id == service_id)
.filter(
.where(
FactBilling.service_id == service_id,
FactBilling.local_date >= year_start,
FactBilling.local_date <= year_end,
@@ -355,7 +355,7 @@ def delete_billing_data_for_service_for_day(process_day, service_id):
Returns how many rows were deleted
"""
stmt = delete(FactBilling).filter(
stmt = delete(FactBilling).where(
FactBilling.local_date == process_day, FactBilling.service_id == service_id
)
result = db.session.execute(stmt)
@@ -403,7 +403,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
func.count().label("notifications_sent"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.status.in_(
NotificationStatus.sent_email_types()
),
@@ -438,7 +438,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
func.count().label("notifications_sent"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.status.in_(
NotificationStatus.billable_sms_types()
),
@@ -474,7 +474,7 @@ def get_service_ids_that_need_billing_populated(start_date, end_date):
stmt = (
select(NotificationHistory.service_id)
.select_from(NotificationHistory)
.filter(
.where(
NotificationHistory.created_at >= start_date,
NotificationHistory.created_at <= end_date,
NotificationHistory.notification_type.in_(
@@ -568,7 +568,7 @@ def fetch_email_usage_for_organization(organization_id, start_date, end_date):
FactBilling,
FactBilling.service_id == Service.id,
)
.filter(
.where(
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
FactBilling.notification_type == NotificationType.EMAIL,
@@ -623,7 +623,7 @@ def fetch_sms_billing_for_organization(organization_id, financial_year):
),
)
.outerjoin(ft_billing_substmt, Service.id == ft_billing_substmt.c.service_id)
.filter(
.where(
Service.organization_id == organization_id, Service.restricted.is_(False)
)
.group_by(Service.id, Service.name, AnnualBilling.free_sms_fragment_limit)
@@ -688,7 +688,7 @@ def query_organization_sms_usage_for_year(organization_id, year):
FactBilling.notification_type == NotificationType.SMS,
),
)
.filter(
.where(
Service.organization_id == organization_id,
AnnualBilling.financial_year_start == year,
)
@@ -812,9 +812,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date):
)
).label("email_totals"),
)
.filter(
FactBilling.local_date >= start_date, FactBilling.local_date <= end_date
)
.where(FactBilling.local_date >= start_date, FactBilling.local_date <= end_date)
.group_by(FactBilling.local_date, FactBilling.notification_type)
.subquery()
)
@@ -857,7 +855,7 @@ def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date):
).label("sms_cost"),
)
.select_from(FactBilling)
.filter(
.where(
FactBilling.notification_type == NotificationType.SMS,
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
@@ -912,9 +910,7 @@ def fetch_volumes_by_service(start_date, end_date):
).label("email_totals"),
)
.select_from(FactBilling)
.filter(
FactBilling.local_date >= start_date, FactBilling.local_date <= end_date
)
.where(FactBilling.local_date >= start_date, FactBilling.local_date <= end_date)
.group_by(
FactBilling.local_date,
FactBilling.service_id,
@@ -930,7 +926,7 @@ def fetch_volumes_by_service(start_date, end_date):
AnnualBilling.free_sms_fragment_limit,
)
.select_from(AnnualBilling)
.filter(AnnualBilling.financial_year_start <= year_end_date)
.where(AnnualBilling.financial_year_start <= year_end_date)
.group_by(AnnualBilling.service_id, AnnualBilling.free_sms_fragment_limit)
.subquery()
)
@@ -957,7 +953,7 @@ def fetch_volumes_by_service(start_date, end_date):
.outerjoin( # include services without volume
volume_stats, Service.id == volume_stats.c.service_id
)
.filter(
.where(
Service.restricted.is_(False),
Service.count_as_live.is_(True),
Service.active.is_(True),

View File

@@ -33,7 +33,7 @@ def update_fact_notification_status(process_day, notification_type, service_id):
end_date = get_midnight_in_utc(process_day + timedelta(days=1))
# delete any existing rows in case some no longer exist e.g. if all messages are sent
stmt = delete(FactNotificationStatus).filter(
stmt = delete(FactNotificationStatus).where(
FactNotificationStatus.local_date == process_day,
FactNotificationStatus.notification_type == notification_type,
FactNotificationStatus.service_id == service_id,
@@ -55,7 +55,7 @@ def update_fact_notification_status(process_day, notification_type, service_id):
func.count().label("notification_count"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
@@ -97,7 +97,7 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service
func.count(NotificationAllTimeView.id).label("count"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
@@ -122,7 +122,7 @@ def fetch_notification_status_for_service_for_day(fetch_day, service_id):
func.count().label("count"),
)
.select_from(Notification)
.filter(
.where(
Notification.created_at >= get_midnight_in_utc(fetch_day),
Notification.created_at
< get_midnight_in_utc(fetch_day + timedelta(days=1)),
@@ -260,7 +260,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
func.sum(FactNotificationStatus.notification_count).label("count"),
)
.select_from(FactNotificationStatus)
.filter(
.where(
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
)
@@ -279,7 +279,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
Notification.key_type.cast(db.Text),
func.count().label("count"),
)
.filter(Notification.created_at >= today)
.where(Notification.created_at >= today)
.group_by(
Notification.notification_type,
Notification.status,
@@ -313,7 +313,7 @@ def fetch_notification_statuses_for_job(job_id):
func.sum(FactNotificationStatus.notification_count).label("count"),
)
.select_from(FactNotificationStatus)
.filter(
.where(
FactNotificationStatus.job_id == job_id,
)
.group_by(FactNotificationStatus.notification_status)
@@ -338,7 +338,7 @@ def fetch_stats_for_all_services_by_date_range(
func.sum(FactNotificationStatus.notification_count).label("count"),
)
.select_from(FactNotificationStatus)
.filter(
.where(
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
FactNotificationStatus.service_id == Service.id,
@@ -357,7 +357,7 @@ def fetch_stats_for_all_services_by_date_range(
)
)
if not include_from_test_key:
stats = stats.filter(FactNotificationStatus.key_type != KeyType.TEST)
stats = stats.where(FactNotificationStatus.key_type != KeyType.TEST)
if start_date <= utc_now().date() <= end_date:
today = get_midnight_in_utc(utc_now())
@@ -369,7 +369,7 @@ def fetch_stats_for_all_services_by_date_range(
func.count(Notification.id).label("count"),
)
.select_from(Notification)
.filter(Notification.created_at >= today)
.where(Notification.created_at >= today)
.group_by(
Notification.notification_type,
Notification.status,
@@ -377,7 +377,7 @@ def fetch_stats_for_all_services_by_date_range(
)
)
if not include_from_test_key:
substmt = substmt.filter(Notification.key_type != KeyType.TEST)
substmt = substmt.where(Notification.key_type != KeyType.TEST)
substmt = substmt.subquery()
stats_for_today = select(
@@ -435,7 +435,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
func.sum(FactNotificationStatus.notification_count).label("count"),
)
.join(Template, FactNotificationStatus.template_id == Template.id)
.filter(
.where(
FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
@@ -473,7 +473,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
Template,
Notification.template_id == Template.id,
)
.filter(
.where(
Notification.created_at >= today,
Notification.service_id == service_id,
Notification.key_type != KeyType.TEST,
@@ -539,14 +539,14 @@ def get_total_notifications_for_date_range(start_date, end_date):
)
).label("sms"),
)
.filter(
.where(
FactNotificationStatus.key_type != KeyType.TEST,
)
.group_by(FactNotificationStatus.local_date)
.order_by(FactNotificationStatus.local_date)
)
if start_date and end_date:
stmt = stmt.filter(
stmt = stmt.where(
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
)
@@ -629,7 +629,7 @@ def fetch_monthly_notification_statuses_per_service(start_date, end_date):
).label("count_sent"),
)
.join(Service, FactNotificationStatus.service_id == Service.id)
.filter(
.where(
FactNotificationStatus.notification_status != NotificationStatus.CREATED,
Service.active.is_(True),
FactNotificationStatus.key_type != KeyType.TEST,

View File

@@ -11,19 +11,19 @@ def dao_get_inbound_numbers():
def dao_get_available_inbound_numbers():
stmt = select(InboundNumber).filter(
stmt = select(InboundNumber).where(
InboundNumber.active, InboundNumber.service_id.is_(None)
)
return db.session.execute(stmt).scalars().all()
def dao_get_inbound_number_for_service(service_id):
stmt = select(InboundNumber).filter(InboundNumber.service_id == service_id)
stmt = select(InboundNumber).where(InboundNumber.service_id == service_id)
return db.session.execute(stmt).scalars().first()
def dao_get_inbound_number(inbound_number_id):
stmt = select(InboundNumber).filter(InboundNumber.id == inbound_number_id)
stmt = select(InboundNumber).where(InboundNumber.id == inbound_number_id)
return db.session.execute(stmt).scalars().first()
@@ -35,7 +35,7 @@ def dao_set_inbound_number_to_service(service_id, inbound_number):
@autocommit
def dao_set_inbound_number_active_flag(service_id, active):
stmt = select(InboundNumber).filter(InboundNumber.service_id == service_id)
stmt = select(InboundNumber).where(InboundNumber.service_id == service_id)
inbound_number = db.session.execute(stmt).scalars().first()
inbound_number.active = active

View File

@@ -20,15 +20,15 @@ def dao_get_inbound_sms_for_service(
):
q = (
select(InboundSms)
.filter(InboundSms.service_id == service_id)
.where(InboundSms.service_id == service_id)
.order_by(InboundSms.created_at.desc())
)
if limit_days is not None:
start_date = midnight_n_days_ago(limit_days)
q = q.filter(InboundSms.created_at >= start_date)
q = q.where(InboundSms.created_at >= start_date)
if user_number:
q = q.filter(InboundSms.user_number == user_number)
q = q.where(InboundSms.user_number == user_number)
if limit:
q = q.limit(limit)
@@ -47,7 +47,7 @@ def dao_get_paginated_inbound_sms_for_service_for_public_api(
if older_than:
older_than_created_at = (
db.session.query(InboundSms.created_at)
.filter(InboundSms.id == older_than)
.where(InboundSms.id == older_than)
.scalar_subquery()
)
filters.append(InboundSms.created_at < older_than_created_at)
@@ -72,7 +72,7 @@ def dao_count_inbound_sms_for_service(service_id, limit_days):
stmt = (
select(func.count())
.select_from(InboundSms)
.filter(
.where(
InboundSms.service_id == service_id,
InboundSms.created_at >= midnight_n_days_ago(limit_days),
)
@@ -117,7 +117,7 @@ def _delete_inbound_sms(datetime_to_delete_from, query_filter):
subquery = (
select(InboundSms.id)
.filter(InboundSms.created_at < datetime_to_delete_from, *query_filter)
.where(InboundSms.created_at < datetime_to_delete_from, *query_filter)
.limit(query_limit)
.subquery()
)
@@ -128,7 +128,7 @@ def _delete_inbound_sms(datetime_to_delete_from, query_filter):
while number_deleted > 0:
_insert_inbound_sms_history(subquery, query_limit=query_limit)
stmt = delete(InboundSms).filter(InboundSms.id.in_(subquery))
stmt = delete(InboundSms).where(InboundSms.id.in_(subquery))
number_deleted = db.session.execute(stmt).rowcount
db.session.commit()
deleted += number_deleted
@@ -145,7 +145,7 @@ def delete_inbound_sms_older_than_retention():
stmt = (
select(ServiceDataRetention)
.join(ServiceDataRetention.service)
.filter(ServiceDataRetention.notification_type == NotificationType.SMS)
.where(ServiceDataRetention.notification_type == NotificationType.SMS)
)
flexible_data_retention = db.session.execute(stmt).scalars().all()

View File

@@ -52,7 +52,7 @@ def get_invited_org_users_for_organization(organization_id):
def delete_org_invitations_created_more_than_two_days_ago():
deleted = (
db.session.query(InvitedOrganizationUser)
.filter(InvitedOrganizationUser.created_at <= utc_now() - timedelta(days=2))
.where(InvitedOrganizationUser.created_at <= utc_now() - timedelta(days=2))
.delete()
)
db.session.commit()

View File

@@ -50,7 +50,7 @@ def get_invited_users_for_service(service_id):
def expire_invitations_created_more_than_two_days_ago():
expired = (
db.session.query(InvitedUser)
.filter(
.where(
InvitedUser.created_at <= utc_now() - timedelta(days=2),
InvitedUser.status.in_((InvitedUserStatus.PENDING,)),
)

View File

@@ -21,7 +21,7 @@ from app.utils import midnight_n_days_ago, utc_now
def dao_get_notification_outcomes_for_job(service_id, job_id):
stmt = (
select(func.count(Notification.status).label("count"), Notification.status)
.filter(Notification.service_id == service_id, Notification.job_id == job_id)
.where(Notification.service_id == service_id, Notification.job_id == job_id)
.group_by(Notification.status)
)
notification_statuses = db.session.execute(stmt).all()
@@ -30,7 +30,7 @@ def dao_get_notification_outcomes_for_job(service_id, job_id):
stmt = select(
FactNotificationStatus.notification_count.label("count"),
FactNotificationStatus.notification_status.label("status"),
).filter(
).where(
FactNotificationStatus.service_id == service_id,
FactNotificationStatus.job_id == job_id,
)
@@ -44,7 +44,7 @@ def dao_get_job_by_service_id_and_job_id(service_id, job_id):
def dao_get_unfinished_jobs():
stmt = select(Job).filter(Job.processing_finished.is_(None))
stmt = select(Job).where(Job.processing_finished.is_(None))
return db.session.execute(stmt).all()
@@ -67,13 +67,13 @@ def dao_get_jobs_by_service_id(
query_filter.append(Job.job_status.in_(statuses))
total_items = db.session.execute(
select(func.count()).select_from(Job).filter(*query_filter)
select(func.count()).select_from(Job).where(*query_filter)
).scalar_one()
offset = (page - 1) * page_size
stmt = (
select(Job)
.filter(*query_filter)
.where(*query_filter)
.order_by(Job.processing_started.desc(), Job.created_at.desc())
.limit(page_size)
.offset(offset)
@@ -89,7 +89,7 @@ def dao_get_scheduled_job_stats(
stmt = select(
func.count(Job.id),
func.min(Job.scheduled_for),
).filter(
).where(
Job.service_id == service_id,
Job.job_status == JobStatus.SCHEDULED,
)
@@ -117,7 +117,7 @@ def dao_set_scheduled_jobs_to_pending():
"""
stmt = (
select(Job)
.filter(
.where(
Job.job_status == JobStatus.SCHEDULED,
Job.scheduled_for < utc_now(),
)
@@ -136,7 +136,7 @@ def dao_set_scheduled_jobs_to_pending():
def dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id):
stmt = select(Job).filter(
stmt = select(Job).where(
Job.service_id == service_id,
Job.id == job_id,
Job.job_status == JobStatus.SCHEDULED,
@@ -177,7 +177,7 @@ def dao_update_job(job):
def dao_get_jobs_older_than_data_retention(notification_types):
stmt = select(ServiceDataRetention).filter(
stmt = select(ServiceDataRetention).where(
ServiceDataRetention.notification_type.in_(notification_types)
)
flexible_data_retention = db.session.execute(stmt).scalars().all()
@@ -188,7 +188,7 @@ def dao_get_jobs_older_than_data_retention(notification_types):
stmt = (
select(Job)
.join(Template)
.filter(
.where(
func.coalesce(Job.scheduled_for, Job.created_at) < end_date,
Job.archived == False, # noqa
Template.template_type == f.notification_type,
@@ -209,7 +209,7 @@ def dao_get_jobs_older_than_data_retention(notification_types):
stmt = (
select(Job)
.join(Template)
.filter(
.where(
func.coalesce(Job.scheduled_for, Job.created_at) < end_date,
Job.archived == False, # noqa
Template.template_type == notification_type,
@@ -229,7 +229,7 @@ def find_jobs_with_missing_rows():
yesterday = utc_now() - timedelta(days=1)
jobs_with_rows_missing = (
select(Job)
.filter(
.where(
Job.job_status == JobStatus.FINISHED,
Job.processing_finished < ten_minutes_ago,
Job.processing_finished > yesterday,
@@ -258,6 +258,6 @@ def find_missing_row_for_job(job_id, job_size):
Notification.job_id == job_id,
),
)
.filter(Notification.job_row_number == None) # noqa
.where(Notification.job_row_number == None) # noqa
)
return db.session.execute(query).all()

View File

@@ -30,7 +30,7 @@ from notifications_utils.recipients import (
def dao_get_last_date_template_was_used(template_id, service_id):
last_date_from_notifications = (
db.session.query(functions.max(Notification.created_at))
.filter(
.where(
Notification.service_id == service_id,
Notification.template_id == template_id,
Notification.key_type != KeyType.TEST,
@@ -43,7 +43,7 @@ def dao_get_last_date_template_was_used(template_id, service_id):
last_date = (
db.session.query(functions.max(FactNotificationStatus.local_date))
.filter(
.where(
FactNotificationStatus.template_id == template_id,
FactNotificationStatus.key_type != KeyType.TEST,
)
@@ -126,9 +126,7 @@ def update_notification_status_by_id(
notification_id, status, sent_by=None, provider_response=None, carrier=None
):
stmt = (
select(Notification)
.with_for_update()
.filter(Notification.id == notification_id)
select(Notification).with_for_update().where(Notification.id == notification_id)
)
notification = db.session.execute(stmt).scalars().first()
@@ -173,7 +171,7 @@ def update_notification_status_by_id(
@autocommit
def update_notification_status_by_reference(reference, status):
# this is used to update emails
stmt = select(Notification).filter(Notification.reference == reference)
stmt = select(Notification).where(Notification.reference == reference)
notification = db.session.execute(stmt).scalars().first()
if not notification:
@@ -271,7 +269,7 @@ def get_notification_by_id(notification_id, service_id=None, _raise=False):
if service_id:
filters.append(Notification.service_id == service_id)
stmt = select(Notification).filter(*filters)
stmt = select(Notification).where(*filters)
return (
db.session.execute(stmt).scalars().one()
@@ -307,7 +305,7 @@ def get_notifications_for_service(
if older_than is not None:
older_than_created_at = (
db.session.query(Notification.created_at)
.filter(Notification.id == older_than)
.where(Notification.id == older_than)
.as_scalar()
)
filters.append(Notification.created_at < older_than_created_at)
@@ -457,7 +455,7 @@ def move_notifications_to_notification_history(
deleted += delete_count_per_call
# Deleting test Notifications, test notifications are not persisted to NotificationHistory
stmt = delete(Notification).filter(
stmt = delete(Notification).where(
Notification.notification_type == notification_type,
Notification.service_id == service_id,
Notification.created_at < timestamp_to_delete_backwards_from,
@@ -471,7 +469,7 @@ def move_notifications_to_notification_history(
@autocommit
def dao_delete_notifications_by_id(notification_id):
db.session.query(Notification).filter(Notification.id == notification_id).delete(
db.session.query(Notification).where(Notification.id == notification_id).delete(
synchronize_session="fetch"
)
@@ -487,7 +485,7 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
stmt = (
select(Notification)
.filter(
.where(
Notification.created_at < cutoff_time,
Notification.status.in_(current_statuses),
Notification.notification_type.in_(
@@ -500,7 +498,7 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
stmt = (
update(Notification)
.filter(Notification.id.in_([n.id for n in notifications]))
.where(Notification.id.in_([n.id for n in notifications]))
.values({"status": new_status, "updated_at": updated_at})
)
db.session.execute(stmt)
@@ -513,7 +511,7 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
def dao_update_notifications_by_reference(references, update_dict):
stmt = (
update(Notification)
.filter(Notification.reference.in_(references))
.where(Notification.reference.in_(references))
.values(update_dict)
)
result = db.session.execute(stmt)
@@ -523,7 +521,7 @@ def dao_update_notifications_by_reference(references, update_dict):
if updated_count != len(references):
stmt = (
update(NotificationHistory)
.filter(NotificationHistory.reference.in_(references))
.where(NotificationHistory.reference.in_(references))
.values(update_dict)
)
result = db.session.execute(stmt)
@@ -586,7 +584,7 @@ def dao_get_notifications_by_recipient_or_reference(
results = (
db.session.query(Notification)
.filter(*filters)
.where(*filters)
.order_by(desc(Notification.created_at))
.paginate(page=page, per_page=page_size, count=False, error_out=error_out)
)
@@ -594,7 +592,7 @@ def dao_get_notifications_by_recipient_or_reference(
def dao_get_notification_by_reference(reference):
stmt = select(Notification).filter(Notification.reference == reference)
stmt = select(Notification).where(Notification.reference == reference)
return db.session.execute(stmt).scalars().one()
@@ -602,10 +600,10 @@ def dao_get_notification_history_by_reference(reference):
try:
# This try except is necessary because in test keys and research mode does not create notification history.
# Otherwise we could just search for the NotificationHistory object
stmt = select(Notification).filter(Notification.reference == reference)
stmt = select(Notification).where(Notification.reference == reference)
return db.session.execute(stmt).scalars().one()
except NoResultFound:
stmt = select(NotificationHistory).filter(
stmt = select(NotificationHistory).where(
NotificationHistory.reference == reference
)
return db.session.execute(stmt).scalars().one()
@@ -648,7 +646,7 @@ def dao_get_notifications_processing_time_stats(start_date, end_date):
def dao_get_last_notification_added_for_job_id(job_id):
stmt = (
select(Notification)
.filter(Notification.job_id == job_id)
.where(Notification.job_id == job_id)
.order_by(Notification.job_row_number.desc())
)
last_notification_added = db.session.execute(stmt).scalars().first()
@@ -659,7 +657,7 @@ def dao_get_last_notification_added_for_job_id(job_id):
def notifications_not_yet_sent(should_be_sending_after_seconds, notification_type):
older_than_date = utc_now() - timedelta(seconds=should_be_sending_after_seconds)
stmt = select(Notification).filter(
stmt = select(Notification).where(
Notification.created_at <= older_than_date,
Notification.notification_type == notification_type,
Notification.status == NotificationStatus.CREATED,
@@ -691,7 +689,7 @@ def get_service_ids_with_notifications_before(notification_type, timestamp):
return {
row.service_id
for row in db.session.query(Notification.service_id)
.filter(
.where(
Notification.notification_type == notification_type,
Notification.created_at < timestamp,
)
@@ -705,7 +703,7 @@ def get_service_ids_with_notifications_on_date(notification_type, date):
notification_table_query = db.session.query(
Notification.service_id.label("service_id")
).filter(
).where(
Notification.notification_type == notification_type,
# using >= + < is much more efficient than date(created_at)
Notification.created_at >= start_date,
@@ -716,7 +714,7 @@ def get_service_ids_with_notifications_on_date(notification_type, date):
# provided the task to populate it has run before they were archived.
ft_status_table_query = db.session.query(
FactNotificationStatus.service_id.label("service_id")
).filter(
).where(
FactNotificationStatus.notification_type == notification_type,
FactNotificationStatus.local_date == date,
)

View File

@@ -17,7 +17,7 @@ def dao_count_organizations_with_live_services():
stmt = (
select(func.count(func.distinct(Organization.id)))
.join(Organization.services)
.filter(
.where(
Service.active.is_(True),
Service.restricted.is_(False),
Service.count_as_live.is_(True),
@@ -59,9 +59,7 @@ def dao_get_organization_by_email_address(email_address):
def dao_get_organization_by_service_id(service_id):
stmt = (
select(Organization)
.join(Organization.services)
.where(Service.id == service_id)
select(Organization).join(Organization.services).where(Service.id == service_id)
)
return db.session.execute(stmt).scalars().first()
@@ -127,7 +125,7 @@ def dao_get_users_for_organization(organization_id):
return (
db.session.query(User)
.join(User.organizations)
.filter(Organization.id == organization_id, User.state == "active")
.where(Organization.id == organization_id, User.state == "active")
.order_by(User.created_at)
.all()
)

View File

@@ -109,7 +109,7 @@ def dao_get_provider_stats():
"current_month_billable_sms"
),
)
.filter(
.where(
FactBilling.notification_type == NotificationType.SMS,
FactBilling.local_date >= first_day_of_the_month,
)

View File

@@ -10,7 +10,7 @@ from app.models import ServiceEmailReplyTo
def dao_get_reply_to_by_service_id(service_id):
reply_to = (
db.session.query(ServiceEmailReplyTo)
.filter(
.where(
ServiceEmailReplyTo.service_id == service_id,
ServiceEmailReplyTo.archived == False, # noqa
)
@@ -25,7 +25,7 @@ def dao_get_reply_to_by_service_id(service_id):
def dao_get_reply_to_by_id(service_id, reply_to_id):
reply_to = (
db.session.query(ServiceEmailReplyTo)
.filter(
.where(
ServiceEmailReplyTo.service_id == service_id,
ServiceEmailReplyTo.id == reply_to_id,
ServiceEmailReplyTo.archived == False, # noqa

View File

@@ -7,7 +7,7 @@ from app.models import ServicePermission
def dao_fetch_service_permissions(service_id):
stmt = select(ServicePermission).filter(ServicePermission.service_id == service_id)
stmt = select(ServicePermission).where(ServicePermission.service_id == service_id)
return db.session.execute(stmt).scalars().all()

View File

@@ -17,7 +17,7 @@ def dao_get_active_service_users(service_id):
stmt = (
select(ServiceUser)
.join(User, User.id == ServiceUser.user_id)
.filter(User.state == "active", ServiceUser.service_id == service_id)
.where(User.state == "active", ServiceUser.service_id == service_id)
)
return db.session.execute(stmt).scalars().all()

View File

@@ -96,7 +96,7 @@ def dao_fetch_live_services_data():
this_year_ft_billing = (
select(FactBilling)
.filter(
.where(
FactBilling.local_date >= year_start_date,
FactBilling.local_date <= year_end_date,
)
@@ -145,7 +145,7 @@ def dao_fetch_live_services_data():
this_year_ft_billing, Service.id == this_year_ft_billing.c.service_id
)
.outerjoin(User, Service.go_live_user_id == User.id)
.filter(
.where(
Service.count_as_live.is_(True),
Service.active.is_(True),
Service.restricted.is_(False),
@@ -221,7 +221,7 @@ def dao_fetch_service_by_id_with_api_keys(service_id, only_active=False):
.options(joinedload(Service.api_keys))
)
if only_active:
stmt = stmt.filter(Service.active)
stmt = stmt.where(Service.active)
return db.session.execute(stmt).scalars().unique().one()
@@ -229,12 +229,12 @@ def dao_fetch_all_services_by_user(user_id, only_active=False):
stmt = (
select(Service)
.filter(Service.users.any(id=user_id))
.where(Service.users.any(id=user_id))
.order_by(asc(Service.created_at))
.options(joinedload(Service.users))
)
if only_active:
stmt = stmt.filter(Service.active)
stmt = stmt.where(Service.active)
return db.session.execute(stmt).scalars().unique().all()
@@ -262,7 +262,7 @@ def dao_archive_service(service_id):
joinedload(Service.templates).subqueryload(Template.template_redacted),
joinedload(Service.api_keys),
)
.filter(Service.id == service_id)
.where(Service.id == service_id)
)
service = db.session.execute(stmt).scalars().unique().one()
@@ -283,7 +283,7 @@ def dao_fetch_service_by_id_and_user(service_id, user_id):
stmt = (
select(Service)
.filter(Service.users.any(id=user_id), Service.id == service_id)
.where(Service.users.any(id=user_id), Service.id == service_id)
.options(joinedload(Service.users))
)
result = db.session.execute(stmt).scalar_one()
@@ -396,7 +396,7 @@ def delete_service_and_all_associated_db_objects(service):
subq = select(Template.id).where(Template.service == service).subquery()
stmt = delete(TemplateRedacted).filter(TemplateRedacted.template_id.in_(subq))
stmt = delete(TemplateRedacted).where(TemplateRedacted.template_id.in_(subq))
_delete_commit(stmt)
_delete_commit(delete(ServiceSmsSender).where(ServiceSmsSender.service == service))
@@ -426,7 +426,7 @@ def delete_service_and_all_associated_db_objects(service):
_delete_commit(delete(AnnualBilling).where(AnnualBilling.service_id == service.id))
stmt = (
select(VerifyCode).join(User).filter(User.id.in_([x.id for x in service.users]))
select(VerifyCode).join(User).where(User.id.in_([x.id for x in service.users]))
)
verify_codes = db.session.execute(stmt).scalars().all()
list(map(db.session.delete, verify_codes))
@@ -452,7 +452,7 @@ def dao_fetch_todays_stats_for_service(service_id):
Notification.status,
func.count(Notification.id).label("count"),
)
.filter(
.where(
Notification.service_id == service_id,
Notification.key_type != KeyType.TEST,
Notification.created_at >= start_date,
@@ -476,7 +476,7 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
func.count(NotificationAllTimeView.id).label("count"),
)
.filter(
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type != KeyType.TEST,
NotificationAllTimeView.created_at >= start_date,
@@ -505,7 +505,7 @@ def dao_fetch_stats_for_service_from_days_for_user(
func.count(NotificationAllTimeView.id).label("count"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type != KeyType.TEST,
NotificationAllTimeView.created_at >= start_date,
@@ -535,7 +535,7 @@ def dao_fetch_todays_stats_for_all_services(
Notification.service_id,
func.count(Notification.id).label("count"),
)
.filter(
.where(
Notification.created_at >= start_date, Notification.created_at < end_date
)
.group_by(
@@ -544,7 +544,7 @@ def dao_fetch_todays_stats_for_all_services(
)
if not include_from_test_key:
substmt = substmt.filter(Notification.key_type != KeyType.TEST)
substmt = substmt.where(Notification.key_type != KeyType.TEST)
substmt = substmt.subquery()
@@ -564,7 +564,7 @@ def dao_fetch_todays_stats_for_all_services(
)
if only_active:
stmt = stmt.filter(Service.active)
stmt = stmt.where(Service.active)
return db.session.execute(stmt).all()
@@ -579,7 +579,7 @@ def dao_suspend_service(service_id):
stmt = (
select(Service)
.options(joinedload(Service.api_keys))
.filter(Service.id == service_id)
.where(Service.id == service_id)
)
service = db.session.execute(stmt).scalars().unique().one()
@@ -612,7 +612,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
Notification.service_id.label("service_id"),
func.count(Notification.id).label("notification_count"),
)
.filter(
.where(
Notification.service_id == Service.id,
Notification.created_at >= start_date,
Notification.created_at <= end_date,
@@ -636,7 +636,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
func.count(Notification.id).label("total_count"),
Notification.service_id.label("service_id"),
)
.filter(
.where(
Notification.service_id == Service.id,
Notification.created_at >= start_date,
Notification.created_at <= end_date,
@@ -664,7 +664,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
).label("permanent_failure_rate"),
)
.join(substmt, substmt.c.service_id == Notification.service_id)
.filter(
.where(
Notification.service_id == Service.id,
Notification.created_at >= start_date,
Notification.created_at <= end_date,
@@ -696,7 +696,7 @@ def get_live_services_with_organization():
)
.select_from(Service)
.outerjoin(Service.organization)
.filter(
.where(
Service.count_as_live.is_(True),
Service.active.is_(True),
Service.restricted.is_(False),
@@ -718,7 +718,7 @@ def fetch_notification_stats_for_service_by_month_by_user(
(NotificationAllTimeView.status).label("notification_status"),
func.count(NotificationAllTimeView.id).label("count"),
)
.filter(
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,

View File

@@ -6,14 +6,14 @@ from app.models import TemplateFolder
def dao_get_template_folder_by_id_and_service_id(template_folder_id, service_id):
stmt = select(TemplateFolder).filter(
stmt = select(TemplateFolder).where(
TemplateFolder.id == template_folder_id, TemplateFolder.service_id == service_id
)
return db.session.execute(stmt).scalars().one()
def dao_get_valid_template_folders_by_id(folder_ids):
stmt = select(TemplateFolder).filter(TemplateFolder.id.in_(folder_ids))
stmt = select(TemplateFolder).where(TemplateFolder.id.in_(folder_ids))
return db.session.execute(stmt).scalars().all()

View File

@@ -54,7 +54,7 @@ def get_login_gov_user(login_uuid, email_address):
return user
# Remove this 1 July 2025, all users should have login.gov uuids by now
stmt = select(User).filter(User.email_address.ilike(email_address))
stmt = select(User).where(User.email_address.ilike(email_address))
user = db.session.execute(stmt).scalars().first()
if user:
@@ -113,7 +113,7 @@ def get_user_code(user, code, code_type):
def delete_codes_older_created_more_than_a_day_ago():
stmt = delete(VerifyCode).filter(
stmt = delete(VerifyCode).where(
VerifyCode.created_at < utc_now() - timedelta(hours=24)
)
@@ -141,7 +141,7 @@ def delete_user_verify_codes(user):
def count_user_verify_codes(user):
stmt = select(func.count(VerifyCode.id)).filter(
stmt = select(func.count(VerifyCode.id)).where(
VerifyCode.user == user,
VerifyCode.expiry_datetime > utc_now(),
VerifyCode.code_used.is_(False),
@@ -163,13 +163,13 @@ def get_users():
def get_user_by_email(email):
stmt = select(User).filter(func.lower(User.email_address) == func.lower(email))
stmt = select(User).where(func.lower(User.email_address) == func.lower(email))
return db.session.execute(stmt).scalars().one()
def get_users_by_partial_email(email):
email = escape_special_characters(email)
stmt = select(User).filter(User.email_address.ilike("%{}%".format(email)))
stmt = select(User).where(User.email_address.ilike("%{}%".format(email)))
return db.session.execute(stmt).scalars().all()
@@ -200,7 +200,7 @@ def get_user_and_accounts(user_id):
# that we have put is functionally doing the same thing as before
stmt = (
select(User)
.filter(User.id == user_id)
.where(User.id == user_id)
.options(
# eagerly load the user's services and organizations, and also the service's org and vice versa
# (so we can see if the user knows about it)