Skip to content

Commit

Permalink
Merge pull request #140 from boxine/fix#135
Browse files Browse the repository at this point in the history
Fix `DisallowedModelAdminLookup` in `SignalInfoModelAdmin`, too.
  • Loading branch information
cnschn authored Jan 26, 2024
2 parents fa4c3f3 + 39d87b8 commit 2d37f25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,12 @@ You must change your Django settings and replace the app name:

## History

* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.9.0...main)
* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.9.1...main)
* _tbc_
* [v0.9.1 - 26.01.2024](https://github.com/boxine/django-huey-monitor/compare/v0.9.0...v0.9.1)
* Fix `DisallowedModelAdminLookup` in `SignalInfoModelAdmin`, too.
* [v0.9.0 - 22.12.2023](https://github.com/boxine/django-huey-monitor/compare/v0.8.1...v0.9.0)
* Fix #135 DisallowedModelAdminLookup
* Fix #135 `DisallowedModelAdminLookup` in `TaskModelAdmin`
* Add "thread" name as change list filter.
* Enhance test project setup
* Apply manageprojects updates
Expand Down
2 changes: 1 addition & 1 deletion huey_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
Django based tool for monitoring huey task queue: https://github.com/coleifer/huey
"""

__version__ = '0.9.0'
__version__ = '0.9.1'
__author__ = 'Jens Diemer <[email protected]>'
68 changes: 44 additions & 24 deletions huey_monitor/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,41 @@
from huey_monitor.models import SignalInfoModel, TaskModel


class FixLookupAllowedMixin:
"""
Work-a-round for: https://code.djangoproject.com/ticket/35020
# FIXME: Remove after release into all supported Django versions.
"""

def lookup_allowed(self, lookup, value):
list_filter = self._get_list_filter()
if lookup in list_filter:
return True
return super().lookup_allowed(lookup, value)

def _get_list_filter(self):
"""return list_filter without `request` object."""
raise NotImplementedError()

def get_list_filter(self, request):
return self._get_list_filter()


class TaskModelChangeList(ChangeList):
def get_queryset(self, request):
"""
List only the main-tasks (sub-tasks will be inlined)
"""
qs = super().get_queryset(request)
executing_dt = SignalInfoModel.objects.filter(
task_id=OuterRef("task_id"), signal_name=SIGNAL_EXECUTING
task_id=OuterRef('task_id'), signal_name=SIGNAL_EXECUTING
).values('create_dt')[:1]
qs = (
qs.filter(parent_task__isnull=True)
.prefetch_related(
Prefetch(
"sub_tasks",
queryset=TaskModel.objects.select_related("state")
'sub_tasks',
queryset=TaskModel.objects.select_related('state')
.annotate(executing_dt=executing_dt)
.order_by('-create_dt'),
)
Expand All @@ -39,7 +59,7 @@ def get_queryset(self, request):


@admin.register(TaskModel)
class TaskModelAdmin(admin.ModelAdmin):
class TaskModelAdmin(FixLookupAllowedMixin, admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return TaskModelChangeList

Expand All @@ -60,13 +80,13 @@ def task_hierarchy_info(self, obj):
if obj.parent_task_id is not None:
# This is a sub task
context = {
'main_task': TaskModel.objects.get(pk=obj.parent_task_id)
'main_task': TaskModel.objects.get(pk=obj.parent_task_id),
}
else:
# This is a main Task
qs = TaskModel.objects.filter(parent_task_id=obj.pk)
context = {
'sub_tasks': qs
'sub_tasks': qs,
}

return render_to_string(
Expand Down Expand Up @@ -130,10 +150,13 @@ def get_urls(self):
'human_percentage',
'human_progress',
'human_throughput',
'duration'
'duration',
)
readonly_fields = (
'task_id', 'signals', 'create_dt', 'update_dt',
'task_id',
'signals',
'create_dt',
'update_dt',
'human_percentage',
'human_progress',
'human_throughput',
Expand Down Expand Up @@ -162,33 +185,26 @@ def get_urls(self):
(_('Hierarchy'), {'fields': ('task_hierarchy_info',)}),
)

def lookup_allowed(self, lookup, value):
if lookup in (
'state__signal_name',
'state__thread',
'state__hostname',
):
# Work-a-round for: https://code.djangoproject.com/ticket/35020
# FIXME: Remove after release.
return True
return super().lookup_allowed(lookup, value)

def get_list_filter(self, request):
def _get_list_filter(self):
"""return list_filter without `request` object."""
return getattr(settings, 'HUEY_MONITOR_TASK_MODEL_LIST_FILTER', None) or (
'name',
'state__signal_name',
'state__thread',
'state__hostname',
)

def get_list_filter(self, request):
return self._get_list_filter()

class Media:
css = {
'all': ('huey_monitor.css',)
'all': ('huey_monitor.css',),
}


@admin.register(SignalInfoModel)
class SignalInfoModelAdmin(admin.ModelAdmin):
class SignalInfoModelAdmin(FixLookupAllowedMixin, admin.ModelAdmin):
def task_name(self, obj):
return obj.task.name

Expand All @@ -207,16 +223,20 @@ def task_name(self, obj):
date_hierarchy = 'create_dt'
search_fields = ('task__name', 'exception_line', 'exception')

def get_list_filter(self, request):
def _get_list_filter(self):
"""return list_filter without `request` object."""
return getattr(settings, 'HUEY_MONITOR_SIGNAL_INFO_MODEL_LIST_FILTER', None) or (
'task__name',
'signal_name',
'thread',
'hostname',
)

def get_list_filter(self, request):
return self._get_list_filter()

def has_change_permission(self, request, obj=None):
return False

def get_queryset(self, request):
return super().get_queryset(request).prefetch_related("task")
return super().get_queryset(request).prefetch_related('task')

0 comments on commit 2d37f25

Please sign in to comment.