From 032596c0e4abf41b7cad4233089b59cad9fd7850 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 20 May 2025 22:39:32 +0200 Subject: [PATCH] Refactor common view functionality into mixin --- src/servala/frontend/views/service.py | 41 +++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/servala/frontend/views/service.py b/src/servala/frontend/views/service.py index ab4fa0e..1ee46b4 100644 --- a/src/servala/frontend/views/service.py +++ b/src/servala/frontend/views/service.py @@ -159,15 +159,15 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView return self.render_to_response(context) -class ServiceInstanceDetailView(OrganizationViewMixin, DetailView): - """View to display details of a specific service instance.""" - - template_name = "frontend/organizations/service_instance_detail.html" - context_object_name = "instance" +class ServiceInstanceMixin: model = ServiceInstance - permission_type = "view" + context_object_name = "instance" slug_field = "name" + def dispatch(self, *args, **kwargs): + self._has_warned = False + return super().dispatch(*args, **kwargs) + def get_queryset(self): """Return service instance for the current organization.""" return ServiceInstance.objects.filter( @@ -178,6 +178,35 @@ class ServiceInstanceDetailView(OrganizationViewMixin, DetailView): "context__service_definition__service", ) + def get_object(self, **kwargs): + instance = super().get_object(**kwargs) + if ( + not instance.kubernetes_object + and not instance.is_deleted + and not self._has_warned + ): + messages.warning( + self.request, + _( + "Could not retrieve instance details from Kubernetes. It might have been deleted externally." + ), + ) + self._has_warned = True + return instance + + +class ServiceInstanceDetailView( + ServiceInstanceMixin, OrganizationViewMixin, DetailView +): + template_name = "frontend/organizations/service_instance_detail.html" + permission_type = "view" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + if self.object.kubernetes_object and self.object.spec: + context["spec_fieldsets"] = self.get_nested_spec() + return context + def get_nested_spec(self): """ Organize spec data into fieldsets similar to how the form does it.