From e293f7e3f5a26a13cc04d787bd0f3d7af65629b2 Mon Sep 17 00:00:00 2001 From: Anastasia Gradova Date: Fri, 14 Jun 2024 16:01:04 -0600 Subject: [PATCH] Updated all usage of datetime.utcnow() to app.utils utc_now() function. Added new endpoint /service/{{service_id}}/notifications/month --- app/service/rest.py | 31 ++++++++++++++++++- .../versions/0025_notify_service_data.py | 23 +++++++------- .../versions/0028_fix_reg_template_history.py | 4 ++- .../versions/0082_add_golive_template.py | 4 ++- .../versions/0117_international_sms_notify.py | 4 ++- .../versions/0134_add_email_2fa_template_.py | 4 ++- .../0139_migrate_sms_allowance_data.py | 3 +- .../versions/0171_add_org_invite_template.py | 4 ++- .../0265_add_confirm_edit_templates.py | 6 ++-- .../versions/0294_add_verify_reply_to_.py | 4 ++- .../versions/0330_broadcast_invite_email.py | 4 ++- .../0347_add_dvla_volumes_template.py | 4 ++- migrations/versions/0401_add_e2e_test_user.py | 7 +++-- 13 files changed, 76 insertions(+), 26 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 372dec7a5..29408f348 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -706,7 +706,7 @@ def get_monthly_notification_stats_by_user(service_id, user_id): statistics.add_monthly_notification_status_stats(data, stats) - now = datetime.utcnow() + now = utc_now() if end_date > now: todays_deltas = fetch_notification_status_for_service_for_day( now, service_id=service_id @@ -749,6 +749,35 @@ def get_single_month_notification_stats_by_user(service_id, user_id): return jsonify(stats) +@service_blueprint.route("//notifications/month", methods=["GET"]) +def get_single_month_notification_stats_for_service(service_id): + # check service_id validity + dao_fetch_service_by_id(service_id) + + try: + month = int(request.args.get("month", "NaN")) + year = int(request.args.get("year", "NaN")) + except ValueError: + raise InvalidRequest( + "Both a month and year are required as numbers", status_code=400 + ) + + month_year = datetime(year, month, 10, 00, 00, 00) + days = get_number_of_days_for_month(year, month) + start_date, end_date = get_month_start_and_end_date_in_utc(month_year) + + stats = {} + for d in range(days): + new_date = start_date + timedelta(days=d) + if new_date <= end_date: + key = new_date.strftime("%Y-%m-%d") + stats[key] = statistics.format_statistics( + dao_fetch_stats_for_service_from_day(service_id, new_date) + ) + + return jsonify(stats) + + def get_detailed_service(service_id, today_only=False): service = dao_fetch_service_by_id(service_id) diff --git a/migrations/versions/0025_notify_service_data.py b/migrations/versions/0025_notify_service_data.py index 0683e7dd2..e90d01aad 100644 --- a/migrations/versions/0025_notify_service_data.py +++ b/migrations/versions/0025_notify_service_data.py @@ -15,6 +15,7 @@ from alembic import op from sqlalchemy import text from app.hashing import hashpw +from app.utils import utc_now revision = "0025_notify_service_data" down_revision = "0024_add_research_mode_defaults" @@ -32,7 +33,7 @@ def upgrade(): """ conn.execute( text(user_insert), - {"user_id": user_id, "time_now": datetime.utcnow(), "password": password}, + {"user_id": user_id, "time_now": utc_now(), "password": password}, ) service_history_insert = """INSERT INTO services_history (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version) VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com', @@ -41,7 +42,7 @@ def upgrade(): """ conn.execute( text(service_history_insert), - {"service_id": service_id, "time_now": datetime.utcnow(), "user_id": user_id}, + {"service_id": service_id, "time_now": utc_now(), "user_id": user_id}, ) service_insert = """INSERT INTO services (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version) VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com', @@ -49,7 +50,7 @@ def upgrade(): """ conn.execute( text(service_insert), - {"service_id": service_id, "time_now": datetime.utcnow(), "user_id": user_id}, + {"service_id": service_id, "time_now": utc_now(), "user_id": user_id}, ) user_to_service_insert = """INSERT INTO user_to_service (user_id, service_id) VALUES (:user_id, :service_id)""" conn.execute( @@ -74,7 +75,7 @@ def upgrade(): "template_id": uuid.uuid4(), "template_name": "Notify email verification code", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_verification_content, "service_id": service_id, "subject": "Confirm GOV.UK Notify registration", @@ -87,7 +88,7 @@ def upgrade(): "template_id": "ece42649-22a8-4d06-b87f-d52d5d3f0a27", "template_name": "Notify email verification code", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_verification_content, "service_id": service_id, "subject": "Confirm GOV.UK Notify registration", @@ -107,7 +108,7 @@ def upgrade(): "template_id": "4f46df42-f795-4cc4-83bb-65ca312f49cc", "template_name": "Notify invitation email", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": invitation_content, "service_id": service_id, "subject": invitation_subject, @@ -120,7 +121,7 @@ def upgrade(): "template_id": "4f46df42-f795-4cc4-83bb-65ca312f49cc", "template_name": "Notify invitation email", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": invitation_content, "service_id": service_id, "subject": invitation_subject, @@ -135,7 +136,7 @@ def upgrade(): "template_id": "36fb0730-6259-4da1-8a80-c8de22ad4246", "template_name": "Notify SMS verify code", "template_type": "sms", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": sms_code_content, "service_id": service_id, "subject": None, @@ -149,7 +150,7 @@ def upgrade(): "template_id": "36fb0730-6259-4da1-8a80-c8de22ad4246", "template_name": "Notify SMS verify code", "template_type": "sms", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": sms_code_content, "service_id": service_id, "subject": None, @@ -172,7 +173,7 @@ def upgrade(): "template_id": "474e9242-823b-4f99-813d-ed392e7f1201", "template_name": "Notify password reset email", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": password_reset_content, "service_id": service_id, "subject": "Reset your GOV.UK Notify password", @@ -186,7 +187,7 @@ def upgrade(): "template_id": "474e9242-823b-4f99-813d-ed392e7f1201", "template_name": "Notify password reset email", "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": password_reset_content, "service_id": service_id, "subject": "Reset your GOV.UK Notify password", diff --git a/migrations/versions/0028_fix_reg_template_history.py b/migrations/versions/0028_fix_reg_template_history.py index 8bf11fe59..fcbffc51a 100644 --- a/migrations/versions/0028_fix_reg_template_history.py +++ b/migrations/versions/0028_fix_reg_template_history.py @@ -11,6 +11,8 @@ from datetime import datetime from sqlalchemy import text +from app.utils import utc_now + revision = "0028_fix_reg_template_history" down_revision = "0026_rename_notify_service" @@ -38,7 +40,7 @@ def upgrade(): "id": "ece42649-22a8-4d06-b87f-d52d5d3f0a27", "name": "Notify email verification code", "type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_verification_content, "service_id": service_id, "subject": "Confirm GOV.UK Notify registration", diff --git a/migrations/versions/0082_add_golive_template.py b/migrations/versions/0082_add_golive_template.py index 55cdc3e04..97ff5b97e 100644 --- a/migrations/versions/0082_add_golive_template.py +++ b/migrations/versions/0082_add_golive_template.py @@ -14,6 +14,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0082_add_go_live_template" down_revision = "0081_noti_status_as_enum" @@ -89,7 +91,7 @@ GOV.UK Notify team "template_id": template_id, "template_name": template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": template_subject, diff --git a/migrations/versions/0117_international_sms_notify.py b/migrations/versions/0117_international_sms_notify.py index ebdbbddef..c33750af4 100644 --- a/migrations/versions/0117_international_sms_notify.py +++ b/migrations/versions/0117_international_sms_notify.py @@ -9,6 +9,8 @@ Create Date: 2017-08-29 14:09:41.042061 # revision identifiers, used by Alembic. from sqlalchemy import text +from app.utils import utc_now + revision = "0117_international_sms_notify" down_revision = "0115_add_inbound_numbers" @@ -22,7 +24,7 @@ NOTIFY_SERVICE_ID = "d6aa2c68-a2d9-4437-ab19-3ae8eb202553" def upgrade(): input_params = { "notify_service_id": NOTIFY_SERVICE_ID, - "datetime_now": datetime.utcnow(), + "datetime_now": utc_now(), } conn = op.get_bind() conn.execute( diff --git a/migrations/versions/0134_add_email_2fa_template_.py b/migrations/versions/0134_add_email_2fa_template_.py index 492281175..57809e1bc 100644 --- a/migrations/versions/0134_add_email_2fa_template_.py +++ b/migrations/versions/0134_add_email_2fa_template_.py @@ -12,6 +12,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0134_add_email_2fa_template" down_revision = "0133_set_services_sms_prefix" @@ -44,7 +46,7 @@ def upgrade(): "template_id": template_id, "template_name": template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": template_subject, diff --git a/migrations/versions/0139_migrate_sms_allowance_data.py b/migrations/versions/0139_migrate_sms_allowance_data.py index 8e7536bfb..0203f5563 100644 --- a/migrations/versions/0139_migrate_sms_allowance_data.py +++ b/migrations/versions/0139_migrate_sms_allowance_data.py @@ -13,6 +13,7 @@ from alembic import op from sqlalchemy import text from app.dao.date_util import get_current_calendar_year_start_year +from app.utils import utc_now revision = "0139_migrate_sms_allowance_data" down_revision = "0138_sms_sender_nullable" @@ -34,7 +35,7 @@ def upgrade(): input_params = { "current_year": current_year, "default_limit": default_limit, - "time_now": datetime.utcnow(), + "time_now": utc_now(), } insert_row_if_not_exist = """ INSERT INTO annual_billing diff --git a/migrations/versions/0171_add_org_invite_template.py b/migrations/versions/0171_add_org_invite_template.py index 5ec1925da..7c0b9df09 100644 --- a/migrations/versions/0171_add_org_invite_template.py +++ b/migrations/versions/0171_add_org_invite_template.py @@ -12,6 +12,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0171_add_org_invite_template" down_revision = "0170_hidden_non_nullable" @@ -53,7 +55,7 @@ def upgrade(): "template_id": template_id, "template_name": template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": template_subject, diff --git a/migrations/versions/0265_add_confirm_edit_templates.py b/migrations/versions/0265_add_confirm_edit_templates.py index 378891313..ad8d2470e 100644 --- a/migrations/versions/0265_add_confirm_edit_templates.py +++ b/migrations/versions/0265_add_confirm_edit_templates.py @@ -12,6 +12,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0265_add_confirm_edit_templates" down_revision = "0264_add_folder_permissions_perm" @@ -57,7 +59,7 @@ def upgrade(): "template_id": email_template_id, "template_name": email_template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": email_template_subject, @@ -78,7 +80,7 @@ def upgrade(): "template_id": mobile_template_id, "template_name": mobile_template_name, "template_type": "sms", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": mobile_template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": None, diff --git a/migrations/versions/0294_add_verify_reply_to_.py b/migrations/versions/0294_add_verify_reply_to_.py index 305c52997..d37f75ea0 100644 --- a/migrations/versions/0294_add_verify_reply_to_.py +++ b/migrations/versions/0294_add_verify_reply_to_.py @@ -12,6 +12,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0294_add_verify_reply_to" down_revision = "0293_drop_complaint_fk" @@ -58,7 +60,7 @@ def upgrade(): "template_id": email_template_id, "template_name": email_template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": email_template_subject, diff --git a/migrations/versions/0330_broadcast_invite_email.py b/migrations/versions/0330_broadcast_invite_email.py index 24dc60c68..bcd865d46 100644 --- a/migrations/versions/0330_broadcast_invite_email.py +++ b/migrations/versions/0330_broadcast_invite_email.py @@ -12,6 +12,8 @@ from datetime import datetime from alembic import op from sqlalchemy import text +from app.utils import utc_now + revision = "0330_broadcast_invite_email" down_revision = "0329_purge_broadcast_data" @@ -60,7 +62,7 @@ def upgrade(): input_params = { "template_id": template_id, "template_name": broadcast_invitation_template_name, - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": broadcast_invitation_content, "service_id": service_id, "subject": broadcast_invitation_subject, diff --git a/migrations/versions/0347_add_dvla_volumes_template.py b/migrations/versions/0347_add_dvla_volumes_template.py index 6b610c16c..f32821b79 100644 --- a/migrations/versions/0347_add_dvla_volumes_template.py +++ b/migrations/versions/0347_add_dvla_volumes_template.py @@ -13,6 +13,8 @@ from alembic import op from flask import current_app from sqlalchemy import text +from app.utils import utc_now + revision = "0347_add_dvla_volumes_template" down_revision = "0346_notify_number_sms_sender" @@ -57,7 +59,7 @@ def upgrade(): "template_id": email_template_id, "template_name": email_template_name, "template_type": "email", - "time_now": datetime.utcnow(), + "time_now": utc_now(), "content": email_template_content, "notify_service_id": current_app.config["NOTIFY_SERVICE_ID"], "subject": email_template_subject, diff --git a/migrations/versions/0401_add_e2e_test_user.py b/migrations/versions/0401_add_e2e_test_user.py index d3d83afad..e99a6af8a 100644 --- a/migrations/versions/0401_add_e2e_test_user.py +++ b/migrations/versions/0401_add_e2e_test_user.py @@ -16,6 +16,7 @@ from alembic import op from app import db from app.dao.users_dao import get_user_by_email from app.models import User +from app.utils import utc_now revision = "0401_add_e2e_test_user" down_revision = "0400_add_total_message_limit" @@ -32,11 +33,11 @@ def upgrade(): "password": password, "mobile_number": "+12025555555", "state": "active", - "created_at": datetime.datetime.utcnow(), - "password_changed_at": datetime.datetime.utcnow(), + "created_at": utc_now(), + "password_changed_at": utc_now(), "failed_login_count": 0, "platform_admin": "f", - "email_access_validated_at": datetime.datetime.utcnow(), + "email_access_validated_at": utc_now(), } conn = op.get_bind() insert_sql = """