Implement service instance update
This commit is contained in:
parent
fd3cb6a1d4
commit
eb4d3f9556
2 changed files with 87 additions and 8 deletions
|
@ -0,0 +1,39 @@
|
|||
{% extends "frontend/base.html" %}
|
||||
{% load i18n %}
|
||||
{% load static %}
|
||||
{% load partials %}
|
||||
{% block html_title %}
|
||||
{% block page_title %}
|
||||
{% block title %}
|
||||
{% blocktranslate with instance_name=instance.name organization_name=request.organization.name %}Update {{ instance_name }} in {{ organization_name }}{% endblocktranslate %}
|
||||
{% endblock %}
|
||||
{% endblock page_title %}
|
||||
{% endblock html_title %}
|
||||
{% partialdef service-form %}
|
||||
{% if form %}
|
||||
<div class="card">
|
||||
<div class="card-header d-flex align-items-center"></div>
|
||||
<div class="card-body">
|
||||
{% if form_error %}
|
||||
<div class="alert alert-danger">
|
||||
{% translate "Oops! Something went wrong with the service form generation. Please try again later." %}
|
||||
</div>
|
||||
{% else %}
|
||||
{% include "includes/tabbed_fieldset_form.html" with form=form %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endpartialdef %}
|
||||
{% block content %}
|
||||
<section class="section">
|
||||
<div class="card">
|
||||
{% if not form %}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
{% translate "Cannot update this service instance because its details could not be retrieved from the underlying system. It might have been deleted externally." %}
|
||||
</div>
|
||||
{% else %}
|
||||
<div id="service-form">{% partial service-form %}</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endblock %}
|
|
@ -17,7 +17,11 @@ from servala.frontend.forms.service import (
|
|||
ServiceFilterForm,
|
||||
ServiceInstanceFilterForm,
|
||||
)
|
||||
from servala.frontend.views.mixins import HtmxViewMixin, OrganizationViewMixin
|
||||
from servala.frontend.views.mixins import (
|
||||
HtmxUpdateView,
|
||||
HtmxViewMixin,
|
||||
OrganizationViewMixin,
|
||||
)
|
||||
|
||||
|
||||
class ServiceListView(OrganizationViewMixin, ListView):
|
||||
|
@ -310,12 +314,43 @@ class ServiceInstanceDetailView(
|
|||
|
||||
return fieldsets
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return service instance for the current organization."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
if self.object.spec:
|
||||
context["spec_fieldsets"] = self.get_nested_spec()
|
||||
return context
|
||||
|
||||
class ServiceInstanceUpdateView(
|
||||
ServiceInstanceMixin, OrganizationViewMixin, HtmxUpdateView
|
||||
):
|
||||
template_name = "frontend/organizations/service_instance_update.html"
|
||||
permission_type = "change"
|
||||
|
||||
def get_form_class(self):
|
||||
return self.object.context.model_form_class
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs["instance"] = self.object.spec_object
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
spec_data = form.get_nested_data().get("spec")
|
||||
self.object.update_spec(spec_data=spec_data, updated_by=self.request.user)
|
||||
messages.success(
|
||||
self.request,
|
||||
_("Service instance '{name}' updated successfully.").format(
|
||||
name=self.object.name
|
||||
),
|
||||
)
|
||||
return redirect(self.object.urls.base)
|
||||
except ValidationError as e:
|
||||
messages.error(self.request, e.message or str(e))
|
||||
return self.form_invalid(form)
|
||||
except Exception as e:
|
||||
messages.error(
|
||||
self.request, _("Error updating instance: {error}").format(error=str(e))
|
||||
)
|
||||
return self.form_invalid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
return self.object.urls.base
|
||||
|
||||
|
||||
class ServiceInstanceListView(OrganizationViewMixin, ListView):
|
||||
|
@ -331,7 +366,12 @@ class ServiceInstanceListView(OrganizationViewMixin, ListView):
|
|||
def get_queryset(self):
|
||||
"""Return all service instances for the current organization with filtering."""
|
||||
queryset = ServiceInstance.objects.filter(
|
||||
organization=self.request.organization
|
||||
organization=self.request.organization,
|
||||
is_deleted=False, # Exclude soft-deleted
|
||||
).select_related(
|
||||
"context__service_offering__provider",
|
||||
"context__control_plane",
|
||||
"context__service_definition__service",
|
||||
)
|
||||
if self.filter_form.is_valid():
|
||||
queryset = self.filter_form.filter_queryset(queryset)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue