From f92adfd703075b2487bf08d01cf5d88946b818a6 Mon Sep 17 00:00:00 2001 From: Kim Gustyr Date: Thu, 5 Dec 2024 13:12:44 +0000 Subject: [PATCH] feat: Admin improvements (#14) * feat: Admin improvements - Add `last_run_finished_at` admin field - Add `unlock` admin action --- task_processor/admin.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/task_processor/admin.py b/task_processor/admin.py index d62366c..1311bb4 100644 --- a/task_processor/admin.py +++ b/task_processor/admin.py @@ -1,4 +1,8 @@ +from datetime import datetime + from django.contrib import admin +from django.db.models import QuerySet +from django.http import HttpRequest from task_processor.models import RecurringTask @@ -10,6 +14,7 @@ class RecurringTaskAdmin(admin.ModelAdmin): "task_identifier", "run_every", "last_run_status", + "last_run_finished_at", "is_locked", ) readonly_fields = ("args", "kwargs") @@ -18,3 +23,16 @@ def last_run_status(self, instance: RecurringTask) -> str | None: if last_run := instance.task_runs.order_by("-started_at").first(): return last_run.result return None + + def last_run_finished_at(self, instance: RecurringTask) -> datetime | None: + if last_run := instance.task_runs.order_by("-started_at").first(): + return last_run.finished_at + return None + + @admin.action(description="Unlock selected tasks") + def unlock( + self, + request: HttpRequest, + queryset: QuerySet[RecurringTask], + ) -> None: + queryset.update(is_locked=False)