Add filter and search to instance list
This commit is contained in:
parent
23ad1c809b
commit
4495899c02
3 changed files with 128 additions and 41 deletions
|
@ -1,7 +1,13 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from servala.core.models import CloudProvider, ControlPlane, ServiceCategory
|
from servala.core.models import (
|
||||||
|
CloudProvider,
|
||||||
|
ControlPlane,
|
||||||
|
Service,
|
||||||
|
ServiceCategory,
|
||||||
|
ServiceOffering,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ServiceFilterForm(forms.Form):
|
class ServiceFilterForm(forms.Form):
|
||||||
|
@ -33,3 +39,52 @@ class ControlPlaneSelectForm(forms.Form):
|
||||||
def __init__(self, *args, planes=None, **kwargs):
|
def __init__(self, *args, planes=None, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["control_plane"].queryset = planes
|
self.fields["control_plane"].queryset = planes
|
||||||
|
|
||||||
|
|
||||||
|
class ServiceInstanceFilterForm(forms.Form):
|
||||||
|
name = forms.CharField(required=False, label=_("Name"))
|
||||||
|
service = forms.ModelChoiceField(
|
||||||
|
queryset=Service.objects.all(), required=False, label=_("Service")
|
||||||
|
)
|
||||||
|
provider = forms.ModelChoiceField(
|
||||||
|
queryset=ServiceOffering.objects.all()
|
||||||
|
.values_list("provider", flat=True)
|
||||||
|
.distinct(),
|
||||||
|
required=False,
|
||||||
|
label=_("Provider"),
|
||||||
|
)
|
||||||
|
control_plane = forms.ModelChoiceField(
|
||||||
|
queryset=ControlPlane.objects.all(),
|
||||||
|
required=False,
|
||||||
|
label=_("Service Provider Zone"),
|
||||||
|
)
|
||||||
|
status = forms.ChoiceField(
|
||||||
|
choices=(
|
||||||
|
("active", _("Active")),
|
||||||
|
("deleted", _("Deleted")),
|
||||||
|
),
|
||||||
|
required=False,
|
||||||
|
label=_("Status"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def filter_queryset(self, queryset):
|
||||||
|
if self.is_valid():
|
||||||
|
data = self.cleaned_data
|
||||||
|
if data["name"]:
|
||||||
|
queryset = queryset.filter(name__icontains=data["name"])
|
||||||
|
if data["service"]:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
context__service_definition__service=data["service"]
|
||||||
|
)
|
||||||
|
if data["provider"]:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
context__service_offering__provider=data["provider"]
|
||||||
|
)
|
||||||
|
if data["control_plane"]:
|
||||||
|
queryset = queryset.filter(context__control_plane=data["control_plane"])
|
||||||
|
if data["status"]:
|
||||||
|
if data["status"] == "active":
|
||||||
|
queryset = queryset.filter(is_deleted=False)
|
||||||
|
else:
|
||||||
|
queryset = queryset.filter(is_deleted=True)
|
||||||
|
return queryset
|
||||||
|
|
|
@ -5,41 +5,59 @@
|
||||||
{% translate "Instances" %}
|
{% translate "Instances" %}
|
||||||
{% endblock page_title %}
|
{% endblock page_title %}
|
||||||
{% endblock html_title %}
|
{% endblock html_title %}
|
||||||
{% block card_content %}
|
{% block content %}
|
||||||
<table class="table table-striped">
|
<section class="section">
|
||||||
<thead>
|
<div class="card">
|
||||||
<tr>
|
<div class="card-content">
|
||||||
<th>{% translate "Name" %}</th>
|
<div class="card-body">
|
||||||
<th>{% translate "Service" %}</th>
|
<form class="search-form" auto-submit>
|
||||||
<th>{% translate "Service Provider" %}</th>
|
{{ filter_form }}
|
||||||
<th>{% translate "Service Provider Zone" %}</th>
|
</form>
|
||||||
<th>{% translate "Created At" %}</th>
|
</div>
|
||||||
<th>{% translate "Status" %}</th>
|
</div>
|
||||||
</tr>
|
</div>
|
||||||
</thead>
|
<div class="card">
|
||||||
<tbody>
|
<div class="card-content">
|
||||||
{% for instance in instances %}
|
<div class="card-body">
|
||||||
<tr>
|
<table class="table table-striped">
|
||||||
<td>
|
<thead>
|
||||||
<a href="{{ instance.urls.base }}">{{ instance.name }}</a>
|
<tr>
|
||||||
</td>
|
<th>{% translate "Name" %}</th>
|
||||||
<td>{{ instance.context.service_definition.service.name }}</td>
|
<th>{% translate "Service" %}</th>
|
||||||
<td>{{ instance.context.service_offering.provider.name }}</td>
|
<th>{% translate "Service Provider" %}</th>
|
||||||
<td>{{ instance.context.control_plane.name }}</td>
|
<th>{% translate "Service Provider Zone" %}</th>
|
||||||
<td>{{ instance.created_at|date:"SHORT_DATETIME_FORMAT" }}</td>
|
<th>{% translate "Created At" %}</th>
|
||||||
<td>
|
<th>{% translate "Status" %}</th>
|
||||||
{% if instance.is_deleted %}
|
</tr>
|
||||||
<span class="badge text-bg-secondary">{% translate "Deleted" %}</span>
|
</thead>
|
||||||
{% else %}
|
<tbody>
|
||||||
<span class="badge text-bg-success">{% translate "Active" %}</span>
|
{% for instance in instances %}
|
||||||
{% endif %}
|
<tr>
|
||||||
</td>
|
<td>
|
||||||
</tr>
|
<a href="{{ instance.urls.base }}">{{ instance.name }}</a>
|
||||||
{% empty %}
|
</td>
|
||||||
<tr>
|
<td>{{ instance.context.service_definition.service.name }}</td>
|
||||||
<td colspan="6">{% translate "No service instances found." %}</td>
|
<td>{{ instance.context.service_offering.provider.name }}</td>
|
||||||
</tr>
|
<td>{{ instance.context.control_plane.name }}</td>
|
||||||
{% endfor %}
|
<td>{{ instance.created_at|date:"SHORT_DATETIME_FORMAT" }}</td>
|
||||||
</tbody>
|
<td>
|
||||||
</table>
|
{% if instance.is_deleted %}
|
||||||
|
<span class="badge text-bg-secondary">{% translate "Deleted" %}</span>
|
||||||
|
{% else %}
|
||||||
|
<span class="badge text-bg-success">{% translate "Active" %}</span>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="6">{% translate "No service instances found." %}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<script src="{% static "js/autosubmit.js" %}" defer></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -10,7 +10,11 @@ from servala.core.models import (
|
||||||
ServiceInstance,
|
ServiceInstance,
|
||||||
ServiceOffering,
|
ServiceOffering,
|
||||||
)
|
)
|
||||||
from servala.frontend.forms.service import ControlPlaneSelectForm, ServiceFilterForm
|
from servala.frontend.forms.service import (
|
||||||
|
ControlPlaneSelectForm,
|
||||||
|
ServiceFilterForm,
|
||||||
|
ServiceInstanceFilterForm,
|
||||||
|
)
|
||||||
from servala.frontend.views.mixins import HtmxViewMixin, OrganizationViewMixin
|
from servala.frontend.views.mixins import HtmxViewMixin, OrganizationViewMixin
|
||||||
|
|
||||||
|
|
||||||
|
@ -151,11 +155,21 @@ class ServiceInstanceListView(OrganizationViewMixin, ListView):
|
||||||
model = ServiceInstance
|
model = ServiceInstance
|
||||||
permission_type = "view"
|
permission_type = "view"
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def filter_form(self):
|
||||||
|
return ServiceInstanceFilterForm(data=self.request.GET or None)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Return all service instances for the current organization."""
|
"""Return all service instances for the current organization with filtering."""
|
||||||
return ServiceInstance.objects.filter(organization=self.request.organization)
|
queryset = ServiceInstance.objects.filter(
|
||||||
|
organization=self.request.organization
|
||||||
|
)
|
||||||
|
if self.filter_form.is_valid():
|
||||||
|
queryset = self.filter_form.filter_queryset(queryset)
|
||||||
|
return queryset
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context["organization"] = self.request.organization
|
context["organization"] = self.request.organization
|
||||||
|
context["filter_form"] = self.filter_form
|
||||||
return context
|
return context
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue