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

57 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-16 07:42 +0000

1from django.contrib.auth.mixins import LoginRequiredMixin 

2from django.contrib.contenttypes.models import ContentType 

3from django.shortcuts import get_object_or_404, redirect 

4from django.views.generic.base import TemplateView 

5 

6from .models import SkosCollection, SkosCollectionContentObject 

7 

8 

9class ContentObjectMixin: 

10 """ 

11 Setup the ContentType and the object used by a view, based on the 

12 `content_type_id` and the `object_id` arguments passed in the URL. 

13 """ 

14 

15 def setup(self, *args, **kwargs): 

16 super().setup(*args, **kwargs) 

17 self.content_type = get_object_or_404(ContentType, pk=kwargs["content_type_id"]) 

18 self.object = get_object_or_404( 

19 self.content_type.model_class(), pk=kwargs["object_id"] 

20 ) 

21 

22 def get_context_data(self, *args, **kwargs): 

23 context = super().get_context_data(*args, **kwargs) 

24 context["content_type"] = self.content_type 

25 context["object"] = self.object 

26 return context 

27 

28 

29class CollectionToggle(LoginRequiredMixin, ContentObjectMixin, TemplateView): 

30 """ 

31 Toggle a collection - if a CollectionObject connecting the requested object 

32 and collection does not exist, then create it. If it does exist, delete it. 

33 """ 

34 

35 template_name = "collections/collection_toggle.html" 

36 

37 def setup(self, *args, **kwargs): 

38 super().setup(*args, **kwargs) 

39 self.collection = get_object_or_404(SkosCollection, pk=kwargs["collection"]) 

40 

41 def get_context_data(self, *args, **kwargs): 

42 context = super().get_context_data(*args, **kwargs) 

43 context["exists"] = self.created 

44 context["collection"] = self.collection 

45 return context 

46 

47 def get(self, *args, **kwargs): 

48 scco, self.created = SkosCollectionContentObject.objects.get_or_create( 

49 collection=self.collection, 

50 content_type=self.content_type, 

51 object_id=self.object.id, 

52 ) 

53 if not self.created: 

54 scco.delete() 

55 if redirect_to := self.request.GET.get("to", False): 

56 return redirect(redirect_to) 

57 return super().get(*args, **kwargs) 

58 

59 

60class CollectionObjectParent(LoginRequiredMixin, ContentObjectMixin, TemplateView): 

61 """ 

62 Change the requested CollectionObjects collection to point to the parent of the 

63 current collection. 

64 """ 

65 

66 template_name = "collections/collection_object_parent.html" 

67 

68 def get_context_data(self, *args, **kwargs): 

69 collectionobject = get_object_or_404( 

70 SkosCollectionContentObject, pk=kwargs["collectionobject"] 

71 ) 

72 if collectionobject.collection.parent: 

73 collectionobject.collection = collectionobject.collection.parent 

74 collectionobject.save() 

75 context = super().get_context_data(*args, **kwargs) 

76 context["collectionobject"] = collectionobject 

77 return context 

78 

79 

80class CollectionSessionToggle(LoginRequiredMixin, TemplateView): 

81 """ 

82 Toggle the existence of an SkosCollection in the `session_collections` 

83 session variable. 

84 This can be used in combination with the 

85 `collections.signals.add_to_session_collection` signal, to add objects 

86 to a collection if the collections id is listed in the session variable. 

87 The equivalent templateatag that calls this view is 

88 `collections.templatetags.collections.collection_session_toggle_by_id` 

89 """ 

90 

91 template_name = "collections/collection_session_toggle.html" 

92 

93 def get_context_data(self, *args, **kwargs): 

94 ctx = super().get_context_data(*args, **kwargs) 

95 ctx["collection"] = self.skoscollection 

96 ctx["enabled"] = self.skoscollection.id in self.session_collections 

97 return ctx 

98 

99 def get(self, *args, **kwargs): 

100 self.skoscollection = get_object_or_404( 

101 SkosCollection, pk=kwargs["skoscollection"] 

102 ) 

103 self.session_collections = set( 

104 self.request.session.get("session_collections", []) 

105 ) 

106 self.session_collections ^= {self.skoscollection.id} 

107 self.request.session["session_collections"] = list(self.session_collections) 

108 if redirect_to := self.request.GET.get("to", False): 

109 return redirect(redirect_to) 

110 return super().get(*args, **kwargs)