Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete jobs that are more than a year older than the active job #380

Merged
merged 10 commits into from
Nov 7, 2024
20 changes: 7 additions & 13 deletions data_registry/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.contrib import admin, messages
from django.db.models import Exists, OuterRef, Q
from django.db.models import Q
from django.urls import NoReverseMatch, reverse
from django.utils import timezone
from django.utils.html import escape
Expand Down Expand Up @@ -206,22 +206,16 @@ def get_form(self, request, obj=None, **kwargs):
return form


class FailedFilter(admin.SimpleListFilter):
title = _("failed")
parameter_name = "failed"
class UnsuccessfulFilter(admin.SimpleListFilter):
title = _("unsuccessful")
parameter_name = "unsuccessful"

def lookups(self, request, model_admin):
return (("1", _("Yes")),)

def queryset(self, request, queryset):
if self.value() == "1":
# https://docs.djangoproject.com/en/4.2/ref/models/expressions/#some-examples
failed_tasks = Task.objects.filter(
job=OuterRef("pk"),
status=Task.Status.COMPLETED,
result=Task.Result.FAILED,
)
return queryset.filter(Exists(failed_tasks))
return queryset.unsuccessful()
return None


Expand All @@ -248,7 +242,7 @@ class Media:
list_display = ["__str__", "country", "collection", "status", "last_task", "active", "archived", "keep_all_data"]
# "active" is read-only and uneditable, because at most one job must be set as active for a given collection.
list_editable = ["status", "keep_all_data"]
list_filter = ["status", ("active_collection", admin.EmptyFieldListFilter), "archived", FailedFilter]
list_filter = ["status", ("active_collection", admin.EmptyFieldListFilter), "archived", UnsuccessfulFilter]

fieldsets = (
(
Expand Down Expand Up @@ -339,7 +333,7 @@ def country(self, obj):

@admin.display(description="Active", boolean=True)
def active(self, obj):
return obj.id == obj.collection.active_job_id
return obj.pk == obj.collection.active_job_id

@admin.display(description="Last completed task")
def last_task(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion data_registry/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, *args, request=None, **kwargs):
# It's not obvious how to use limit_choices_to to filter jobs by collection.
# https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
self.fields["active_job"].queryset = (
models.Job.objects.filter(collection=self.instance).complete().order_by(F("id").desc())
models.Job.objects.filter(collection=self.instance).complete().order_by(F("pk").desc())
)

# Populate choices in the form, not the model, for easier migration between icon sets.
Expand Down
6 changes: 3 additions & 3 deletions data_registry/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ msgstr "Past year"
msgid "More than a year ago"
msgstr "More than a year ago"

#: data_registry/admin.py:134 data_registry/admin.py:254
#: data_registry/admin.py:134 data_registry/admin.py:248
msgid "Management"
msgstr "Management"

Expand All @@ -71,8 +71,8 @@ msgid "Details"
msgstr "Details"

#: data_registry/admin.py:209
msgid "failed"
msgstr "failed"
msgid "unsuccessful"
msgstr "unsuccessful"

#: data_registry/admin.py:281
msgid "Data availability"
Expand Down
Loading