From 39d87b828c11b7f51f52711c0c3d3cfd1e2705b2 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Fri, 26 Jan 2024 08:46:49 +0100 Subject: [PATCH] Fix `DisallowedModelAdminLookup` in `SignalInfoModelAdmin`, too. Reported here: https://github.com/boxine/django-huey-monitor/issues/135#issuecomment-1902099062 --- README.md | 6 ++-- huey_monitor/__init__.py | 2 +- huey_monitor/admin.py | 68 ++++++++++++++++++++++++++-------------- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2cf7f28..a8e04bb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/huey_monitor/__init__.py b/huey_monitor/__init__.py index 479c5c4..df6344d 100644 --- a/huey_monitor/__init__.py +++ b/huey_monitor/__init__.py @@ -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 ' diff --git a/huey_monitor/admin.py b/huey_monitor/admin.py index d25cb39..7151fe3 100644 --- a/huey_monitor/admin.py +++ b/huey_monitor/admin.py @@ -14,6 +14,26 @@ 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): """ @@ -21,14 +41,14 @@ def get_queryset(self, request): """ 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'), ) @@ -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 @@ -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( @@ -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', @@ -162,18 +185,8 @@ 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', @@ -181,14 +194,17 @@ def get_list_filter(self, request): '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 @@ -207,7 +223,8 @@ 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', @@ -215,8 +232,11 @@ def get_list_filter(self, request): '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')