Coverage for apis_core/apis_relations/signals.py: 60%
25 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:51 +0000
1import logging
3from django.dispatch import receiver
5from apis_core.apis_entities.signals import post_merge_with
6from apis_core.apis_metainfo.models import RootObject
7from apis_core.apis_metainfo.signals import post_duplicate
8from apis_core.apis_relations.models import TempTriple
10logger = logging.getLogger(__name__)
13@receiver(post_duplicate)
14def copy_relations(sender, instance, duplicate, **kwargs):
15 logger.info(f"Copying relations from {instance} to {duplicate}")
16 if isinstance(instance, RootObject):
17 for rel in TempTriple.objects.filter(subj=instance):
18 newrel = rel.duplicate()
19 newrel.subj = duplicate
20 newrel.save()
21 for rel in TempTriple.objects.filter(obj=instance):
22 newrel = rel.duplicate()
23 newrel.obj = duplicate
24 newrel.save()
27@receiver(post_merge_with)
28def merge_relations(sender, instance, entities, **kwargs):
29 for ent in entities:
30 logger.info(f"Merging relations from {ent!r} into {instance!r}")
31 TempTriple.objects.filter(obj__id=ent.id).update(obj=instance)
32 TempTriple.objects.filter(subj__id=ent.id).update(subj=instance)