Improve instance delete
This commit is contained in:
parent
09ce5406ee
commit
3a8b9987b2
6 changed files with 46 additions and 27 deletions
|
@ -6,7 +6,7 @@ import urlman
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import IntegrityError, models
|
from django.db import IntegrityError, models, transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
@ -636,6 +636,7 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
_("Error updating instance: {error}").format(error=str(e))
|
_("Error updating instance: {error}").format(error=str(e))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
def delete_instance(self, user):
|
def delete_instance(self, user):
|
||||||
"""
|
"""
|
||||||
Soft deletes the instance in Django and initiates deletion of the
|
Soft deletes the instance in Django and initiates deletion of the
|
||||||
|
|
|
@ -88,33 +88,28 @@ class ServiceInstanceFilterForm(forms.Form):
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class ServiceInstanceDeleteForm(forms.Form):
|
class ServiceInstanceDeleteForm(forms.ModelForm):
|
||||||
name_confirm = forms.CharField(
|
name = forms.CharField(
|
||||||
label=_("Instance Name"),
|
label=_("Instance Name"),
|
||||||
max_length=63,
|
max_length=63,
|
||||||
widget=forms.TextInput(attrs={"class": "form-control"}),
|
widget=forms.TextInput(attrs={"class": "form-control"}),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.instance_name = kwargs.pop("instance_name", None)
|
kwargs["initial"] = {"name": ""}
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
if self.instance_name:
|
self.fields["name"].help_text = _(
|
||||||
self.fields["name_confirm"].help_text = _(
|
"To confirm deletion, please type the instance name: <strong>{instance_name}</strong>"
|
||||||
"To confirm deletion, please type the instance name: <strong>{instance_name}</strong>"
|
).format(instance_name=self.instance.name)
|
||||||
).format(instance_name=self.instance_name)
|
|
||||||
else:
|
|
||||||
self.fields["name_confirm"].help_text = _(
|
|
||||||
"Please type the instance name to confirm deletion."
|
|
||||||
)
|
|
||||||
|
|
||||||
def clean_name_confirm(self):
|
def clean_name(self):
|
||||||
entered_name = self.cleaned_data.get("name_confirm")
|
entered_name = self.cleaned_data.get("name")
|
||||||
if not self.instance_name:
|
if entered_name != self.instance.name:
|
||||||
raise ValidationError(
|
|
||||||
_("Cannot confirm deletion: instance name not provided to form.")
|
|
||||||
)
|
|
||||||
if entered_name != self.instance_name:
|
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_("The entered name does not match the instance name. Deletion not confirmed.")
|
_("The entered name does not match the instance name. Deletion not confirmed.")
|
||||||
)
|
)
|
||||||
return entered_name
|
return entered_name
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = ServiceInstance
|
||||||
|
fields = ("name", )
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% if form.non_field_errors or form.errors %}
|
{% if form.non_field_errors or form.errors %}
|
||||||
<div class="alert alert-danger" role="alert">
|
<div class="alert alert-danger form-errors" role="alert">
|
||||||
<div>
|
<div>
|
||||||
{% if form.non_field_errors %}
|
{% if form.non_field_errors %}
|
||||||
{% if form.non_field_errors|length > 1 %}
|
{% if form.non_field_errors|length > 1 %}
|
||||||
|
|
|
@ -6,9 +6,20 @@
|
||||||
{% endblock page_title %}
|
{% endblock page_title %}
|
||||||
{% endblock html_title %}
|
{% endblock html_title %}
|
||||||
{% block page_title_extra %}
|
{% block page_title_extra %}
|
||||||
{% if has_change_permission %}
|
<div>
|
||||||
<a href="{{ instance.urls.update }}" class="btn btn-primary me-1 mb-1">{% translate "Edit" %}</a>
|
{% if has_change_permission and not instance.is_deleted %}
|
||||||
{% endif %}
|
<a href="{{ instance.urls.update }}" class="btn btn-primary me-1 mb-1">{% translate "Edit" %}</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if has_delete_permission and not instance.is_deleted %}
|
||||||
|
<button type="button" class="btn btn-danger me-1 mb-1"
|
||||||
|
hx-get="{{ instance.urls.delete }}"
|
||||||
|
hx-target="#deleteInstanceModalContent"
|
||||||
|
data-bs-toggle="modal"
|
||||||
|
data-bs-target="#deleteInstanceModal">
|
||||||
|
{% translate "Delete" %}
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
{% endblock page_title_extra %}
|
{% endblock page_title_extra %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<section class="section">
|
<section class="section">
|
||||||
|
@ -47,7 +58,14 @@
|
||||||
<dt class="col-sm-4">{% translate "Status" %}</dt>
|
<dt class="col-sm-4">{% translate "Status" %}</dt>
|
||||||
<dd class="col-sm-8">
|
<dd class="col-sm-8">
|
||||||
{% if instance.is_deleted %}
|
{% if instance.is_deleted %}
|
||||||
<span class="badge text-bg-secondary">{% translate "Deleted" %}</span>
|
<span class="badge text-bg-danger">{% translate "Deleted" %}</span>
|
||||||
|
{% if instance.deleted_at %}
|
||||||
|
<small class="text-muted d-block mt-1">
|
||||||
|
{% blocktranslate with date=instance.deleted_at|date:"SHORT_DATETIME_FORMAT" user=instance.deleted_by|default:_("system") %}
|
||||||
|
On {{ date }} by {{ user }}
|
||||||
|
{% endblocktranslate %}
|
||||||
|
</small>
|
||||||
|
{% endif %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<span class="badge text-bg-success">{% translate "Active" %}</span>
|
<span class="badge text-bg-success">{% translate "Active" %}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -56,7 +74,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% if instance.status_conditions %}
|
{% if not instance.is_deleted and instance.status_conditions %}
|
||||||
<div class="col-12 col-md-7">
|
<div class="col-12 col-md-7">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
|
@ -101,7 +119,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if instance.spec %}
|
{% if not instance.is_deleted and instance.spec and spec_fieldsets %}
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
|
|
|
@ -423,7 +423,9 @@ class ServiceInstanceDeleteView(
|
||||||
name=self.object.name, error=str(e)
|
name=self.object.name, error=str(e)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
return self.form_invalid(form)
|
response = HttpResponse()
|
||||||
|
response["HX-Redirect"] = str(self.object.urls.base)
|
||||||
|
return response
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return self.request.organization.urls.instances
|
return self.request.organization.urls.instances
|
||||||
|
|
|
@ -90,3 +90,6 @@ a.btn-keycloak {
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
}
|
}
|
||||||
|
.hide-form-errors .alert.form-errors {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue