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