From 8d5e701eacbb5bc4b2a0e91cbc17b6f348a209b2 Mon Sep 17 00:00:00 2001 From: arinak1017 Date: Mon, 4 Nov 2024 05:31:04 -0500 Subject: [PATCH] Added a settings variable to allow configurable time periods for notification deletion (#2547) Added NOTIFICATION_RETENTION_DAYS settings variable to allow configurable time period with default of 30 days --- care/facility/tasks/cleanup.py | 7 +++++-- .../facility/tests/test_delete_older_notifications_task.py | 7 +++++-- config/settings/base.py | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/care/facility/tasks/cleanup.py b/care/facility/tasks/cleanup.py index 3f913142cc..4fca52eddc 100644 --- a/care/facility/tasks/cleanup.py +++ b/care/facility/tasks/cleanup.py @@ -1,6 +1,7 @@ from datetime import timedelta from celery import shared_task +from django.conf import settings from django.utils import timezone from care.facility.models.notification import Notification @@ -8,5 +9,7 @@ @shared_task def delete_old_notifications(): - ninety_days_ago = timezone.now() - timedelta(days=90) - Notification.objects.filter(created_date__lte=ninety_days_ago).delete() + retention_days = settings.NOTIFICATION_RETENTION_DAYS + + threshold_date = timezone.now() - timedelta(days=retention_days) + Notification.objects.filter(created_date__lte=threshold_date).delete() diff --git a/care/facility/tests/test_delete_older_notifications_task.py b/care/facility/tests/test_delete_older_notifications_task.py index 9f860770b2..70ded42e71 100644 --- a/care/facility/tests/test_delete_older_notifications_task.py +++ b/care/facility/tests/test_delete_older_notifications_task.py @@ -1,5 +1,6 @@ from datetime import timedelta +from django.conf import settings from django.test import TestCase from django.utils import timezone from freezegun import freeze_time @@ -10,8 +11,10 @@ class DeleteOldNotificationsTest(TestCase): def test_delete_old_notifications(self): - # notifications created 90 days ago - with freeze_time(timezone.now() - timedelta(days=90)): + retention_days = settings.NOTIFICATION_RETENTION_DAYS + + # notifications created at the threshold of retention + with freeze_time(timezone.now() - timedelta(days=retention_days)): notification1 = Notification.objects.create() notification2 = Notification.objects.create() diff --git a/config/settings/base.py b/config/settings/base.py index 65e58d235d..6983433f86 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -530,6 +530,7 @@ "VAPID_PRIVATE_KEY", default="7mf3OFreFsgFF4jd8A71ZGdVaj8kpJdOto4cFbfAS-s" ) SEND_SMS_NOTIFICATION = False +NOTIFICATION_RETENTION_DAYS = env.int("NOTIFICATION_RETENTION_DAYS", default=30) # Cloud and Buckets # ------------------------------------------------------------------------------