added format_datetime_scheduled_notification to formatters and removed unneccesary format changes

This commit is contained in:
Beverly Nguyen
2024-04-09 12:52:45 -07:00
parent de0aed60f9
commit 63bc85d8d4
8 changed files with 35 additions and 16 deletions

View File

@@ -52,6 +52,7 @@ from app.formatters import (
format_datetime_human,
format_datetime_normal,
format_datetime_relative,
format_datetime_scheduled_notification,
format_datetime_table,
format_day_of_week,
format_delta,
@@ -551,6 +552,7 @@ def add_template_filters(application):
format_datetime,
format_datetime_24h,
format_datetime_normal,
format_datetime_scheduled_notification,
format_datetime_table,
valid_phone_number,
linkable_name,

View File

@@ -22,7 +22,7 @@ from notifications_utils.recipients import InvalidPhoneError, validate_phone_num
from notifications_utils.take import Take
from app.utils.csv import get_user_preferred_timezone
from app.utils.time import parse_dt, parse_naive_dt
from app.utils.time import parse_naive_dt
def apply_html_class(tags, html_file):
@@ -93,16 +93,34 @@ def format_datetime_normal(date):
)
def format_datetime_scheduled_notification(date):
# e.g. April 09, 2024 at 04:00 PM US/Eastern.
# Everything except scheduled notifications, the time is always "now".
# Scheduled notifications are the exception to the rule.
# Here we are formating and displaying the datetime without converting datetime to a different timezone.
datetime_obj = parse_naive_dt(date)
format_time_without_tz = datetime_obj.replace(tzinfo=timezone.utc).strftime(
"%I:%M %p"
)
return "{} at {} {}".format(
format_date_normal(date), format_time_without_tz, get_user_preferred_timezone()
)
def format_datetime_table(date):
# example: 03-18-2024 at 04:53 PM, intended for datetimes in tables
return "{} at {}".format(format_date_numeric(date), format_time_12h(date))
def format_time_12h(date):
date = parse_dt(date)
date = parse_naive_dt(date)
preferred_tz = pytz.timezone(get_user_preferred_timezone())
return date.astimezone(preferred_tz).strftime("%I:%M %p")
return (
date.replace(tzinfo=timezone.utc).astimezone(preferred_tz).strftime("%I:%M %p")
)
def format_datetime_relative(date):

View File

@@ -650,7 +650,9 @@ def check_messages(service_id, template_id, upload_id, row_index=2):
@user_has_permissions("send_messages", restrict_admin_usage=True)
def preview_job(service_id, template_id, upload_id, row_index=2):
session["scheduled_for"] = request.form.get("scheduled_for", "")
data = _check_messages(service_id, template_id, upload_id, row_index, force_hide_sender=True)
data = _check_messages(
service_id, template_id, upload_id, row_index, force_hide_sender=True
)
return render_template(
"views/check/preview.html",
@@ -911,7 +913,9 @@ def preview_notification(service_id, template_id):
return render_template(
"views/notifications/preview.html",
**_check_notification(service_id, template_id, show_recipient=False, force_hide_sender=True),
**_check_notification(
service_id, template_id, show_recipient=False, force_hide_sender=True
),
scheduled_for=session["scheduled_for"],
recipient=recipient,
)

View File

@@ -21,7 +21,7 @@
{{ page_header('Preview') }}
<div>
<p class="sms-message-scheduler">Scheduled: {{ scheduled_for |format_datetime_normal if scheduled_for else 'Now'}}</p>
<p class="sms-message-scheduler">Scheduled: {{ scheduled_for |format_datetime_scheduled_notification if scheduled_for else 'Now'}}</p>
<p class="sms-message-file-name">File: {{original_file_name}}</p>
<p class="sms-message-template">Template: {{template.name}}</p>
<p class="sms-message-sender" >From: {{ template.sender }}</p>

View File

@@ -43,7 +43,7 @@
{{ page_header('Preview') }}
{% endif %}
<div>
<p class="sms-message-scheduler">Scheduled: {{ scheduled_for |format_datetime_normal if scheduled_for else 'Now'}}</p>
<p class="sms-message-scheduler">Scheduled: {{ scheduled_for |format_datetime_scheduled_notification if scheduled_for else 'Now'}}</p>
<p class="sms-message-template">Template: {{template.name}}</p>
<p class="sms-message-sender" >From: {{ template.sender }}</p>
<p class="sms-message-sender" >To: {{ recipient }}</p>

View File

@@ -17,7 +17,7 @@ def get_template(
redact_missing_personalisation=False,
email_reply_to=None,
sms_sender=None,
force_hide_sender=False
force_hide_sender=False,
):
if "email" == template["template_type"]:
return EmailPreviewTemplate(

View File

@@ -22,8 +22,3 @@ def is_less_than_days_ago(date_from_db, number_of_days):
def parse_naive_dt(dt):
return parser.parse(dt, ignoretz=True)
def parse_dt(dt):
# Parse datetime without ignoring the timezone
return parser.parse(dt)

View File

@@ -979,7 +979,7 @@ def test_upload_valid_csv_shows_preview_and_table(
'<td class="table-field-left-aligned"> <div> A </div> </td>',
(
'<td class="table-field-left-aligned"> '
'<div> '
"<div> "
"<ul> "
"<li>foo</li> <li>foo</li> <li>foo</li> "
"</ul> "
@@ -992,7 +992,7 @@ def test_upload_valid_csv_shows_preview_and_table(
'<td class="table-field-left-aligned"> <div> B </div> </td>',
(
'<td class="table-field-left-aligned"> '
'<div> '
"<div> "
"<ul> "
"<li>foo</li> <li>foo</li> <li>foo</li> "
"</ul> "
@@ -1005,7 +1005,7 @@ def test_upload_valid_csv_shows_preview_and_table(
'<td class="table-field-left-aligned"> <div> C </div> </td>',
(
'<td class="table-field-left-aligned"> '
'<div> '
"<div> "
"<ul> "
"<li>foo</li> <li>foo</li> "
"</ul> "