diff --git a/django_celery_beat/managers.py b/django_celery_beat/managers.py deleted file mode 100644 index fe3b9176..00000000 --- a/django_celery_beat/managers.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Model managers.""" -from django.db import models -from django.db.models.query import QuerySet - - -class ExtendedQuerySet(QuerySet): - """Base class for query sets.""" - - def update_or_create(self, defaults=None, **kwargs): - obj, created = self.get_or_create(defaults=defaults, **kwargs) - if not created: - self._update_model_with_dict(obj, dict(defaults or {}, **kwargs)) - return obj - - def _update_model_with_dict(self, obj, fields): - [setattr(obj, attr_name, attr_value) - for attr_name, attr_value in fields.items()] - obj.save() - return obj - - -class ExtendedManager(models.Manager.from_queryset(ExtendedQuerySet)): - """Manager with common utilities.""" - - -class PeriodicTaskManager(ExtendedManager): - """Manager for PeriodicTask model.""" - - def enabled(self): - return self.filter(enabled=True) diff --git a/django_celery_beat/models.py b/django_celery_beat/models.py index 7cb8d0b4..f014f6d6 100644 --- a/django_celery_beat/models.py +++ b/django_celery_beat/models.py @@ -14,7 +14,7 @@ from django.db.models import signals from django.utils.translation import gettext_lazy as _ -from . import managers, validators +from . import querysets, validators from .tzcrontab import TzAwareCrontab from .utils import make_aware, now from .clockedschedule import clocked @@ -370,8 +370,6 @@ class PeriodicTasks(models.Model): ident = models.SmallIntegerField(default=1, primary_key=True, unique=True) last_update = models.DateTimeField(null=False) - objects = managers.ExtendedManager() - @classmethod def changed(cls, instance, **kwargs): if not instance.no_changes: @@ -541,7 +539,7 @@ class PeriodicTask(models.Model): 'Detailed description about the details of this Periodic Task'), ) - objects = managers.PeriodicTaskManager() + objects = querysets.PeriodicTaskQuerySet.as_manager() no_changes = False class Meta: diff --git a/django_celery_beat/querysets.py b/django_celery_beat/querysets.py new file mode 100644 index 00000000..622c4c2b --- /dev/null +++ b/django_celery_beat/querysets.py @@ -0,0 +1,9 @@ +"""Model querysets.""" +from django.db import models + + +class PeriodicTaskQuerySet(models.QuerySet): + """QuerySet for PeriodicTask.""" + + def enabled(self): + return self.filter(enabled=True) diff --git a/django_celery_beat/schedulers.py b/django_celery_beat/schedulers.py index 176b95ec..dcdf8761 100644 --- a/django_celery_beat/schedulers.py +++ b/django_celery_beat/schedulers.py @@ -173,9 +173,10 @@ def to_model_schedule(cls, schedule): @classmethod def from_entry(cls, name, app=None, **entry): - return cls(PeriodicTask._default_manager.update_or_create( + obj, created = PeriodicTask._default_manager.update_or_create( name=name, defaults=cls._unpack_fields(**entry), - ), app=app) + ) + return cls(obj, app=app) @classmethod def _unpack_fields(cls, schedule, diff --git a/docs/reference/django-celery-beat.managers.rst b/docs/reference/django-celery-beat.querysets.rst similarity index 57% rename from docs/reference/django-celery-beat.managers.rst rename to docs/reference/django-celery-beat.querysets.rst index f0b4eaaf..f7464569 100644 --- a/docs/reference/django-celery-beat.managers.rst +++ b/docs/reference/django-celery-beat.querysets.rst @@ -1,11 +1,11 @@ ===================================================== - ``django_celery_beat.managers`` + ``django_celery_beat.querysets`` ===================================================== .. contents:: :local: -.. currentmodule:: django_celery_beat.managers +.. currentmodule:: django_celery_beat.querysets -.. automodule:: django_celery_beat.managers +.. automodule:: django_celery_beat.querysets :members: :undoc-members: diff --git a/docs/reference/index.rst b/docs/reference/index.rst index d667d378..83d384db 100644 --- a/docs/reference/index.rst +++ b/docs/reference/index.rst @@ -13,7 +13,7 @@ django-celery-beat django-celery-beat.models django-celery-beat.tzcrontab - django-celery-beat.managers + django-celery-beat.querysets django-celery-beat.schedulers django-celery-beat.admin django-celery-beat.utils