mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -04:00
Changed the scheduled_for datetime to only send and hour of a day to send.
Also expect the date being passed in is BST. The date is converted to UTC before saving. And converted to BST when returning a notification.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import base64
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from app.utils import get_midnight_for_day_before, get_london_midnight_in_utc, get_utc_time_in_bst
|
||||
from app.utils import get_midnight_for_day_before, get_london_midnight_in_utc, convert_utc_time_in_bst
|
||||
|
||||
|
||||
class PerformancePlatformClient:
|
||||
@@ -27,7 +27,7 @@ class PerformancePlatformClient:
|
||||
def send_performance_stats(self, date, channel, count, period):
|
||||
if self.active:
|
||||
payload = {
|
||||
'_timestamp': get_utc_time_in_bst(date).isoformat(),
|
||||
'_timestamp': convert_utc_time_in_bst(date).isoformat(),
|
||||
'service': 'govuk-notify',
|
||||
'channel': channel,
|
||||
'count': count,
|
||||
|
||||
@@ -29,7 +29,7 @@ from app import (
|
||||
)
|
||||
|
||||
from app.history_meta import Versioned
|
||||
from app.utils import get_utc_time_in_bst
|
||||
from app.utils import convert_utc_time_in_bst
|
||||
|
||||
SMS_TYPE = 'sms'
|
||||
EMAIL_TYPE = 'email'
|
||||
@@ -832,7 +832,7 @@ class Notification(db.Model):
|
||||
}[self.template.template_type].get(self.status, self.status)
|
||||
|
||||
def serialize_for_csv(self):
|
||||
created_at_in_bst = get_utc_time_in_bst(self.created_at)
|
||||
created_at_in_bst = convert_utc_time_in_bst(self.created_at)
|
||||
serialized = {
|
||||
"row_number": '' if self.job_row_number is None else self.job_row_number + 1,
|
||||
"recipient": self.to,
|
||||
@@ -873,7 +873,7 @@ class Notification(db.Model):
|
||||
"sent_at": self.sent_at.strftime(DATETIME_FORMAT) if self.sent_at else None,
|
||||
"completed_at": self.completed_at(),
|
||||
"scheduled_for": self.scheduled_notification.scheduled_for.strftime(
|
||||
DATETIME_FORMAT) if self.scheduled_notification else None
|
||||
"%Y-%m-%d %H") if self.scheduled_notification else None
|
||||
}
|
||||
|
||||
return serialized
|
||||
|
||||
@@ -15,7 +15,7 @@ from app.dao.notifications_dao import (dao_create_notification,
|
||||
dao_created_scheduled_notification)
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE, ScheduledNotification
|
||||
from app.v2.errors import BadRequestError, SendNotificationToQueueError
|
||||
from app.utils import get_template_instance, cache_key_for_service_template_counter
|
||||
from app.utils import get_template_instance, cache_key_for_service_template_counter, convert_bst_to_utc
|
||||
|
||||
|
||||
def create_content_for_notification(template, personalisation):
|
||||
@@ -125,6 +125,7 @@ def simulated_recipient(to_address, notification_type):
|
||||
|
||||
|
||||
def persist_scheduled_notification(notification_id, scheduled_for):
|
||||
scheduled_datetime = convert_bst_to_utc(datetime.strptime(scheduled_for, "%Y-%m-%d %H"))
|
||||
scheduled_notification = ScheduledNotification(notification_id=notification_id,
|
||||
scheduled_for=scheduled_for)
|
||||
scheduled_for=scheduled_datetime)
|
||||
dao_created_scheduled_notification(scheduled_notification)
|
||||
|
||||
@@ -22,13 +22,13 @@ def validate(json_to_validate, schema):
|
||||
return True
|
||||
|
||||
@format_checker.checks('datetime', raises=ValidationError)
|
||||
def validate_schema_datetime(instance):
|
||||
def validate_schema_date_with_hour(instance):
|
||||
if isinstance(instance, str):
|
||||
try:
|
||||
datetime.strptime(instance, "%Y-%m-%d %H:%M:%S")
|
||||
datetime.strptime(instance, "%Y-%m-%d %H")
|
||||
except ValueError as e:
|
||||
raise ValidationError("datetime format is invalid. Use the format: "
|
||||
"YYYY-MM-DD HH:MM:SS, for example 2017-05-30 13:00:00")
|
||||
"YYYY-MM-DD HH, for example 2017-05-30 13")
|
||||
return True
|
||||
|
||||
validator = Draft4Validator(schema, format_checker=format_checker)
|
||||
|
||||
@@ -51,10 +51,14 @@ def get_midnight_for_day_before(date):
|
||||
return get_london_midnight_in_utc(day_before)
|
||||
|
||||
|
||||
def get_utc_time_in_bst(utc_dt):
|
||||
def convert_utc_time_in_bst(utc_dt):
|
||||
return pytz.utc.localize(utc_dt).astimezone(local_timezone).replace(tzinfo=None)
|
||||
|
||||
|
||||
def convert_bst_to_utc(date):
|
||||
return local_timezone.localize(date).astimezone(pytz.UTC).replace(tzinfo=None)
|
||||
|
||||
|
||||
def get_london_month_from_utc_column(column):
|
||||
"""
|
||||
Where queries need to count notifications by month it needs to be
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
|
||||
from app.schema_validation.definitions import (uuid, personalisation)
|
||||
|
||||
# this may belong in a templates module
|
||||
|
||||
template = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "template schema",
|
||||
@@ -204,8 +201,7 @@ def create_post_sms_response_from_notification(notification, body, from_number,
|
||||
"template": __create_template_from_notification(notification=notification,
|
||||
url_root=url_root,
|
||||
service_id=service_id),
|
||||
"scheduled_for": datetime.strptime(scheduled_for,
|
||||
"%Y-%m-%d %H:%M:%S").strftime(DATETIME_FORMAT) if scheduled_for else None
|
||||
"scheduled_for": scheduled_for if scheduled_for else None
|
||||
}
|
||||
|
||||
|
||||
@@ -223,8 +219,7 @@ def create_post_email_response_from_notification(notification, content, subject,
|
||||
"template": __create_template_from_notification(notification=notification,
|
||||
url_root=url_root,
|
||||
service_id=service_id),
|
||||
"scheduled_for": datetime.strptime(scheduled_for,
|
||||
"%Y-%m-%d %H:%M:%S").strftime(DATETIME_FORMAT) if scheduled_for else None
|
||||
"scheduled_for": scheduled_for if scheduled_for else None
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user