Coverage for apis_core/collections/signals.py: 69%
16 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-20 09:24 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-20 09:24 +0000
1from crum import get_current_request
2from django.contrib import messages
3from django.contrib.contenttypes.models import ContentType
4from django.db.models.signals import post_save
5from django.dispatch import receiver
7from apis_core.collections.models import SkosCollection, SkosCollectionContentObject
8from apis_core.history.models import APISHistoryTableBase
11@receiver(post_save)
12def add_to_session_collection(
13 sender, instance, created, raw, using, update_fields, **kwargs
14):
15 """
16 Add a created apis_core.history model instance to all the SkosCollections
17 that are listed in the `session_collections` session variable.
18 This needs the 'crum.CurrentRequestUserMiddleware' middleware to
19 be enabled.
20 """
21 request = get_current_request()
22 if isinstance(instance, APISHistoryTableBase) and request:
23 for pk in request.session.get("session_collections", []):
24 sc = SkosCollection.objects.get(pk=pk)
25 content_type = ContentType.objects.get_for_model(instance)
26 SkosCollectionContentObject.objects.create(
27 collection=sc,
28 content_type=content_type,
29 object_id=instance.history_id,
30 )
31 messages.info(request, f"Tagged {instance} with tag {sc}")