Coverage for apis_core/collections/views.py: 38%
84 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-09-03 06:15 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-09-03 06:15 +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.urls import reverse
5from django.views.generic.base import TemplateView
6from django.views.generic.edit import FormView
8from apis_core.collections.forms import CollectionObjectForm
9from apis_core.generic.views import GenericModelMixin
11from .models import SkosCollection, SkosCollectionContentObject
14class ContentObjectMixin:
15 """
16 Setup the ContentType and the object used by a view, based on the
17 `content_type_id` and the `object_id` arguments passed in the URL.
18 """
20 def setup(self, *args, **kwargs):
21 super().setup(*args, **kwargs)
22 self.obj_content_type = get_object_or_404(
23 ContentType, pk=kwargs["content_type_id"]
24 )
25 self.object = get_object_or_404(
26 self.obj_content_type.model_class(), pk=kwargs["object_id"]
27 )
29 def get_context_data(self, *args, **kwargs):
30 context = super().get_context_data(*args, **kwargs)
31 context["content_type"] = self.obj_content_type
32 context["object"] = self.object
33 return context
36class CollectionObjectFormView(LoginRequiredMixin, GenericModelMixin, FormView):
37 template_name = "collections/collection_object_form.html"
38 form_class = CollectionObjectForm
40 def get_object(self):
41 return self.queryset.get(pk=self.kwargs.get("pk"))
43 def get_initial(self):
44 initial = super().get_initial()
45 initial["collections"] = SkosCollection.objects.by_instance(
46 self.get_object()
47 ).values_list("pk", flat=True)
48 return initial
50 def get_success_url(self):
51 return reverse(
52 "apis_core:collections:collectionobjectformview",
53 args=[self.kwargs.get("contenttype"), self.get_object().id],
54 )
56 def form_valid(self, form):
57 instance = self.get_object()
58 collections = form.cleaned_data.get("collections", [])
59 for collection in SkosCollection.objects.exclude(pk__in=collections):
60 collection.remove(instance)
61 for collection in SkosCollection.objects.filter(pk__in=collections):
62 collection.add(instance)
63 return super().form_valid(form)
65 def get_context_data(self, **kwargs):
66 context = super().get_context_data(**kwargs)
67 context["object"] = self.get_object()
68 return context
71class CollectionToggle(LoginRequiredMixin, ContentObjectMixin, TemplateView):
72 """
73 Toggle a collection - if a CollectionObject connecting the requested object
74 and collection does not exist, then create it. If it does exist, delete it.
75 """
77 template_name = "collections/collection_toggle.html"
79 def setup(self, *args, **kwargs):
80 super().setup(*args, **kwargs)
81 self.collection = get_object_or_404(SkosCollection, pk=kwargs["collection"])
83 def get_context_data(self, *args, **kwargs):
84 context = super().get_context_data(*args, **kwargs)
85 context["exists"] = self.created
86 context["collection"] = self.collection
87 return context
89 def get(self, *args, **kwargs):
90 scco, self.created = SkosCollectionContentObject.objects.get_or_create(
91 collection=self.collection,
92 content_type=self.obj_content_type,
93 object_id=self.object.id,
94 )
95 if not self.created:
96 scco.delete()
97 if redirect_to := self.request.GET.get("to", False):
98 return redirect(redirect_to)
99 return super().get(*args, **kwargs)
102class CollectionObjectCollection(LoginRequiredMixin, ContentObjectMixin, TemplateView):
103 """
104 Change the requested CollectionObjects collection to point to the collection the
105 current collection is in.
106 """
108 template_name = "collections/collection_object_collection.html"
110 def get_context_data(self, *args, **kwargs):
111 collectionobject = get_object_or_404(
112 SkosCollectionContentObject, pk=kwargs["collectionobject"]
113 )
114 parent = collectionobject.collection.parent_collection
115 collectionobject.collection = parent
116 collectionobject.save()
117 context = super().get_context_data(*args, **kwargs)
118 context["collectionobject"] = collectionobject
119 return context
122class CollectionSessionToggle(LoginRequiredMixin, TemplateView):
123 """
124 Toggle the existence of an SkosCollection in the `session_collections`
125 session variable.
126 This can be used in combination with the
127 `collections.signals.add_to_session_collection` signal, to add objects
128 to a collection if the collections id is listed in the session variable.
129 The equivalent templateatag that calls this view is
130 `collections.templatetags.collections.collection_session_toggle_by_id`
131 """
133 template_name = "collections/collection_session_toggle.html"
135 def get_context_data(self, *args, **kwargs):
136 ctx = super().get_context_data(*args, **kwargs)
137 ctx["collection"] = self.skoscollection
138 ctx["enabled"] = self.skoscollection.id in self.session_collections
139 return ctx
141 def get(self, *args, **kwargs):
142 self.skoscollection = get_object_or_404(
143 SkosCollection, pk=kwargs["skoscollection"]
144 )
145 self.session_collections = set(
146 self.request.session.get("session_collections", [])
147 )
148 self.session_collections ^= {self.skoscollection.id}
149 self.request.session["session_collections"] = list(self.session_collections)
150 if redirect_to := self.request.GET.get("to", False):
151 return redirect(redirect_to)
152 return super().get(*args, **kwargs)