Coverage for apis_core/collections/templatetags/collections.py: 35%

46 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-06-25 10:00 +0000

1from django import template 

2from django.contrib.contenttypes.models import ContentType 

3 

4from apis_core.collections.models import SkosCollection, SkosCollectionContentObject 

5 

6register = template.Library() 

7 

8 

9### templatetags for the APIS collections model ### 

10 

11 

12@register.inclusion_tag("collections/collection_toggle.html", takes_context=True) 

13def collection_toggle(context, obj, collection): 

14 """ 

15 Provide a button to add or remove a connection between a 

16 collection and an object. 

17 """ 

18 content_type = ContentType.objects.get_for_model(obj) 

19 context["content_type"] = content_type 

20 context["object"] = obj 

21 context["collection"] = collection 

22 context["exists"] = SkosCollectionContentObject.objects.filter( 

23 object_id=obj.id, content_type=content_type, collection=collection 

24 ).exists() 

25 return context 

26 

27 

28@register.inclusion_tag("collections/collection_toggle.html", takes_context=True) 

29def collection_toggle_by_id(context, obj, collectionid): 

30 """ 

31 Wrapper templatetag to allow using `collection_toggle` 

32 with just the `id` of the collection. 

33 """ 

34 collection = SkosCollection.objects.get(pk=collectionid) 

35 return collection_toggle(context, obj, collection) 

36 

37 

38@register.inclusion_tag( 

39 "collections/collection_object_collection.html", takes_context=True 

40) 

41def collection_object_collection(context, obj, skoscollectioncollectionobjects): 

42 """ 

43 Provide a button to change the connection between an object and 

44 a collection to point to the collection the collection is in. 

45 """ 

46 if len(skoscollectioncollectionobjects) > 1: 

47 context["error"] = ( 

48 "Multiple collections found to toggle, please check `collection_object_collection`" 

49 ) 

50 if len(skoscollectioncollectionobjects) == 1: 

51 collectionobject = skoscollectioncollectionobjects.first() 

52 context["parent"] = collectionobject.collection.parent_collection 

53 context["collectionobject"] = collectionobject 

54 context["content_type"] = ContentType.objects.get_for_model(obj) 

55 context["object"] = obj 

56 return context 

57 

58 

59@register.inclusion_tag( 

60 "collections/collection_object_collection.html", takes_context=True 

61) 

62def collection_object_collection_by_id(context, obj, *collection_ids): 

63 """ 

64 Wrapper templatetag to allow using `collection_object_parent` with 

65 just the `ids` of collections. 

66 """ 

67 content_type = ContentType.objects.get_for_model(obj) 

68 sccos = SkosCollectionContentObject.objects.filter( 

69 collection__in=collection_ids, content_type=content_type, object_id=obj.id 

70 ) 

71 return collection_object_collection(context, obj, sccos) 

72 

73 

74@register.simple_tag(takes_context=True) 

75def collection_content_objects(context, obj, collectionids=None): 

76 content_type = ContentType.objects.get_for_model(obj) 

77 sccos = SkosCollectionContentObject.objects.filter( 

78 content_type=content_type, object_id=obj.id 

79 ) 

80 if collectionids is not None: 

81 ids = collectionids.split(",") 

82 sccos = sccos.filter(collection__id__in=ids) 

83 return sccos 

84 

85 

86@register.inclusion_tag( 

87 "collections/collection_session_toggle.html", takes_context=True 

88) 

89def collection_session_toggle_by_id(context, collection_id): 

90 """ 

91 Provide a checkbox to toggle if a session collection is active. 

92 The checkbox calls the CollectionSessionToggle view. 

93 """ 

94 session_collections = context.request.session.get("session_collections", []) 

95 context["collection"] = SkosCollection.objects.get(pk=collection_id) 

96 context["enabled"] = collection_id in session_collections 

97 return context