From df41028e999029876f49cb407dc819ff511ad82d Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 10 Oct 2024 17:06:50 +0200 Subject: [PATCH] feat: Add unchecked AN cleanup to health check --- .../instance-health-check.html | 46 ++++++++++++++----- src/fellchensammlung/tools/admin.py | 23 +++++++++- src/fellchensammlung/views.py | 11 ++++- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/fellchensammlung/templates/fellchensammlung/instance-health-check.html b/src/fellchensammlung/templates/fellchensammlung/instance-health-check.html index 84d66ad..6d64403 100644 --- a/src/fellchensammlung/templates/fellchensammlung/instance-health-check.html +++ b/src/fellchensammlung/templates/fellchensammlung/instance-health-check.html @@ -1,24 +1,24 @@ {% extends "fellchensammlung/base_generic.html" %} {% load i18n %} -{% block title %}{% translate "Instanz-Check" %} %} +{% block title %}{% translate "Instanz-Check" %} {% endblock %} {% block content %}

{% translate "Instanz-Check" %}

{% if missing_texts|length > 0 %}

{% trans "Fehlende Texte" %}

- +
+ + + + + {% for missing_text in missing_texts %} - - + + - {% for missing_text in missing_texts %} - - - - - {% endfor %} -
{% translate "Text Code" %}{% translate "Sprache" %}
{% translate "Text Code" %}{% translate "Sprache" %}{{ missing_text.0 }}{{ missing_text.1 }}
{{ missing_text.0 }}{{ missing_text.1 }}
+ {% endfor %} +

{% else %}

{% translate "Texte scheinen vollständig" %}

@@ -56,6 +56,22 @@

{{ number_not_geocoded_rescue_orgs }}/{{ number_of_rescue_orgs }}

{% endif %} +

{% translate "Nicht-geprüfte Vermittlungen" %}

+ {% if number_unchecked_ans > 0 %} +
+ {{ number_unchecked_ans }} + +
+ {% else %} +

{{ number_unchecked_ans }}

+ {% endif %} +
{% csrf_token %} @@ -63,5 +79,13 @@ {% translate "Erneut lokalisieren" %}
+ +
+ {% csrf_token %} + + +
{% endblock content %} diff --git a/src/fellchensammlung/tools/admin.py b/src/fellchensammlung/tools/admin.py index 56188ad..eaa64a8 100644 --- a/src/fellchensammlung/tools/admin.py +++ b/src/fellchensammlung/tools/admin.py @@ -1,4 +1,8 @@ -from fellchensammlung.models import AdoptionNotice, Location, RescueOrganization +from django.utils import timezone +from datetime import timedelta + +from fellchensammlung.models import AdoptionNotice, Location, RescueOrganization, AdoptionNoticeStatus + def clean_locations(quiet=True): # ADOPTION NOTICES @@ -42,3 +46,20 @@ def clean_locations(quiet=True): num_new = num_without_location - num_without_location_new if not quiet: print(f"Added {num_new} new locations") + + +def get_unchecked_adoption_notices(weeks=3): + now = timezone.now() + three_weeks_ago = now - timedelta(weeks=weeks) + + # Query for active adoption notices that were checked in the last three weeks + unchecked_adoptions = AdoptionNotice.objects.filter( + last_checked__gte=three_weeks_ago + ) + active_unchecked_adoptions = [adoption for adoption in unchecked_adoptions if adoption.is_active] + return active_unchecked_adoptions + + +def deactivate_unchecked_adoption_notices(): + for adoption_notice in get_unchecked_adoption_notices(weeks=3): + AdoptionNoticeStatus.objects.get(adoption_notice=adoption_notice).deactivate_unchecked() diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 7c9fe6e..87a3b68 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -21,7 +21,7 @@ from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageFo from .models import Language, Announcement from .tools.geo import GeoAPI from .tools.metrics import gather_metrics_data -from .tools.admin import clean_locations +from .tools.admin import clean_locations, get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices from .tasks import add_adoption_notice_location @@ -470,6 +470,8 @@ def instance_health_check(request): action = request.POST.get("action") if action == "clean_locations": clean_locations(quiet=False) + elif action == "deactivate_unchecked_adoption_notices": + deactivate_unchecked_adoption_notices() number_of_adoption_notices = AdoptionNotice.objects.all().count() none_geocoded_adoption_notices = AdoptionNotice.objects.filter(location__isnull=True) @@ -479,6 +481,9 @@ def instance_health_check(request): none_geocoded_rescue_orgs = RescueOrganization.objects.filter(location__isnull=True) number_not_geocoded_rescue_orgs = len(none_geocoded_rescue_orgs) + unchecked_ans = get_unchecked_adoption_notices() + number_unchecked_ans = len(unchecked_ans) + # CHECK FOR MISSING TEXTS languages = Language.objects.all() texts = Text.objects.all() @@ -498,7 +503,9 @@ def instance_health_check(request): "number_of_rescue_orgs": number_of_rescue_orgs, "number_not_geocoded_rescue_orgs": number_not_geocoded_rescue_orgs, "none_geocoded_rescue_orgs": none_geocoded_rescue_orgs, - "missing_texts": missing_texts + "missing_texts": missing_texts, + "number_unchecked_ans": number_unchecked_ans, + "unchecked_ans": unchecked_ans } return render(request, 'fellchensammlung/instance-health-check.html', context=context)