mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-02 17:48:36 -04:00
tz utility swap and many test updates
This commit is contained in:
@@ -6,7 +6,7 @@ from sqlalchemy import desc
|
||||
from app import db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.models import Complaint
|
||||
from app.utils import get_london_midnight_in_utc
|
||||
from app.utils import get_local_midnight_in_utc
|
||||
|
||||
|
||||
@autocommit
|
||||
@@ -28,7 +28,7 @@ def fetch_complaints_by_service(service_id):
|
||||
|
||||
|
||||
def fetch_count_of_complaints(start_date, end_date):
|
||||
start_date = get_london_midnight_in_utc(start_date)
|
||||
end_date = get_london_midnight_in_utc(end_date + timedelta(days=1))
|
||||
start_date = get_local_midnight_in_utc(start_date)
|
||||
end_date = get_local_midnight_in_utc(end_date + timedelta(days=1))
|
||||
|
||||
return Complaint.query.filter(Complaint.created_at >= start_date, Complaint.created_at < end_date).count()
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
from datetime import date, datetime, time, timedelta
|
||||
from os import getenv
|
||||
|
||||
import pytz
|
||||
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
|
||||
from notifications_utils.timezones import convert_local_timezone_to_utc, convert_utc_to_local_timezone
|
||||
|
||||
|
||||
def get_months_for_financial_year(year):
|
||||
return [
|
||||
convert_bst_to_utc(month) for month in (
|
||||
convert_local_timezone_to_utc(month) for month in (
|
||||
get_months_for_year(4, 13, year)
|
||||
+ get_months_for_year(1, 4, year + 1)
|
||||
)
|
||||
if convert_bst_to_utc(month) < datetime.now()
|
||||
if convert_local_timezone_to_utc(month) < datetime.now()
|
||||
]
|
||||
|
||||
|
||||
@@ -26,8 +27,8 @@ def get_financial_year_dates(year):
|
||||
year_start_datetime, year_end_datetime = get_financial_year(year)
|
||||
|
||||
return (
|
||||
convert_utc_to_bst(year_start_datetime).date(),
|
||||
convert_utc_to_bst(year_end_datetime).date()
|
||||
convert_utc_to_local_timezone(year_start_datetime).date(),
|
||||
convert_utc_to_local_timezone(year_end_datetime).date()
|
||||
)
|
||||
|
||||
|
||||
@@ -46,7 +47,7 @@ def get_april_fools(year):
|
||||
:param year: the year to calculate the April 1, 00:00 BST for
|
||||
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
||||
"""
|
||||
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
||||
return pytz.timezone(getenv("TIMEZONE", "America/New_York")).localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
||||
tzinfo=None)
|
||||
|
||||
|
||||
@@ -60,7 +61,7 @@ def get_month_start_and_end_date_in_utc(month_year):
|
||||
_, num_days = calendar.monthrange(month_year.year, month_year.month)
|
||||
first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0)
|
||||
last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999)
|
||||
return convert_bst_to_utc(first_day), convert_bst_to_utc(last_day)
|
||||
return convert_local_timezone_to_utc(first_day), convert_local_timezone_to_utc(last_day)
|
||||
|
||||
|
||||
def get_current_financial_year_start_year():
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.timezones import convert_utc_to_bst
|
||||
from notifications_utils.timezones import convert_utc_to_local_timezone
|
||||
from sqlalchemy import Date, Integer, and_, desc, func, union
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.sql.expression import case, literal
|
||||
@@ -31,7 +31,7 @@ from app.models import (
|
||||
Rate,
|
||||
Service,
|
||||
)
|
||||
from app.utils import get_london_midnight_in_utc
|
||||
from app.utils import get_local_midnight_in_utc
|
||||
|
||||
|
||||
def fetch_sms_free_allowance_remainder_until_date(end_date):
|
||||
@@ -263,7 +263,7 @@ def fetch_monthly_billing_for_year(service_id, year):
|
||||
we also update the table on-the-fly if we need accurate data for this year.
|
||||
"""
|
||||
_, year_end = get_financial_year_dates(year)
|
||||
today = convert_utc_to_bst(datetime.utcnow()).date()
|
||||
today = convert_utc_to_local_timezone(datetime.utcnow()).date()
|
||||
|
||||
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
|
||||
if year_end >= today:
|
||||
@@ -443,8 +443,8 @@ def delete_billing_data_for_service_for_day(process_day, service_id):
|
||||
|
||||
|
||||
def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=False):
|
||||
start_date = get_london_midnight_in_utc(process_day)
|
||||
end_date = get_london_midnight_in_utc(process_day + timedelta(days=1))
|
||||
start_date = get_local_midnight_in_utc(process_day)
|
||||
end_date = get_local_midnight_in_utc(process_day + timedelta(days=1))
|
||||
current_app.logger.info("Populate ft_billing for {} to {}".format(start_date, end_date))
|
||||
transit_data = []
|
||||
if not service_id:
|
||||
@@ -581,7 +581,7 @@ def get_service_ids_that_need_billing_populated(start_date, end_date):
|
||||
def get_rate(
|
||||
non_letter_rates, letter_rates, notification_type, date, crown=None, letter_page_count=None, post_class='second'
|
||||
):
|
||||
start_of_day = get_london_midnight_in_utc(date)
|
||||
start_of_day = get_local_midnight_in_utc(date)
|
||||
|
||||
if notification_type == LETTER_TYPE:
|
||||
if letter_page_count == 0:
|
||||
@@ -820,7 +820,7 @@ def query_organisation_sms_usage_for_year(organisation_id, year):
|
||||
|
||||
def fetch_usage_year_for_organisation(organisation_id, year):
|
||||
year_start, year_end = get_financial_year_dates(year)
|
||||
today = convert_utc_to_bst(datetime.utcnow()).date()
|
||||
today = convert_utc_to_local_timezone(datetime.utcnow()).date()
|
||||
services = dao_get_organisation_live_services(organisation_id)
|
||||
|
||||
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
|
||||
|
||||
@@ -28,7 +28,7 @@ from app.models import (
|
||||
Template,
|
||||
)
|
||||
from app.utils import (
|
||||
get_london_midnight_in_utc,
|
||||
get_local_midnight_in_utc,
|
||||
get_london_month_from_utc_column,
|
||||
midnight_n_days_ago,
|
||||
)
|
||||
@@ -36,8 +36,8 @@ from app.utils import (
|
||||
|
||||
@autocommit
|
||||
def update_fact_notification_status(process_day, notification_type, service_id):
|
||||
start_date = get_london_midnight_in_utc(process_day)
|
||||
end_date = get_london_midnight_in_utc(process_day + timedelta(days=1))
|
||||
start_date = get_local_midnight_in_utc(process_day)
|
||||
end_date = get_local_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
|
||||
FactNotificationStatus.query.filter(
|
||||
@@ -112,8 +112,8 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
|
||||
Notification.status.label('notification_status'),
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
Notification.created_at >= get_london_midnight_in_utc(bst_day),
|
||||
Notification.created_at < get_london_midnight_in_utc(bst_day + timedelta(days=1)),
|
||||
Notification.created_at >= get_local_midnight_in_utc(bst_day),
|
||||
Notification.created_at < get_local_midnight_in_utc(bst_day + timedelta(days=1)),
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by(
|
||||
@@ -142,7 +142,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
|
||||
*([Notification.template_id] if by_template else []),
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
Notification.created_at >= get_london_midnight_in_utc(now),
|
||||
Notification.created_at >= get_local_midnight_in_utc(now),
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by(
|
||||
@@ -188,7 +188,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
|
||||
FactNotificationStatus.notification_status,
|
||||
FactNotificationStatus.key_type,
|
||||
)
|
||||
today = get_london_midnight_in_utc(datetime.utcnow())
|
||||
today = get_local_midnight_in_utc(datetime.utcnow())
|
||||
if start_date <= datetime.utcnow().date() <= end_date:
|
||||
stats_for_today = db.session.query(
|
||||
Notification.notification_type.cast(db.Text).label('notification_type'),
|
||||
@@ -265,7 +265,7 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_fro
|
||||
stats = stats.filter(FactNotificationStatus.key_type != KEY_TYPE_TEST)
|
||||
|
||||
if start_date <= datetime.utcnow().date() <= end_date:
|
||||
today = get_london_midnight_in_utc(datetime.utcnow())
|
||||
today = get_local_midnight_in_utc(datetime.utcnow())
|
||||
subquery = db.session.query(
|
||||
Notification.notification_type.cast(db.Text).label('notification_type'),
|
||||
Notification.status.label('status'),
|
||||
@@ -359,7 +359,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
|
||||
)
|
||||
|
||||
if start_date <= datetime.utcnow() <= end_date:
|
||||
today = get_london_midnight_in_utc(datetime.utcnow())
|
||||
today = get_local_midnight_in_utc(datetime.utcnow())
|
||||
month = get_london_month_from_utc_column(Notification.created_at)
|
||||
|
||||
stats_for_today = db.session.query(
|
||||
|
||||
@@ -12,7 +12,7 @@ from notifications_utils.recipients import (
|
||||
try_validate_and_format_phone_number,
|
||||
validate_and_format_email_address,
|
||||
)
|
||||
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
|
||||
from notifications_utils.timezones import convert_local_timezone_to_utc, convert_utc_to_local_timezone
|
||||
from sqlalchemy import and_, asc, desc, func, or_, union
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
@@ -45,7 +45,7 @@ from app.models import (
|
||||
)
|
||||
from app.utils import (
|
||||
escape_special_characters,
|
||||
get_london_midnight_in_utc,
|
||||
get_local_midnight_in_utc,
|
||||
midnight_n_days_ago,
|
||||
)
|
||||
|
||||
@@ -678,7 +678,7 @@ def dao_get_letters_to_be_printed(print_run_deadline, postage, query_limit=10000
|
||||
https://www.mail-archive.com/sqlalchemy@googlegroups.com/msg12443.html
|
||||
"""
|
||||
notifications = Notification.query.filter(
|
||||
Notification.created_at < convert_bst_to_utc(print_run_deadline),
|
||||
Notification.created_at < convert_local_timezone_to_utc(print_run_deadline),
|
||||
Notification.notification_type == LETTER_TYPE,
|
||||
Notification.status == NOTIFICATION_CREATED,
|
||||
Notification.key_type == KEY_TYPE_NORMAL,
|
||||
@@ -697,7 +697,7 @@ def dao_get_letters_and_sheets_volume_by_postage(print_run_deadline):
|
||||
func.sum(Notification.billable_units).label('sheets_count'),
|
||||
Notification.postage
|
||||
).filter(
|
||||
Notification.created_at < convert_bst_to_utc(print_run_deadline),
|
||||
Notification.created_at < convert_local_timezone_to_utc(print_run_deadline),
|
||||
Notification.notification_type == LETTER_TYPE,
|
||||
Notification.status == NOTIFICATION_CREATED,
|
||||
Notification.key_type == KEY_TYPE_NORMAL,
|
||||
@@ -711,11 +711,11 @@ def dao_get_letters_and_sheets_volume_by_postage(print_run_deadline):
|
||||
|
||||
|
||||
def dao_old_letters_with_created_status():
|
||||
yesterday_bst = convert_utc_to_bst(datetime.utcnow()) - timedelta(days=1)
|
||||
yesterday_bst = convert_utc_to_local_timezone(datetime.utcnow()) - timedelta(days=1)
|
||||
last_processing_deadline = yesterday_bst.replace(hour=17, minute=30, second=0, microsecond=0)
|
||||
|
||||
notifications = Notification.query.filter(
|
||||
Notification.created_at < convert_bst_to_utc(last_processing_deadline),
|
||||
Notification.created_at < convert_local_timezone_to_utc(last_processing_deadline),
|
||||
Notification.notification_type == LETTER_TYPE,
|
||||
Notification.status == NOTIFICATION_CREATED
|
||||
).order_by(
|
||||
@@ -785,8 +785,8 @@ def get_service_ids_with_notifications_before(notification_type, timestamp):
|
||||
|
||||
|
||||
def get_service_ids_with_notifications_on_date(notification_type, date):
|
||||
start_date = get_london_midnight_in_utc(date)
|
||||
end_date = get_london_midnight_in_utc(date + timedelta(days=1))
|
||||
start_date = get_local_midnight_in_utc(date)
|
||||
end_date = get_local_midnight_in_utc(date + timedelta(days=1))
|
||||
|
||||
notification_table_query = db.session.query(
|
||||
Notification.service_id.label('service_id')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.timezones import convert_utc_to_bst
|
||||
from notifications_utils.timezones import convert_utc_to_local_timezone
|
||||
from sqlalchemy import asc, desc, func
|
||||
|
||||
from app import db
|
||||
@@ -157,8 +157,8 @@ def _update_provider_details_without_commit(provider_details):
|
||||
def dao_get_provider_stats():
|
||||
# this query does not include the current day since the task to populate ft_billing runs overnight
|
||||
|
||||
current_bst_datetime = convert_utc_to_bst(datetime.utcnow())
|
||||
first_day_of_the_month = current_bst_datetime.date().replace(day=1)
|
||||
current_local_datetime = convert_utc_to_local_timezone(datetime.utcnow())
|
||||
first_day_of_the_month = current_local_datetime.date().replace(day=1)
|
||||
|
||||
subquery = db.session.query(
|
||||
FactBilling.provider,
|
||||
|
||||
@@ -47,7 +47,7 @@ from app.models import (
|
||||
from app.utils import (
|
||||
escape_special_characters,
|
||||
get_archived_db_column_value,
|
||||
get_london_midnight_in_utc,
|
||||
get_local_midnight_in_utc,
|
||||
)
|
||||
|
||||
DEFAULT_SERVICE_PERMISSIONS = [
|
||||
@@ -409,7 +409,7 @@ def delete_service_and_all_associated_db_objects(service):
|
||||
|
||||
def dao_fetch_todays_stats_for_service(service_id):
|
||||
today = date.today()
|
||||
start_date = get_london_midnight_in_utc(today)
|
||||
start_date = get_local_midnight_in_utc(today)
|
||||
|
||||
return db.session.query(
|
||||
Notification.notification_type,
|
||||
@@ -427,8 +427,8 @@ def dao_fetch_todays_stats_for_service(service_id):
|
||||
|
||||
def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_active=True):
|
||||
today = date.today()
|
||||
start_date = get_london_midnight_in_utc(today)
|
||||
end_date = get_london_midnight_in_utc(today + timedelta(days=1))
|
||||
start_date = get_local_midnight_in_utc(today)
|
||||
end_date = get_local_midnight_in_utc(today + timedelta(days=1))
|
||||
|
||||
subquery = db.session.query(
|
||||
Notification.notification_type,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from os import getenv
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import String, and_, desc, func, literal, text
|
||||
@@ -20,7 +21,7 @@ from app.utils import midnight_n_days_ago
|
||||
def _get_printing_day(created_at):
|
||||
return func.date_trunc(
|
||||
'day',
|
||||
func.timezone('Europe/London', func.timezone('UTC', created_at)) + text(
|
||||
func.timezone(getenv("TIMEZONE", "America/New_York"), func.timezone('UTC', created_at)) + text(
|
||||
# We add 6 hours 30 minutes to the local created_at time so that
|
||||
# any letters created after 5:30pm get shifted into the next day
|
||||
"interval '6 hours 30 minutes'"
|
||||
@@ -36,7 +37,7 @@ def _get_printing_datetime(created_at):
|
||||
|
||||
|
||||
def _naive_gmt_to_utc(column):
|
||||
return func.timezone('UTC', func.timezone('Europe/London', column))
|
||||
return func.timezone('UTC', func.timezone(getenv("TIMEZONE", "America/New_York"), column))
|
||||
|
||||
|
||||
def dao_get_uploads_by_service_id(service_id, limit_days=None, page=1, page_size=50):
|
||||
|
||||
Reference in New Issue
Block a user