diff --git a/CHANGES.rst b/CHANGES.rst index cf717331..3a0033dc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) diff --git a/docs/models.rst b/docs/models.rst index 8ecf6bce..2c5996f6 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -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 diff --git a/model_utils/models.py b/model_utils/models.py index 268db8c5..c816b464 100644 --- a/model_utils/models.py +++ b/model_utils/models.py @@ -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 ( @@ -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 = [] diff --git a/tests/models.py b/tests/models.py index 6d3f23fc..b5efe55e 100644 --- a/tests/models.py +++ b/tests/models.py @@ -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, @@ -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")), diff --git a/tests/test_models/test_savesignalhandling_model.py b/tests/test_models/test_savesignalhandling_model.py deleted file mode 100644 index 946da364..00000000 --- a/tests/test_models/test_savesignalhandling_model.py +++ /dev/null @@ -1,42 +0,0 @@ -from django.db.models.signals import post_save, pre_save -from django.test import TestCase - -from tests.models import SaveSignalHandlingTestModel -from tests.signals import post_save_test, pre_save_test - - -class SaveSignalHandlingModelTests(TestCase): - - def test_pre_save(self): - pre_save.connect(pre_save_test, sender=SaveSignalHandlingTestModel) - - obj = SaveSignalHandlingTestModel.objects.create(name='Test') - delattr(obj, 'pre_save_runned') - obj.name = 'Test A' - obj.save() - self.assertEqual(obj.name, 'Test A') - self.assertTrue(hasattr(obj, 'pre_save_runned')) - - obj = SaveSignalHandlingTestModel.objects.create(name='Test') - delattr(obj, 'pre_save_runned') - obj.name = 'Test B' - obj.save(signals_to_disable=['pre_save']) - self.assertEqual(obj.name, 'Test B') - self.assertFalse(hasattr(obj, 'pre_save_runned')) - - def test_post_save(self): - post_save.connect(post_save_test, sender=SaveSignalHandlingTestModel) - - obj = SaveSignalHandlingTestModel.objects.create(name='Test') - delattr(obj, 'post_save_runned') - obj.name = 'Test A' - obj.save() - self.assertEqual(obj.name, 'Test A') - self.assertTrue(hasattr(obj, 'post_save_runned')) - - obj = SaveSignalHandlingTestModel.objects.create(name='Test') - delattr(obj, 'post_save_runned') - obj.name = 'Test B' - obj.save(signals_to_disable=['post_save']) - self.assertEqual(obj.name, 'Test B') - self.assertFalse(hasattr(obj, 'post_save_runned'))