Merge pull request #240 from browniebroke/feature/soft-delete-purge

Ability to purge from db a SoftDeletableModel
This commit is contained in:
Carl Meyer 2016-11-28 16:02:12 -08:00 committed by GitHub
commit 0febeae9ee
2 changed files with 23 additions and 5 deletions

View file

@ -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()
@ -115,9 +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)
if soft:
self.is_removed = True
self.save(using=using)
else:
return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs)

View file

@ -36,3 +36,17 @@ 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.delete(soft=False)
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.delete,
using='other', soft=False)