Skip to content

Commit

Permalink
Delete takes care of unscheduling (islco#23)
Browse files Browse the repository at this point in the history
* MIDDLEWARE_CLASSES was renamed to MIDDLEWARE due to change after 1.10

And SessionAuthenticationMiddleware class is removed in 2.0 as it
provided no functionality since session authentication is unconditionally
enabled in Django 1.10.

* Tweaked tests as several failed in python3.x.
In python2.x d.keys() returned a list. With python3.x, d.keys() returns a dict_keys object which behaves a lot more like a set than a list. As such, it can't be indexed.

Changing d.keys() to list(d)) will return a list of keys on both python2.x and python3.x without making any copies.

reference: https://stackoverflow.com/questions/17322668/typeerror-dict-keys-object-does-not-support-indexing

* version bump
delete takes care of unscheduling
admin uses model.delete instead of model.all.delete
tests updated

* revert changes made to queue field in model (moved back queue choices to admin form)
  • Loading branch information
tom-price authored and bashhack committed Jul 10, 2018
1 parent c78f21d commit fa76c6a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions scheduler/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@


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")


@admin.register(ScheduledJob)
class ScheduledJobAdmin(QueueMixin, admin.ModelAdmin):
Expand Down
4 changes: 4 additions & 0 deletions scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def save(self, **kwargs):
self.schedule()
super(BaseJob, self).save(**kwargs)

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

def scheduler(self):
return django_rq.get_scheduler(self.queue)

Expand Down
21 changes: 21 additions & 0 deletions scheduler/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ def test_save_disabled(self):
job.save()
self.assertIsNone(job.job_id)

def test_save_and_schedule(self):
job = self.JobClassFactory()
job.id = 1
job.save()
is_scheduled = job.is_scheduled()
self.assertIsNotNone(job.job_id)
self.assertTrue(is_scheduled)

def test_delete_and_unschedule(self):
job_id = 1
job = self.JobClassFactory()
job.id = job_id
job.save()
is_scheduled = job.is_scheduled()
self.assertIsNotNone(job.job_id)
self.assertTrue(is_scheduled)
scheduler = job.scheduler()
job.delete()
is_scheduled = job_id in scheduler
self.assertFalse(is_scheduled)


class TestRepeatableJob(TestScheduledJob):

Expand Down

0 comments on commit fa76c6a

Please sign in to comment.