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

50 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-20 09:24 +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_children_toggle.html", takes_context=True 

40) 

41def collection_children_toggle(context, obj, collection): 

42 """ 

43 Provide toggle buttons for all the children of a parent collection. 

44 """ 

45 context["children"] = collection.children() 

46 context["object"] = obj 

47 context["collection"] = collection 

48 return context 

49 

50 

51@register.inclusion_tag( 

52 "collections/collection_children_toggle.html", takes_context=True 

53) 

54def collection_children_toggle_by_id(context, obj, collectionid): 

55 """ 

56 Wrapper templatetag to allow using `collection_children_toggle` with 

57 just the `id` of the parent collection. 

58 """ 

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

60 return collection_children_toggle(context, obj, collection) 

61 

62 

63@register.inclusion_tag("collections/collection_object_parent.html", takes_context=True) 

64def collection_object_parent(context, obj, collectionobject): 

65 """ 

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

67 a collection to point to the collections parent. 

68 """ 

69 context["collectionobject"] = collectionobject 

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

71 context["object"] = obj 

72 return context 

73 

74 

75@register.inclusion_tag("collections/collection_object_parent.html", takes_context=True) 

76def collection_object_parent_by_id(context, obj, collectionobject_id): 

77 """ 

78 Wrapper templatetag to allow using `collection_object_parent` with 

79 just the `id` of the collectionobject. 

80 """ 

81 collectionobject = SkosCollectionContentObject.objects.get(pk=collectionobject_id) 

82 return collection_object_parent(context, obj, collectionobject) 

83 

84 

85@register.simple_tag(takes_context=True) 

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

87 content_type = ContentType.objects.get_for_model(obj) 

88 sccos = SkosCollectionContentObject.objects.filter( 

89 content_type=content_type, object_id=obj.id 

90 ) 

91 if collectionids is not None: 

92 ids = collectionids.split(",") 

93 sccos = sccos.filter(collection__id__in=ids) 

94 return sccos 

95 

96 

97@register.inclusion_tag( 

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

99) 

100def collection_session_toggle_by_id(context, collection_id): 

101 """ 

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

103 The checkbox calls the CollectionSessionToggle view. 

104 """ 

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

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

107 context["enabled"] = collection_id in session_collections 

108 return context