From 34f78fe7188857c9c1d506ea8601cdef74eb3d27 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 1 Jun 2022 17:27:49 +0200 Subject: [PATCH] NEW: Unlock all tasks via change list object tool link for https://github.com/boxine/django-huey-monitor/issues/102 Looks like: ![image](https://user-images.githubusercontent.com/71315/171357776-9fc55010-e86f-406a-85bf-eff055302ba5.png) --- huey_monitor/admin.py | 28 ++++++++++++++++++- .../taskmodel/change_list_object_tools.html | 13 +++++++++ huey_monitor_tests/tests/test_huey_monitor.py | 15 ++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 huey_monitor/templates/admin/huey_monitor/taskmodel/change_list_object_tools.html diff --git a/huey_monitor/admin.py b/huey_monitor/admin.py index aa909dc..ab29f02 100644 --- a/huey_monitor/admin.py +++ b/huey_monitor/admin.py @@ -1,9 +1,12 @@ from bx_django_utils.templatetags.humanize_time import human_duration -from django.contrib import admin +from django.contrib import admin, messages from django.contrib.admin.views.main import ChangeList +from django.shortcuts import redirect from django.template.loader import render_to_string +from django.urls import path, reverse from django.utils import timezone from django.utils.translation import gettext_lazy as _ +from huey.contrib.djhuey import HUEY from huey_monitor.models import SignalInfoModel, TaskModel @@ -78,6 +81,29 @@ def duration(self, obj): return human_duration(obj.create_dt, end_dt) + def changelist_url(self): + info = (self.admin_site.name, self.model._meta.app_label, self.model._meta.model_name) + url_name = '%s:%s_%s_changelist' % info + return reverse(url_name, current_app=self.admin_site.name) + + def flush_locks_view(self, request): + flushed = HUEY.flush_locks() + if not flushed: + messages.info(request, 'No tasks locks exists, nothing to flush, ok.') + else: + messages.success(request, f'Flush task locks: {", ".join(sorted(flushed))}') + return redirect(self.changelist_url()) + + def get_urls(self): + urls = [ + path( + 'flush_locks/', + self.admin_site.admin_view(self.flush_locks_view), + name='flush_locks', + ), + ] + super().get_urls() + return urls + list_display = ( 'human_update_dt', 'column_name', diff --git a/huey_monitor/templates/admin/huey_monitor/taskmodel/change_list_object_tools.html b/huey_monitor/templates/admin/huey_monitor/taskmodel/change_list_object_tools.html new file mode 100644 index 0000000..a884472 --- /dev/null +++ b/huey_monitor/templates/admin/huey_monitor/taskmodel/change_list_object_tools.html @@ -0,0 +1,13 @@ +{% extends "admin/change_form_object_tools.html" %} +{% load i18n admin_urls %} + +{% block object-tools-items %} + {% if has_add_permission %} +
  • + + {% trans 'Flush all task locks' %} + +
  • + {% endif %} + {{ block.super }} +{% endblock %} \ No newline at end of file diff --git a/huey_monitor_tests/tests/test_huey_monitor.py b/huey_monitor_tests/tests/test_huey_monitor.py index e1b2cdf..94dad0a 100644 --- a/huey_monitor_tests/tests/test_huey_monitor.py +++ b/huey_monitor_tests/tests/test_huey_monitor.py @@ -98,3 +98,18 @@ def test_raise_error_task(self): 'Traceback (most recent call last):', 'AssertionError: This is a test exception', )) + + def test_admin_flush_locks_view(self): + flush_locks_url = '/admin/huey_monitor/taskmodel/flush_locks/' + response = self.client.get(flush_locks_url) + self.assertRedirects( + response, expected_url='/admin/login/?next=/admin/huey_monitor/taskmodel/flush_locks/' + ) + self.assert_messages(response, expected_messages=[]) + + self.client.force_login(self.superuser) + response = self.client.get(flush_locks_url) + self.assertRedirects(response, expected_url='/admin/huey_monitor/taskmodel/') + self.assert_messages( + response, expected_messages=['No tasks locks exists, nothing to flush, ok.'] + )