Skip to content

Commit

Permalink
Remove SaveSignalHandlingModel
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Oct 26, 2023
1 parent 0ec4ac5 commit 11f3a53
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 124 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
To be released
--------

- Remove ``SaveSignalHandlingModel``. This model used a modified copy of the internal Django method `Model.save_base()`
and had not been updated for upstream bug fixes changes since its addition. (GH-#582)
- Confirm support for `Django 4.2`
- Add support for `Python 3.11` (GH-#545)
- Add support for `Python 3.12` (GH-#545)
Expand Down
18 changes: 0 additions & 18 deletions docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,3 @@ Also you can override the default uuid version. Versions 1,3,4 and 5 are now sup
.. _`UUIDField`: https://github.com/jazzband/django-model-utils/blob/master/docs/fields.rst#uuidfield


SaveSignalHandlingModel
-----------------------

An abstract base class model to pass a parameter ``signals_to_disable``
to ``save`` method in order to disable signals

.. code-block:: python
from model_utils.models import SaveSignalHandlingModel
class SaveSignalTestModel(SaveSignalHandlingModel):
name = models.CharField(max_length=20)
obj = SaveSignalTestModel(name='Test')
# Note: If you use `Model.objects.create`, the signals can't be disabled
obj.save(signals_to_disable=['pre_save'] # disable `pre_save` signal
60 changes: 1 addition & 59 deletions model_utils/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.core.exceptions import ImproperlyConfigured
from django.db import models, router, transaction
from django.db import models
from django.db.models.functions import Now
from django.db.models.signals import post_save, pre_save
from django.utils.translation import gettext_lazy as _

from model_utils.fields import (
Expand Down Expand Up @@ -172,60 +171,3 @@ class UUIDModel(models.Model):

class Meta:
abstract = True


class SaveSignalHandlingModel(models.Model):
"""
An abstract base class model to pass a parameter ``signals_to_disable``
to ``save`` method in order to disable signals
"""
class Meta:
abstract = True

def save(self, signals_to_disable=None, *args, **kwargs):
"""
Add an extra parameters to hold which signals to disable
If empty, nothing will change
"""

self.signals_to_disable = signals_to_disable or []

super().save(*args, **kwargs)

def save_base(self, raw=False, force_insert=False,
force_update=False, using=None, update_fields=None):
"""
Copied from base class for a minor change.
This is an ugly overwriting but since Django's ``save_base`` method
does not differ between versions 1.8 and 1.10,
that way of implementing wouldn't harm the flow
"""
using = using or router.db_for_write(self.__class__, instance=self)
assert not (force_insert and (force_update or update_fields))
assert update_fields is None or len(update_fields) > 0
cls = origin = self.__class__

if cls._meta.proxy:
cls = cls._meta.concrete_model
meta = cls._meta
if not meta.auto_created and 'pre_save' not in self.signals_to_disable:
pre_save.send(
sender=origin, instance=self, raw=raw, using=using,
update_fields=update_fields,
)
with transaction.atomic(using=using, savepoint=False):
if not raw:
self._save_parents(cls, using, update_fields)
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

self._state.db = using
self._state.adding = False

if not meta.auto_created and 'post_save' not in self.signals_to_disable:
post_save.send(
sender=origin, instance=self, created=(not updated),
update_fields=update_fields, raw=raw, using=using,
)

# Empty the signals in case it might be used somewhere else in future
self.signals_to_disable = []
5 changes: 0 additions & 5 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
from model_utils.managers import InheritanceManager, JoinManagerMixin, QueryManager
from model_utils.models import (
SaveSignalHandlingModel,
SoftDeletableModel,
StatusModel,
TimeFramedModel,
Expand Down Expand Up @@ -446,10 +445,6 @@ class CustomNotPrimaryUUIDModel(models.Model):
uuid = UUIDField(primary_key=False)


class SaveSignalHandlingTestModel(SaveSignalHandlingModel):
name = models.CharField(max_length=20)


class TimeStampWithStatusModel(TimeStampedModel, StatusModel):
STATUS = Choices(
("active", _("active")),
Expand Down
42 changes: 0 additions & 42 deletions tests/test_models/test_savesignalhandling_model.py

This file was deleted.

0 comments on commit 11f3a53

Please sign in to comment.