-
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.
feat(relations): implement duplicate and merge signal receivers
Closes: #1251
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import logging | ||
|
||
from django.dispatch import receiver | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from apis_core.apis_entities.signals import post_merge_with | ||
from apis_core.apis_metainfo.signals import post_duplicate | ||
|
||
from apis_core.relations.models import Relation | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@receiver(post_duplicate) | ||
def copy_relations(sender, instance, duplicate, **kwargs): | ||
logger.info(f"Copying relations from {instance!r} to {duplicate!r}") | ||
content_type = ContentType.objects.get_for_model(instance) | ||
subj_rels = Relation.objects.filter(subj_content_type=content_type, subj_object_id=instance.id).select_subclasses() | ||
obj_rels = Relation.objects.filter(obj_content_type=content_type, obj_object_id=instance.id).select_subclasses() | ||
for rel in subj_rels: | ||
rel.pk = None | ||
rel.subj_object_id = duplicate.id | ||
rel.save() | ||
for rel in obj_rels: | ||
rel.pk = None | ||
rel.obj_object_id = duplicate.id | ||
rel.save() | ||
|
||
|
||
@receiver(post_merge_with) | ||
def merge_relations(sender, instance, entities, **kwargs): | ||
for ent in entities: | ||
logger.info(f"Merging relations from {ent!r} into {instance!r}") | ||
content_type = ContentType.objects.get_for_model(ent) | ||
Relation.objects.filter(subj_content_type=content_type, subj_object_id=ent.id).update(subj_object_id=instance.id) | ||
Relation.objects.filter(obj_content_type=content_type, obj_object_id=ent.id).update(obj_object_id=instance.id) |