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

50 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-09-14 10:55 +0000

1from django import template 

2from django.contrib.contenttypes.models import ContentType 

3from django.db import models 

4 

5from apis_core.collections.models import SkosCollection, SkosCollectionContentObject 

6 

7register = template.Library() 

8 

9 

10### templatetags for the APIS collections model ### 

11 

12 

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

14def collection_toggle(context, obj, collection): 

15 """ 

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

17 collection and an object. 

18 """ 

19 content_type = ContentType.objects.get_for_model(obj) 

20 context["content_type"] = content_type 

21 context["object"] = obj 

22 context["collection"] = collection 

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

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

25 ).exists() 

26 return context 

27 

28 

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

30def collection_toggle_by_id(context, obj, collectionid): 

31 """ 

32 Wrapper templatetag to allow using `collection_toggle` 

33 with just the `id` of the collection. 

34 """ 

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

36 return collection_toggle(context, obj, collection) 

37 

38 

39@register.inclusion_tag( 

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

41) 

42def collection_object_collection(context, obj, skoscollectioncollectionobjects): 

43 """ 

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

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

46 """ 

47 if len(skoscollectioncollectionobjects) > 1: 

48 context["error"] = ( 

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

50 ) 

51 if len(skoscollectioncollectionobjects) == 1: 

52 collectionobject = skoscollectioncollectionobjects.first() 

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

54 context["collectionobject"] = collectionobject 

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

56 context["object"] = obj 

57 return context 

58 

59 

60@register.inclusion_tag( 

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

62) 

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

64 """ 

65 Wrapper templatetag to allow using `collection_object_parent` with 

66 just the `ids` of collections. 

67 """ 

68 content_type = ContentType.objects.get_for_model(obj) 

69 sccos = SkosCollectionContentObject.objects.filter( 

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

71 ) 

72 return collection_object_collection(context, obj, sccos) 

73 

74 

75@register.simple_tag(takes_context=True) 

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

77 content_type = ContentType.objects.get_for_model(obj) 

78 sccos = SkosCollectionContentObject.objects.filter( 

79 content_type=content_type, object_id=obj.id 

80 ) 

81 if collectionids is not None: 

82 ids = collectionids.split(",") 

83 sccos = sccos.filter(collection__id__in=ids) 

84 return sccos 

85 

86 

87@register.inclusion_tag( 

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

89) 

90def collection_session_toggle_by_id(context, collection_id): 

91 """ 

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

93 The checkbox calls the CollectionSessionToggle view. 

94 """ 

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

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

97 context["enabled"] = collection_id in session_collections 

98 return context 

99 

100 

101@register.simple_tag 

102def get_collectionsmodels() -> list[models.Model]: 

103 """ 

104 Return a list of `Collection` models 

105 """ 

106 return [SkosCollection, SkosCollectionContentObject]