notify-api-861 show eastern time

This commit is contained in:
Kenneth Kehl
2023-11-16 12:24:27 -08:00
parent 0b4981b25c
commit 36987e7202
19 changed files with 237 additions and 152 deletions

View File

@@ -17,6 +17,7 @@ from notifications_utils.formatters import nl2br as utils_nl2br
from notifications_utils.recipients import InvalidPhoneError, validate_phone_number
from notifications_utils.take import Take
from app.utils.csv import get_user_preferred_timezone
from app.utils.time import parse_naive_dt
@@ -31,13 +32,14 @@ def convert_to_boolean(value):
def format_datetime(date):
return "{} at {} UTC".format(format_date(date), format_time_24h(date))
return "{} at {} {}".format(
format_date(date), format_time_24h(date), get_user_preferred_timezone()
)
def format_datetime_24h(date):
return "{} at {} UTC".format(
format_date(date),
format_time_24h(date),
return "{} at {} {}".format(
format_date(date), format_time_24h(date), get_user_preferred_timezone()
)
@@ -46,39 +48,50 @@ def format_time(date):
def format_datetime_normal(date):
return "{} at {} UTC".format(format_date_normal(date), format_time_24h(date))
return "{} at {} {}".format(
format_date_normal(date), format_time_24h(date), get_user_preferred_timezone()
)
def format_datetime_short(date):
return "{} at {} UTC".format(format_date_short(date), format_time_24h(date))
return "{} at {} {}".format(
format_date_short(date), format_time_24h(date), get_user_preferred_timezone()
)
def format_datetime_relative(date):
return "{} at {} UTC".format(get_human_day(date), format_time_24h(date))
return "{} at {} {}".format(
get_human_day(date), format_time_24h(date), get_user_preferred_timezone()
)
def format_datetime_numeric(date):
return "{} {} UTC".format(
format_date_numeric(date),
format_time_24h(date),
return "{} {} {}".format(
format_date_numeric(date), format_time_24h(date), get_user_preferred_timezone()
)
def format_date_numeric(date):
date = parse_naive_dt(date)
return date.strftime("%Y-%m-%d")
et = pytz.timezone(get_user_preferred_timezone())
return date.replace(tzinfo=timezone.utc).astimezone(et).strftime("%Y-%m-%d")
def format_time_24h(date):
date = parse_naive_dt(date)
return date.strftime("%H:%M")
et = pytz.timezone(get_user_preferred_timezone())
return date.replace(tzinfo=timezone.utc).astimezone(et).strftime("%H:%M")
def get_human_day(time, date_prefix=""):
# Add 1 minute to transform 00:00 into midnight today instead of midnight tomorrow
time = parse_naive_dt(time)
et = pytz.timezone(get_user_preferred_timezone())
time = time.replace(tzinfo=timezone.utc).astimezone(et)
date = (time - timedelta(minutes=1)).date()
now = datetime.now(pytz.utc)
now = datetime.now(et)
if date == (now + timedelta(days=1)).date():
return "tomorrow"
@@ -100,7 +113,8 @@ def get_human_day(time, date_prefix=""):
def format_date(date):
date = parse_naive_dt(date)
return date.strftime("%A %d %B %Y")
et = pytz.timezone(get_user_preferred_timezone())
return date.replace(tzinfo=timezone.utc).astimezone(et).strftime("%A %d %B %Y")
def format_date_normal(date):
@@ -110,7 +124,8 @@ def format_date_normal(date):
def format_date_short(date):
date = parse_naive_dt(date)
return _format_datetime_short(date)
et = pytz.timezone(get_user_preferred_timezone())
return _format_datetime_short(date.replace(tzinfo=timezone.utc).astimezone(et))
def format_date_human(date):
@@ -118,15 +133,17 @@ def format_date_human(date):
def format_datetime_human(date, date_prefix=""):
return "{} at {} UTC".format(
return "{} at {} {}".format(
get_human_day(date, date_prefix="on"),
format_time_24h(date),
get_user_preferred_timezone(),
)
def format_day_of_week(date):
date = parse_naive_dt(date)
return date.strftime("%A")
et = pytz.timezone(get_user_preferred_timezone())
return date.replace(tzinfo=timezone.utc).astimezone(et).strftime("%A")
def _format_datetime_short(datetime):

View File

@@ -64,15 +64,18 @@ from app.main.validators import (
from app.models.feedback import PROBLEM_TICKET_TYPE, QUESTION_TICKET_TYPE
from app.models.organization import Organization
from app.utils import branding, merge_jsonlike
from app.utils.csv import get_user_preferred_timezone
from app.utils.user_permissions import all_ui_permissions, permission_options
def get_time_value_and_label(future_time):
et = pytz.timezone(get_user_preferred_timezone())
return (
future_time.astimezone(pytz.utc).replace(tzinfo=None).isoformat(),
"{} at {} UTC".format(
get_human_day(future_time.astimezone(pytz.utc)),
get_human_time(future_time.astimezone(pytz.utc)),
future_time.astimezone(et).replace(tzinfo=None).isoformat(),
"{} at {} {}".format(
get_human_day(future_time.astimezone(et)),
get_human_time(future_time.astimezone(et)),
get_user_preferred_timezone(),
),
)
@@ -85,21 +88,25 @@ def get_human_time(time):
def get_human_day(time, prefix_today_with="T"):
# Add 1 hour to get midnight today instead of midnight tomorrow
et = pytz.timezone(get_user_preferred_timezone())
time = (time - timedelta(hours=1)).strftime("%A")
if time == datetime.now(pytz.utc).strftime("%A"):
if time == datetime.now(et).strftime("%A"):
return "{}oday".format(prefix_today_with)
if time == (datetime.now(pytz.utc) + timedelta(days=1)).strftime("%A"):
if time == (datetime.now(et) + timedelta(days=1)).strftime("%A"):
return "Tomorrow"
return time
def get_furthest_possible_scheduled_time():
# We want local time to find date boundaries
return (datetime.now(pytz.utc) + timedelta(days=4)).replace(hour=0)
et = pytz.timezone(get_user_preferred_timezone())
return (datetime.now(et) + timedelta(days=4)).replace(hour=0)
def get_next_hours_until(until):
now = datetime.now(pytz.utc)
et = pytz.timezone(get_user_preferred_timezone())
now = datetime.now(et)
hours = int((until - now).total_seconds() / (60 * 60))
return [
(now + timedelta(hours=i)).replace(minute=0, second=0, microsecond=0)
@@ -108,7 +115,8 @@ def get_next_hours_until(until):
def get_next_days_until(until):
now = datetime.now(pytz.utc)
et = pytz.timezone(get_user_preferred_timezone())
now = datetime.now(et)
days = int((until - now).total_seconds() / (60 * 60 * 24))
return [
get_human_day((now + timedelta(days=i)), prefix_today_with="Later t")

View File

@@ -8,13 +8,15 @@ from flask import render_template
from app import performance_dashboard_api_client, status_api_client
from app.main import main
from app.utils.csv import get_user_preferred_timezone
@main.route("/performance")
def performance():
et = pytz.timezone(get_user_preferred_timezone())
stats = performance_dashboard_api_client.get_performance_dashboard_stats(
start_date=(datetime.now(pytz.utc) - timedelta(days=7)).date(),
end_date=datetime.now(pytz.utc).date(),
start_date=(datetime.now(et) - timedelta(days=7)).date(),
end_date=datetime.now(et).date(),
)
stats["organizations_using_notify"] = sorted(
[
@@ -35,4 +37,5 @@ def performance():
stats[
"count_of_live_services_and_organizations"
] = status_api_client.get_count_of_live_services_and_organizations()
return render_template("views/performance.html", **stats)

View File

@@ -47,12 +47,14 @@ class User(JSONModel, UserMixin):
"password_changed_at",
"permissions",
"state",
"preferred_timezone",
}
def __init__(self, _dict):
super().__init__(_dict)
self.permissions = _dict.get("permissions", {})
self._platform_admin = _dict["platform_admin"]
self.preferred_timezone = "US/Eastern"
@classmethod
def from_id(cls, user_id):

View File

@@ -1,8 +1,11 @@
from datetime import datetime
from functools import reduce
import pytz
from dateutil import parser
from app.utils.csv import get_user_preferred_timezone
def sum_of_statistics(delivery_statistics):
statistics_keys = (
@@ -24,6 +27,7 @@ def sum_of_statistics(delivery_statistics):
def add_rates_to(delivery_statistics):
et = pytz.timezone(get_user_preferred_timezone())
return dict(
emails_failure_rate=get_formatted_percentage(
delivery_statistics["emails_failed"],
@@ -33,7 +37,7 @@ def add_rates_to(delivery_statistics):
delivery_statistics["sms_failed"], delivery_statistics["sms_requested"]
),
week_end_datetime=parser.parse(
delivery_statistics.get("week_end", str(datetime.utcnow()))
delivery_statistics.get("week_end", str(datetime.now(et)))
),
**delivery_statistics
)

View File

@@ -1,3 +1,6 @@
import datetime
import pytz
from flask import current_app
from notifications_utils.recipients import RecipientCSV
@@ -59,6 +62,10 @@ def get_errors_for_csv(recipients, template_type):
return errors
def get_user_preferred_timezone():
return "US/Eastern"
def generate_notifications_csv(**kwargs):
from app import notification_api_client
from app.s3_client.s3_csv_client import s3download
@@ -108,6 +115,10 @@ def generate_notifications_csv(**kwargs):
**kwargs
)
for notification in notifications_resp["notifications"]:
preferred_tz_created_at = convert_report_date_to_preferred_timezone(
notification["created_at"]
)
current_app.logger.info(f"\n\n{notification}")
if kwargs.get("job_id"):
values = (
@@ -126,7 +137,7 @@ def generate_notifications_csv(**kwargs):
notification["carrier"],
notification["provider_response"],
notification["status"],
notification["created_at"],
preferred_tz_created_at,
]
)
else:
@@ -139,7 +150,7 @@ def generate_notifications_csv(**kwargs):
notification["carrier"],
notification["provider_response"],
notification["status"],
notification["created_at"],
preferred_tz_created_at,
]
yield Spreadsheet.from_rows([map(str, values)]).as_csv_data
@@ -148,3 +159,16 @@ def generate_notifications_csv(**kwargs):
else:
return
raise Exception("Should never reach here")
def convert_report_date_to_preferred_timezone(db_date_str_in_utc):
date_arr = db_date_str_in_utc.split(" ")
db_date_str_in_utc = f"{date_arr[0]}T{date_arr[1]}+00:00"
utc_date_obj = datetime.datetime.fromisoformat(db_date_str_in_utc)
utc_date_obj = utc_date_obj.astimezone(pytz.utc)
preferred_timezone = pytz.timezone(get_user_preferred_timezone())
preferred_date_obj = utc_date_obj.astimezone(preferred_timezone)
preferred_tz_created_at = preferred_date_obj.strftime("%Y-%m-%d %H:%M:%S")
return f"{preferred_tz_created_at} {get_user_preferred_timezone()}"

View File

@@ -3,9 +3,12 @@ from datetime import datetime
import pytz
from dateutil import parser
from app.utils.csv import get_user_preferred_timezone
def get_current_financial_year():
now = datetime.now(pytz.utc)
et = pytz.timezone(get_user_preferred_timezone())
now = datetime.now(et)
current_month = int(now.strftime("%-m"))
current_year = int(now.strftime("%Y"))
return current_year if current_month > 9 else current_year - 1