feat: Add unchecked AN cleanup to health check

This commit is contained in:
moanos [he/him] 2024-10-10 17:06:50 +02:00
parent f404cfa0a3
commit df41028e99
3 changed files with 66 additions and 14 deletions

View File

@ -1,24 +1,24 @@
{% extends "fellchensammlung/base_generic.html" %}
{% load i18n %}
{% block title %}<title>{% translate "Instanz-Check" %}</title> %}
{% block title %}<title>{% translate "Instanz-Check" %}</title> {% endblock %}
{% block content %}
<div class="card">
<h1>{% translate "Instanz-Check" %}</h1>
{% if missing_texts|length > 0 %}
<h2>{% trans "Fehlende Texte" %}</h2>
<p>
<table>
<table>
<tr>
<th>{% translate "Text Code" %}</th>
<th>{% translate "Sprache" %}</th>
</tr>
{% for missing_text in missing_texts %}
<tr>
<th>{% translate "Text Code" %}</th>
<th>{% translate "Sprache" %}</th>
<td>{{ missing_text.0 }}</td>
<td>{{ missing_text.1 }}</td>
</tr>
{% for missing_text in missing_texts %}
<tr>
<td>{{ missing_text.0 }}</td>
<td>{{ missing_text.1 }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
</table>
</p>
{% else %}
<p>{% translate "Texte scheinen vollständig" %}</p>
@ -56,6 +56,22 @@
<p>{{ number_not_geocoded_rescue_orgs }}/{{ number_of_rescue_orgs }}</p>
{% endif %}
<h2>{% translate "Nicht-geprüfte Vermittlungen" %}</h2>
{% if number_unchecked_ans > 0 %}
<details>
<summary>{{ number_unchecked_ans }}</summary>
<ul>
{% for unchecked_an in unchecked_ans %}
<li>
<a href="{{ unchecked_an.get_absolute_url }}">{{ unchecked_an.name }}</a>
</li>
{% endfor %}
</ul>
</details>
{% else %}
<p>{{ number_unchecked_ans }}</p>
{% endif %}
<form class="notification-card-mark-read" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="clean_locations">
@ -63,5 +79,13 @@
<i class="fa-solid fa-broom"></i> {% translate "Erneut lokalisieren" %}
</button>
</form>
<form class="notification-card-mark-read" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="deactivate_unchecked_adoption_notices">
<button class="btn" type="submit" id="submit">
<i class="fa-solid fa-broom"></i> {% translate "Deaktivire ungeprüfte Vermittlungen" %}
</button>
</form>
</div>
{% endblock content %}

View File

@ -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()

View File

@ -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)