From 70154abd37831065b3e3a4f4f20289cab0d3738d Mon Sep 17 00:00:00 2001 From: moanos Date: Tue, 5 Nov 2024 07:47:31 +0100 Subject: [PATCH] feat: Add function to deactivate AN when link returns 404 --- src/fellchensammlung/models.py | 2 +- src/fellchensammlung/tools/admin.py | 21 ++++++++++++++++++++- src/notfellchen/__init__.py | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index e05511b..adf67c0 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -707,7 +707,7 @@ class Log(models.Model): """ Basic class that allows logging random entries for later inspection """ - user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("Nutzer*in")) + user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("Nutzer*in"), blank=True, null=True) action = models.CharField(max_length=255, verbose_name=_("Aktion")) text = models.CharField(max_length=1000, verbose_name=_("Log text")) created_at = models.DateTimeField(auto_now_add=True) diff --git a/src/fellchensammlung/tools/admin.py b/src/fellchensammlung/tools/admin.py index 349db8f..a820dd0 100644 --- a/src/fellchensammlung/tools/admin.py +++ b/src/fellchensammlung/tools/admin.py @@ -1,7 +1,10 @@ +import logging + from django.utils import timezone from datetime import timedelta -from fellchensammlung.models import AdoptionNotice, Location, RescueOrganization, AdoptionNoticeStatus +from fellchensammlung.models import AdoptionNotice, Location, RescueOrganization, AdoptionNoticeStatus, Log +from fellchensammlung.tools.misc import is_404 def clean_locations(quiet=True): @@ -60,6 +63,22 @@ def get_unchecked_adoption_notices(weeks=3): return active_unchecked_adoptions +def get_active_adoption_notices(): + ans = AdoptionNotice.objects.all() + active_adoptions = [adoption for adoption in ans if adoption.is_active] + return active_adoptions + + def deactivate_unchecked_adoption_notices(): for adoption_notice in get_unchecked_adoption_notices(weeks=3): AdoptionNoticeStatus.objects.get(adoption_notice=adoption_notice).set_unchecked() + + +def deactivate_404_adoption_notices(): + for adoption_notice in get_active_adoption_notices(): + if adoption_notice.link_to_more_information != "": + if is_404(adoption_notice.link_to_more_information): + adoption_notice.set_closed() + logging_msg = f"Automatically set Adoption Notice {adoption_notice.id} closed as link to more information returened 404" + logging.info(logging_msg) + Log.objects.create(action="automated", text=logging_msg) diff --git a/src/notfellchen/__init__.py b/src/notfellchen/__init__.py index 6658f36..14cb709 100644 --- a/src/notfellchen/__init__.py +++ b/src/notfellchen/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.3.0" +__version__ = "0.3.1" # This will make sure the app is always imported when # Django starts so that shared_task will use this app.