natural sorting of compute plans in admin

This commit is contained in:
Tobias Brunner 2025-06-04 11:36:45 +02:00
parent 227feb3a55
commit b32a19ffa2
No known key found for this signature in database

View file

@ -2,6 +2,7 @@
Admin classes for pricing models including compute plans, storage plans, and VSHN AppCat pricing
"""
import re
from django.contrib import admin
from django.utils.html import format_html
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):
"""Inline admin for ComputePlanPrice model"""
@ -117,7 +124,7 @@ class ComputePlanResource(resources.ModelResource):
@admin.register(ComputePlan)
class ComputePlansAdmin(ImportExportModelAdmin):
class ComputePlanAdmin(ImportExportModelAdmin):
"""Admin configuration for ComputePlan model with import/export functionality"""
resource_class = ComputePlanResource
@ -133,9 +140,21 @@ class ComputePlansAdmin(ImportExportModelAdmin):
)
search_fields = ("name", "cloud_provider__name", "group__name")
list_filter = ("active", "cloud_provider", "group")
ordering = ("name",)
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):
"""Display formatted prices for the list view"""
prices = obj.prices.all()