-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre and post clone save signals (#680)
* Add pre and post clone save signals * Fix auto commit * Fix assert annotations * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tonye Jack <[email protected]>
- Loading branch information
1 parent
8d14b87
commit 56342be
Showing
6 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django.db.models.signals import ModelSignal | ||
|
||
pre_clone_save = ModelSignal(use_caching=True) | ||
post_clone_save = ModelSignal(use_caching=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
from django.utils.text import slugify | ||
|
||
from sample.models import Book, Edition | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CloneSignalsTestCase(TestCase): | ||
REPLICA_DB_ALIAS = "replica" | ||
databases = { | ||
"default", | ||
"replica", | ||
} | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.user = User.objects.create(username="user") | ||
|
||
def test_signals(self): | ||
name = "New Book" | ||
first_published_at = timezone.datetime( | ||
1970, 1, 1, tzinfo=timezone.get_default_timezone() | ||
) | ||
book = Book.objects.create( | ||
name=name, | ||
created_by=self.user, | ||
slug=slugify(name), | ||
published_at=first_published_at, | ||
) | ||
self.assertEqual(book.published_at, first_published_at) | ||
edition = Edition.objects.create(seq=1, book=book) | ||
cloned_edition = edition.make_clone() | ||
self.assertEqual(cloned_edition.seq, 2) | ||
book.refresh_from_db() | ||
self.assertNotEqual(book.published_at, first_published_at) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default_app_config = "sample.apps.SampleConfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
# pylint: disable=unused-import | ||
from django.apps import AppConfig | ||
|
||
|
||
class SampleConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "sample" | ||
|
||
def ready(self): | ||
from . import signals # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.dispatch import receiver | ||
from django.utils import timezone | ||
|
||
from model_clone.signals import post_clone_save, pre_clone_save | ||
|
||
from .models import Edition | ||
|
||
|
||
@receiver(pre_clone_save, sender=Edition) | ||
def increase_seq(sender, instance, **kwargs): | ||
instance.seq += 1 | ||
|
||
|
||
@receiver(post_clone_save, sender=Edition) | ||
def update_book_published_at(sender, instance, **kwargs): | ||
if instance.book: | ||
instance.book.published_at = timezone.now() | ||
instance.book.save(update_fields=["published_at"]) |