Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to solve #118 #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion safedelete/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ def undelete(self, force_policy=None, **kwargs):
current_policy = force_policy or self._safedelete_policy

assert self.deleted
deleted_date = self.deleted
self.save(keep_deleted=False, **kwargs)

if current_policy == SOFT_DELETE_CASCADE:
for related in related_objects(self):
if is_safedelete_cls(related.__class__) and related.deleted:
if is_safedelete_cls(related.__class__) and related.deleted and abs((deleted_date - related.deleted).total_seconds()) < 60:
related.undelete()

def delete(self, force_policy=None, **kwargs):
Expand Down
13 changes: 10 additions & 3 deletions safedelete/tests/test_soft_delete_cascade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime

from django.db import models
from django.test import TestCase
from django.utils import timezone
from safedelete import SOFT_DELETE_CASCADE, SOFT_DELETE
from safedelete.models import SafeDeleteModel
from safedelete.tests.models import Article, Author, Category
Expand Down Expand Up @@ -53,8 +56,12 @@ def setUp(self):
Article.objects.create(author=self.authors[1]),
Article.objects.create(author=self.authors[1], category=self.categories[1]),
Article.objects.create(author=self.authors[2], category=self.categories[2]),
Article.objects.create(author=self.authors[2], category=self.categories[2])
)

with patch.object(timezone, 'now', return_value=datetime.datetime(2012, 12, 21)) as mock_now:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the mock_now as it's unused. See the travis build log, which tests for pyflakes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also separate this patch into a test to test for the behaviour you changed. Reason being is that you'd have to keep this in mind for all of the tests. Best to test for this separately each time imo.

self.articles[3].delete(force_policy=SOFT_DELETE_CASCADE)

self.press = (
Press.objects.create(name='press 0', article=self.articles[2])
)
Expand All @@ -70,7 +77,7 @@ def test_soft_delete_cascade(self):
self.assertEqual(Author.objects.count(), 2)
self.assertEqual(Author.all_objects.count(), 3)
self.assertEqual(Article.objects.count(), 2)
self.assertEqual(Article.all_objects.count(), 3)
self.assertEqual(Article.all_objects.count(), 4)
self.assertEqual(Press.objects.count(), 0)
self.assertEqual(Press.all_objects.count(), 1)

Expand All @@ -81,7 +88,7 @@ def test_soft_delete_cascade_with_normal_model(self):
self.assertEqual(Author.objects.count(), 2)
self.assertEqual(Author.all_objects.count(), 3)
self.assertEqual(Article.objects.count(), 2)
self.assertEqual(Article.all_objects.count(), 3)
self.assertEqual(Article.all_objects.count(), 4)
self.assertEqual(Press.objects.count(), 0)
self.assertEqual(Press.all_objects.count(), 1)

Expand All @@ -91,7 +98,7 @@ def test_soft_delete_cascade_with_abstract_model(self):
self.articles[2].delete(force_policy=SOFT_DELETE_CASCADE)

self.assertEqual(Article.objects.count(), 2)
self.assertEqual(Article.all_objects.count(), 3)
self.assertEqual(Article.all_objects.count(), 4)

self.assertEqual(ArticleView.objects.count(), 0)
self.assertEqual(ArticleView.all_objects.count(), 1)
Expand Down