Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DisallowedModelAdminLookup in SignalInfoModelAdmin, too. #140

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <django-huey-monitor@jensdiemer.de>'
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')
Loading