-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace custom duplication logic with signal
For duplicating entities, there was custom logic to copy ManyToManyField - which was tailored to TempTriples. This had the downside of simply creating new objects by hand. This commit introduces a `duplicate` method in the Triple model, which implements the functionality of duplicating itself and also triggers pre_- and post_duplicate signals. This has the advantage that instances that point to the Triple (like References) can also be duplicated using signals.
- Loading branch information
Showing
4 changed files
with
40 additions
and
28 deletions.
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
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,18 @@ | ||
from apis_core.apis_relations.models import TempTriple | ||
from apis_core.apis_metainfo.models import RootObject | ||
from apis_core.apis_metainfo.signals import post_duplicate | ||
from django.dispatch import receiver | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@receiver(post_duplicate) | ||
def copy_relations(sender, instance, duplicate, **kwargs): | ||
logger.info(f"Copying relations from {instance} to {duplicate}") | ||
if isinstance(instance, RootObject): | ||
for rel in TempTriple.objects.filter(subj=instance): | ||
newrel = rel.duplicate() | ||
newrel.subj = duplicate | ||
newrel.save() |