Skip to content

Commit

Permalink
Update to form admin de-dupe code and shifting enable toggling to action
Browse files Browse the repository at this point in the history
Several duplicated modelAdmin entries moved into mixin.
Added search of name and callable fields to admin.
Instead of enabled being a list_editable field there's now a form action to enable or disable all in a selection. This is useful when paired with the list_filter and search fields allowing for bulk enabling / disabling of jobs.
  • Loading branch information
tom-price committed Jul 30, 2018
1 parent c149c1d commit 08e7d33
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
84 changes: 43 additions & 41 deletions scheduler/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@
QUEUES = [(key, key) for key in settings.RQ_QUEUES.keys()]


class QueueMixin(object):
actions = ['delete_model']

def get_actions(self, request):
actions = super(QueueMixin, self).get_actions(request)
del actions['delete_selected']
return actions

def get_form(self, request, obj=None, **kwargs):
queue_field = self.model._meta.get_field('queue')
queue_field.choices = QUEUES
return super(QueueMixin, self).get_form(request, obj, **kwargs)

def delete_model(self, request, obj):
if hasattr(obj, 'all'):
for o in obj.all():
o.delete()
else:
obj.delete()
delete_model.short_description = _("Delete selected %(verbose_name_plural)s")


class JobArgInline(GenericStackedInline):
model = JobArg
extra = 0
Expand All @@ -59,17 +37,52 @@ class Media:
js = ('scheduler/js/base.js',)


class QueueMixin(object):
actions = ['delete_model', 'disable_selected', 'enable_selected', ]
inlines = [JobArgInline, JobKwargInline]
list_filter = ('enabled', 'callable')
readonly_fields = ('job_id', )
search_fields = ('name', 'callable', )

def get_actions(self, request):
actions = super(QueueMixin, self).get_actions(request)
del actions['delete_selected']
return actions

def get_form(self, request, obj=None, **kwargs):
queue_field = self.model._meta.get_field('queue')
queue_field.choices = QUEUES
return super(QueueMixin, self).get_form(request, obj, **kwargs)

def delete_model(self, request, obj):
if hasattr(obj, 'all'):
for o in obj.all():
o.delete()
else:
obj.delete()
delete_model.short_description = _("Delete selected %(verbose_name_plural)s")

def disable_selected(self, request, queryset):
for obj in queryset.all():
obj.enabled = False
obj.save()
disable_selected.short_description = _("Disable selected %(verbose_name_plural)s")

def enable_selected(self, request, queryset):
for obj in queryset.all():
obj.enabled = True
obj.save()
enable_selected.short_description = _("Enable selected %(verbose_name_plural)s")


@admin.register(ScheduledJob)
class ScheduledJobAdmin(QueueMixin, admin.ModelAdmin):
list_display = (
'name', 'job_id', ScheduledJob.function_string, 'is_scheduled', 'scheduled_time', 'enabled')
list_filter = ('enabled', )
list_editable = ('enabled', )
'enabled', 'name', 'job_id', ScheduledJob.function_string, 'is_scheduled', 'scheduled_time', )

readonly_fields = ('job_id', )
fieldsets = (
(None, {
'fields': ('name', 'callable', 'enabled', ),
'fields': ('name', 'callable', 'enabled'),
}),
(_('RQ Settings'), {
'fields': ('queue', 'job_id', ),
Expand All @@ -82,18 +95,14 @@ class ScheduledJobAdmin(QueueMixin, admin.ModelAdmin):
),
}),
)
inlines = [JobArgInline, JobKwargInline]


@admin.register(RepeatableJob)
class RepeatableJobAdmin(QueueMixin, admin.ModelAdmin):
list_display = (
'name', 'job_id', RepeatableJob.function_string, 'is_scheduled', 'scheduled_time', 'interval_display',
'enabled')
list_filter = ('enabled', )
list_editable = ('enabled', )
'enabled', 'name', 'job_id', RepeatableJob.function_string, 'is_scheduled', 'scheduled_time',
'interval_display')

readonly_fields = ('job_id', )
fieldsets = (
(None, {
'fields': ('name', 'callable', 'enabled', ),
Expand All @@ -111,17 +120,12 @@ class RepeatableJobAdmin(QueueMixin, admin.ModelAdmin):
),
}),
)
inlines = [JobArgInline, JobKwargInline]


@admin.register(CronJob)
class CronJobAdmin(QueueMixin, admin.ModelAdmin):
list_display = (
'name', 'job_id', CronJob.function_string, 'is_scheduled', 'cron_string', 'enabled')
list_filter = ('enabled', )
list_editable = ('enabled', )
'enabled', 'name', 'job_id', CronJob.function_string, 'is_scheduled', 'cron_string')

readonly_fields = ('job_id', )
fieldsets = (
(None, {
'fields': ('name', 'callable', 'enabled', ),
Expand All @@ -137,5 +141,3 @@ class CronJobAdmin(QueueMixin, admin.ModelAdmin):
),
}),
)
inlines = [JobArgInline, JobKwargInline]

2 changes: 0 additions & 2 deletions scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,12 @@ def is_scheduled(self):
def save(self, **kwargs):
self.schedule()
super(BaseJob, self).save(**kwargs)
# print('save', self.job_id, self.callable_args.all().count(), self.callable_kwargs.all().count())

def delete(self, **kwargs):
self.unschedule()
super(BaseJob, self).delete(**kwargs)

def schedule(self):
# print('schedule', self.job_id, self.callable_args.all().count(), self.callable_kwargs.all().count())
self.unschedule()
if self.is_schedulable() is False:
return False
Expand Down

0 comments on commit 08e7d33

Please sign in to comment.