natural sorting of compute plans in admin
This commit is contained in:
parent
227feb3a55
commit
b32a19ffa2
1 changed files with 21 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
||||||
Admin classes for pricing models including compute plans, storage plans, and VSHN AppCat pricing
|
Admin classes for pricing models including compute plans, storage plans, and VSHN AppCat pricing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from adminsortable2.admin import SortableAdminMixin
|
from adminsortable2.admin import SortableAdminMixin
|
||||||
|
@ -27,6 +28,12 @@ from ..models import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def natural_sort_key(obj):
|
||||||
|
"""Extract numeric parts for natural sorting"""
|
||||||
|
parts = re.split(r"(\d+)", obj.name)
|
||||||
|
return [int(part) if part.isdigit() else part for part in parts]
|
||||||
|
|
||||||
|
|
||||||
class ComputePlanPriceInline(admin.TabularInline):
|
class ComputePlanPriceInline(admin.TabularInline):
|
||||||
"""Inline admin for ComputePlanPrice model"""
|
"""Inline admin for ComputePlanPrice model"""
|
||||||
|
|
||||||
|
@ -117,7 +124,7 @@ class ComputePlanResource(resources.ModelResource):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(ComputePlan)
|
@admin.register(ComputePlan)
|
||||||
class ComputePlansAdmin(ImportExportModelAdmin):
|
class ComputePlanAdmin(ImportExportModelAdmin):
|
||||||
"""Admin configuration for ComputePlan model with import/export functionality"""
|
"""Admin configuration for ComputePlan model with import/export functionality"""
|
||||||
|
|
||||||
resource_class = ComputePlanResource
|
resource_class = ComputePlanResource
|
||||||
|
@ -133,9 +140,21 @@ class ComputePlansAdmin(ImportExportModelAdmin):
|
||||||
)
|
)
|
||||||
search_fields = ("name", "cloud_provider__name", "group__name")
|
search_fields = ("name", "cloud_provider__name", "group__name")
|
||||||
list_filter = ("active", "cloud_provider", "group")
|
list_filter = ("active", "cloud_provider", "group")
|
||||||
ordering = ("name",)
|
|
||||||
inlines = [ComputePlanPriceInline]
|
inlines = [ComputePlanPriceInline]
|
||||||
|
|
||||||
|
def changelist_view(self, request, extra_context=None):
|
||||||
|
"""Override changelist view to apply natural sorting"""
|
||||||
|
# Get the response from parent
|
||||||
|
response = super().changelist_view(request, extra_context)
|
||||||
|
|
||||||
|
# If it's a TemplateResponse, we can modify the context
|
||||||
|
if hasattr(response, "context_data") and "cl" in response.context_data:
|
||||||
|
cl = response.context_data["cl"]
|
||||||
|
if hasattr(cl, "result_list"):
|
||||||
|
cl.result_list = sorted(cl.result_list, key=natural_sort_key)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
def display_prices(self, obj):
|
def display_prices(self, obj):
|
||||||
"""Display formatted prices for the list view"""
|
"""Display formatted prices for the list view"""
|
||||||
prices = obj.prices.all()
|
prices = obj.prices.all()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue