From ff4afd7288b186901b2bcd1361ec879a7678567b Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Thu, 17 Nov 2016 08:56:59 +0000 Subject: [PATCH 1/2] Add a purge_from_db to SoftDeletableModel --- model_utils/models.py | 12 +++++++++++- model_utils/tests/tests.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/model_utils/models.py b/model_utils/models.py index 22e9dc8..a7876e7 100644 --- a/model_utils/models.py +++ b/model_utils/models.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals import django +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.core.exceptions import ImproperlyConfigured if django.VERSION >= (1, 9, 0): from django.db.models.functions import Now now = Now() @@ -121,3 +121,13 @@ class SoftDeletableModel(models.Model): """ self.is_removed = True self.save(using=using) + + def purge_from_db(self, using=None, keep_parents=False): + """ + Actually purge the entry from the database + """ + del_kwargs = {'using': using} + # keep_parents option is new in Django 1.9 + if django.VERSION >= (1, 9, 0): + del_kwargs['keep_parents'] = keep_parents + super(SoftDeletableModel, self).delete(**del_kwargs) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 17fd67e..39637dd 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -2011,3 +2011,16 @@ class SoftDeletableModelTests(TestCase): obj = SoftDeletable.objects.create(name='a') self.assertRaises(ConnectionDoesNotExist, obj.delete, using='other') + + def test_instance_purge(self): + instance = SoftDeletable.objects.create(name='a') + + instance.purge_from_db() + + self.assertEqual(SoftDeletable.objects.count(), 0) + self.assertEqual(SoftDeletable.all_objects.count(), 0) + + def test_instance_purge_no_connection(self): + instance = SoftDeletable.objects.create(name='a') + + self.assertRaises(ConnectionDoesNotExist, instance.purge_from_db, using='other') From 4311e24e98fd1939d821cbe2ff375f41cb2b87cb Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 28 Nov 2016 23:45:48 +0000 Subject: [PATCH 2/2] Add soft parameter to delete method instead of new one --- model_utils/models.py | 22 +++++++------------ .../test_models/test_softdeletable_model.py | 5 +++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/model_utils/models.py b/model_utils/models.py index a7876e7..9c022cb 100644 --- a/model_utils/models.py +++ b/model_utils/models.py @@ -115,19 +115,13 @@ class SoftDeletableModel(models.Model): objects = SoftDeletableManager() - def delete(self, using=None, keep_parents=False): + def delete(self, using=None, soft=True, *args, **kwargs): """ - Soft delete object (set its ``is_removed`` field to True) + Soft delete object (set its ``is_removed`` field to True). + Actually delete object if setting ``soft`` to False. """ - self.is_removed = True - self.save(using=using) - - def purge_from_db(self, using=None, keep_parents=False): - """ - Actually purge the entry from the database - """ - del_kwargs = {'using': using} - # keep_parents option is new in Django 1.9 - if django.VERSION >= (1, 9, 0): - del_kwargs['keep_parents'] = keep_parents - super(SoftDeletableModel, self).delete(**del_kwargs) + if soft: + self.is_removed = True + self.save(using=using) + else: + return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs) diff --git a/model_utils/tests/test_models/test_softdeletable_model.py b/model_utils/tests/test_models/test_softdeletable_model.py index ee7f2f1..fb05e08 100644 --- a/model_utils/tests/test_models/test_softdeletable_model.py +++ b/model_utils/tests/test_models/test_softdeletable_model.py @@ -40,7 +40,7 @@ class SoftDeletableModelTests(TestCase): def test_instance_purge(self): instance = SoftDeletable.objects.create(name='a') - instance.purge_from_db() + instance.delete(soft=False) self.assertEqual(SoftDeletable.objects.count(), 0) self.assertEqual(SoftDeletable.all_objects.count(), 0) @@ -48,4 +48,5 @@ class SoftDeletableModelTests(TestCase): def test_instance_purge_no_connection(self): instance = SoftDeletable.objects.create(name='a') - self.assertRaises(ConnectionDoesNotExist, instance.purge_from_db, using='other') + self.assertRaises(ConnectionDoesNotExist, instance.delete, + using='other', soft=False)