Skip to content

Commit

Permalink
NEW: Unlock all tasks via change list object tool link
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Diemer committed Jun 1, 2022
1 parent bfb48df commit 34f78fe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
28 changes: 27 additions & 1 deletion huey_monitor/admin.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "admin/change_form_object_tools.html" %}
{% load i18n admin_urls %}

{% block object-tools-items %}
{% if has_add_permission %}
<li>
<a href="{% url 'admin:flush_locks' %}">
{% trans 'Flush all task locks' %}
</a>
</li>
{% endif %}
{{ block.super }}
{% endblock %}
15 changes: 15 additions & 0 deletions huey_monitor_tests/tests/test_huey_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.']
)

0 comments on commit 34f78fe

Please sign in to comment.