From 3a4aa971bf9dec802ed642f6831c3b029ea3e6b9 Mon Sep 17 00:00:00 2001 From: Andy Shapiro Date: Tue, 23 Jul 2024 11:48:36 -0400 Subject: [PATCH] analysis deletion dates improvements (#50) * fix bug in deletion date deletion * remove unused command * smart display of days to keep --- bmds_ui/analysis/fixtures/initial_data.json | 2 +- .../analysis/management/commands/__init__.py | 0 .../management/commands/delete_old_jobs.py | 18 ------------------ bmds_ui/analysis/models.py | 2 +- bmds_ui/analysis/views.py | 9 ++++++++- bmds_ui/main/settings/base.py | 2 +- tests/analysis/test_views.py | 19 ++++++++++++++++++- tests/data/db.yaml | 2 +- 8 files changed, 30 insertions(+), 24 deletions(-) delete mode 100644 bmds_ui/analysis/management/commands/__init__.py delete mode 100644 bmds_ui/analysis/management/commands/delete_old_jobs.py diff --git a/bmds_ui/analysis/fixtures/initial_data.json b/bmds_ui/analysis/fixtures/initial_data.json index 981c777c..887fbbbb 100644 --- a/bmds_ui/analysis/fixtures/initial_data.json +++ b/bmds_ui/analysis/fixtures/initial_data.json @@ -6,7 +6,7 @@ "content_type": 1, "subject": "Homepage", "content": { - "template": "
\n
\n

Welcome to BMDS online.

\n
\n
\n
\n
\n
\n

\n This web-application executes the US EPA's benchmark dose modeling software (BMDS).\n

\n
    \n
  • Load example datasets below to view example input files; for more information, see the documentation\n
  • \n
  • Software is designed for automated BMDS execution using a\n programming language such as Python, R, or Java. To take\n advantage of automation, use the API,\n and see the quickstart section.\n
  • \n
\n
\n
\n
\n
\n {% csrf_token %}\n \n \n

Analyses are deleted after {{days_to_keep_analyses}} days.

\n
\n
\n
\n" + "template": "
\n
\n

Welcome to BMDS online.

\n
\n
\n
\n
\n
\n

\n This web-application executes the US EPA's benchmark dose modeling software (BMDS).\n

\n
    \n
  • Load example datasets below to view example input files; for more information, see the documentation\n
  • \n
  • Software is designed for automated BMDS execution using a\n programming language such as Python, R, or Java. To take\n advantage of automation, use the API,\n and see the quickstart section.\n
  • \n
\n
\n
\n
\n
\n {% csrf_token %}\n \n \n

Analyses are deleted after {{days_to_keep_analyses}}.

\n
\n
\n
\n" }, "created": "2021-01-31 08:00:00.123456+00:00", "last_updated": "2021-01-31 08:00:00.123456+00:00" diff --git a/bmds_ui/analysis/management/commands/__init__.py b/bmds_ui/analysis/management/commands/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/bmds_ui/analysis/management/commands/delete_old_jobs.py b/bmds_ui/analysis/management/commands/delete_old_jobs.py deleted file mode 100644 index 5971eb54..00000000 --- a/bmds_ui/analysis/management/commands/delete_old_jobs.py +++ /dev/null @@ -1,18 +0,0 @@ -from datetime import timedelta - -from django.apps import apps -from django.conf import settings -from django.core.management.base import BaseCommand -from django.utils.timezone import now - - -class Command(BaseCommand): - help = "Delete old results which are older than N days." - - def handle(self, *args, **options): - oldest_to_keep = now() - timedelta(days=settings.DAYS_TO_KEEP_ANALYSES) - Analysis = apps.get_model("analysis", "Analysis") - qs = Analysis.objects.filter(created__lt=oldest_to_keep) - keep_time = oldest_to_keep.strftime("%c") - self.stdout.write(f"Deleting {qs.count()} analyses created before {keep_time}\n") - qs.delete() diff --git a/bmds_ui/analysis/models.py b/bmds_ui/analysis/models.py index 3c6017bc..fbf4b680 100644 --- a/bmds_ui/analysis/models.py +++ b/bmds_ui/analysis/models.py @@ -142,7 +142,7 @@ def has_errors(self): @classmethod def delete_old_analyses(cls): - qs = cls.objects.filter(deletion_date=now()) + qs = cls.objects.filter(deletion_date__lt=now()) logger.info(f"Removing {qs.count()} old BMDS analysis") qs.delete() diff --git a/bmds_ui/analysis/views.py b/bmds_ui/analysis/views.py index 8b0ae424..87914391 100644 --- a/bmds_ui/analysis/views.py +++ b/bmds_ui/analysis/views.py @@ -34,10 +34,17 @@ def _render_template(self, extra): content = models.Content.get_cached_content(models.ContentType.HOMEPAGE) return Template(content["template"]).render(context) + def days_to_keep(self, days: int) -> str: + if days == 365: + return "1 year" + elif days < 365 or days % 365 > 0: + return f"{days} days" + return f"{days // 365} years" + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update( - days_to_keep_analyses=settings.DAYS_TO_KEEP_ANALYSES, + days_to_keep_analyses=self.days_to_keep(settings.DAYS_TO_KEEP_ANALYSES), citation=get_citation(), ) context["page"] = self._render_template(context) diff --git a/bmds_ui/main/settings/base.py b/bmds_ui/main/settings/base.py index 224d56fe..1ed09879 100644 --- a/bmds_ui/main/settings/base.py +++ b/bmds_ui/main/settings/base.py @@ -209,7 +209,7 @@ } } -DAYS_TO_KEEP_ANALYSES = 180 +DAYS_TO_KEEP_ANALYSES = int(os.environ.get("ANALYSIS_RETENTION_DAYS", "365")) # commit information diff --git a/tests/analysis/test_views.py b/tests/analysis/test_views.py index 0a9a6c70..9b6ee124 100644 --- a/tests/analysis/test_views.py +++ b/tests/analysis/test_views.py @@ -7,7 +7,7 @@ from bmds_ui.analysis.models import Analysis, Collection from bmds_ui.analysis.validators.session import BmdsVersion -from bmds_ui.analysis.views import Analytics, DesktopHome, get_analysis_or_404 +from bmds_ui.analysis.views import Analytics, DesktopHome, Home, get_analysis_or_404 @pytest.mark.django_db @@ -132,6 +132,23 @@ def test_view(self): assertTemplateUsed(resp, template) +@pytest.mark.django_db +class TestHome: + def test_views(self): + request = RequestFactory().get(reverse("home")) + response = Home.as_view()(request) + assert response.status_code == 200 + + def test_get_days_to_keep(self): + view = Home() + assert view.days_to_keep(180) == "180 days" + assert view.days_to_keep(365) == "1 year" + assert view.days_to_keep(366) == "366 days" + assert view.days_to_keep(730) == "2 years" + assert view.days_to_keep(1095) == "3 years" + assert view.days_to_keep(1096) == "1096 days" + + @pytest.mark.django_db class TestDesktopHome: def test_views(self): diff --git a/tests/data/db.yaml b/tests/data/db.yaml index 801e0ce7..e05e1c6c 100644 --- a/tests/data/db.yaml +++ b/tests/data/db.yaml @@ -5171,7 +5171,7 @@ csrf_token %}\n \n \ \n

Analyses - are deleted after {{days_to_keep_analyses}} days.

\n \n \n\n" + are deleted after {{days_to_keep_analyses}}.

\n \n \n\n" created: 2021-03-01 22:39:06.328990+00:00 last_updated: 2021-03-01 22:39:06.329015+00:00 - model: reversion.revision