mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 10:28:41 -04:00
util for naive dt parsing
This commit is contained in:
+11
-10
@@ -21,6 +21,7 @@ from notifications_utils.recipients import (
|
|||||||
from notifications_utils.take import Take
|
from notifications_utils.take import Take
|
||||||
from notifications_utils.timezones import convert_utc_to_local_timezone
|
from notifications_utils.timezones import convert_utc_to_local_timezone
|
||||||
|
|
||||||
|
from app.utils.time import parse_naive_dt
|
||||||
|
|
||||||
def convert_to_boolean(value):
|
def convert_to_boolean(value):
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
@@ -75,19 +76,19 @@ def format_datetime_numeric(date):
|
|||||||
|
|
||||||
|
|
||||||
def format_date_numeric(date):
|
def format_date_numeric(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return convert_utc_to_local_timezone(date).strftime('%Y-%m-%d')
|
return convert_utc_to_local_timezone(date).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
|
||||||
def format_time_24h(date):
|
def format_time_24h(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return convert_utc_to_local_timezone(date).strftime('%H:%M')
|
return convert_utc_to_local_timezone(date).strftime('%H:%M')
|
||||||
|
|
||||||
|
|
||||||
def get_human_day(time, date_prefix=''):
|
def get_human_day(time, date_prefix=''):
|
||||||
|
|
||||||
# Add 1 minute to transform 00:00 into ‘midnight today’ instead of ‘midnight tomorrow’
|
# Add 1 minute to transform 00:00 into ‘midnight today’ instead of ‘midnight tomorrow’
|
||||||
time = dateutil.parser.parse(time, ignoretz=True)
|
time = parse_naive_dt(time)
|
||||||
date = (convert_utc_to_local_timezone(time) - timedelta(minutes=1)).date()
|
date = (convert_utc_to_local_timezone(time) - timedelta(minutes=1)).date()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ def get_human_day(time, date_prefix=''):
|
|||||||
|
|
||||||
|
|
||||||
def format_time(date):
|
def format_time(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return {
|
return {
|
||||||
'12:00AM': 'Midnight',
|
'12:00AM': 'Midnight',
|
||||||
'12:00PM': 'Noon'
|
'12:00PM': 'Noon'
|
||||||
@@ -121,17 +122,17 @@ def format_time(date):
|
|||||||
|
|
||||||
|
|
||||||
def format_date(date):
|
def format_date(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return convert_utc_to_local_timezone(date).strftime('%A %d %B %Y')
|
return convert_utc_to_local_timezone(date).strftime('%A %d %B %Y')
|
||||||
|
|
||||||
|
|
||||||
def format_date_normal(date):
|
def format_date_normal(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return convert_utc_to_local_timezone(date).strftime('%d %B %Y').lstrip('0')
|
return convert_utc_to_local_timezone(date).strftime('%d %B %Y').lstrip('0')
|
||||||
|
|
||||||
|
|
||||||
def format_date_short(date):
|
def format_date_short(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return _format_datetime_short(convert_utc_to_local_timezone(date))
|
return _format_datetime_short(convert_utc_to_local_timezone(date))
|
||||||
|
|
||||||
|
|
||||||
@@ -147,7 +148,7 @@ def format_datetime_human(date, date_prefix=''):
|
|||||||
|
|
||||||
|
|
||||||
def format_day_of_week(date):
|
def format_day_of_week(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
return convert_utc_to_local_timezone(date).strftime('%A')
|
return convert_utc_to_local_timezone(date).strftime('%A')
|
||||||
|
|
||||||
|
|
||||||
@@ -164,7 +165,7 @@ def naturaltime_without_indefinite_article(date):
|
|||||||
|
|
||||||
|
|
||||||
def format_delta(date):
|
def format_delta(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
delta = (
|
delta = (
|
||||||
datetime.now(timezone.utc)
|
datetime.now(timezone.utc)
|
||||||
) - (
|
) - (
|
||||||
@@ -178,7 +179,7 @@ def format_delta(date):
|
|||||||
|
|
||||||
|
|
||||||
def format_delta_days(date):
|
def format_delta_days(date):
|
||||||
date = dateutil.parser.parse(date, ignoretz=True)
|
date = parse_naive_dt(date)
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
date = convert_utc_to_local_timezone(date).replace(tzinfo=pytz.utc)
|
date = convert_utc_to_local_timezone(date).replace(tzinfo=pytz.utc)
|
||||||
if date.strftime('%Y-%m-%d') == now.strftime('%Y-%m-%d'):
|
if date.strftime('%Y-%m-%d') == now.strftime('%Y-%m-%d'):
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ from app.main.forms import (
|
|||||||
from app.utils import DELIVERED_STATUSES, FAILURE_STATUSES, SENDING_STATUSES
|
from app.utils import DELIVERED_STATUSES, FAILURE_STATUSES, SENDING_STATUSES
|
||||||
from app.utils.branding import NHS_EMAIL_BRANDING_ID
|
from app.utils.branding import NHS_EMAIL_BRANDING_ID
|
||||||
from app.utils.branding import get_email_choices as get_email_branding_choices
|
from app.utils.branding import get_email_choices as get_email_branding_choices
|
||||||
|
from app.utils.time import parse_naive_dt
|
||||||
from app.utils.user import (
|
from app.utils.user import (
|
||||||
user_has_permissions,
|
user_has_permissions,
|
||||||
user_is_gov_user,
|
user_is_gov_user,
|
||||||
@@ -440,7 +441,7 @@ def get_service_verify_reply_to_address_partials(service_id, notification_id):
|
|||||||
is_default=is_default
|
is_default=is_default
|
||||||
)
|
)
|
||||||
seconds_since_sending = (
|
seconds_since_sending = (
|
||||||
datetime.utcnow() - dateutil.parser.parse(notification['created_at'], ignoretz=True)
|
datetime.utcnow() - parse_naive_dt(notification['created_at'])
|
||||||
).seconds
|
).seconds
|
||||||
if notification["status"] in FAILURE_STATUSES or (
|
if notification["status"] in FAILURE_STATUSES or (
|
||||||
notification["status"] in SENDING_STATUSES and
|
notification["status"] in SENDING_STATUSES and
|
||||||
|
|||||||
+3
-2
@@ -18,6 +18,7 @@ from app.notify_client import InviteTokenError
|
|||||||
from app.notify_client.invite_api_client import invite_api_client
|
from app.notify_client.invite_api_client import invite_api_client
|
||||||
from app.notify_client.org_invite_api_client import org_invite_api_client
|
from app.notify_client.org_invite_api_client import org_invite_api_client
|
||||||
from app.notify_client.user_api_client import user_api_client
|
from app.notify_client.user_api_client import user_api_client
|
||||||
|
from app.utils.time import parse_naive_dt
|
||||||
from app.utils.user import is_gov_user
|
from app.utils.user import is_gov_user
|
||||||
from app.utils.user_permissions import (
|
from app.utils.user_permissions import (
|
||||||
all_ui_permissions,
|
all_ui_permissions,
|
||||||
@@ -127,8 +128,8 @@ class User(JSONModel, UserMixin):
|
|||||||
def password_changed_more_recently_than(self, datetime_string):
|
def password_changed_more_recently_than(self, datetime_string):
|
||||||
if not self.password_changed_at:
|
if not self.password_changed_at:
|
||||||
return False
|
return False
|
||||||
datetime_string = dateutil.parser.parse(datetime_string, ignoretz=True)
|
datetime_string = parse_naive_dt(datetime_string)
|
||||||
changed = dateutil.parser.parse(self.password_changed_at, ignoretz=True)
|
changed = parse_naive_dt(self.password_changed_at)
|
||||||
return convert_utc_to_local_timezone(
|
return convert_utc_to_local_timezone(
|
||||||
changed
|
changed
|
||||||
) > convert_utc_to_local_timezone(
|
) > convert_utc_to_local_timezone(
|
||||||
|
|||||||
@@ -18,3 +18,7 @@ def is_less_than_days_ago(date_from_db, number_of_days):
|
|||||||
return (
|
return (
|
||||||
datetime.utcnow().astimezone(pytz.utc) - parser.parse(date_from_db)
|
datetime.utcnow().astimezone(pytz.utc) - parser.parse(date_from_db)
|
||||||
).days < number_of_days
|
).days < number_of_days
|
||||||
|
|
||||||
|
|
||||||
|
def parse_naive_dt(dt):
|
||||||
|
return parser.parse(dt, ignoretz=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user