From 45f165e2ced600f6ffe7bd84e3db19b0b4e17b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Mon, 9 Sep 2024 15:49:36 +0300 Subject: [PATCH] Sort community filter options alphabetically on package listing admin Custom filter was used rather than the ordering meta field of the Community class since the latter might have some uninteded consequences elsewhere. --- .../community/admin/package_listing.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/django/thunderstore/community/admin/package_listing.py b/django/thunderstore/community/admin/package_listing.py index 07bdf3651..9ca803af7 100644 --- a/django/thunderstore/community/admin/package_listing.py +++ b/django/thunderstore/community/admin/package_listing.py @@ -3,11 +3,13 @@ from django.contrib import admin from django.db import transaction -from django.db.models import QuerySet +from django.db.models import Q, QuerySet +from django.http import HttpRequest from django.utils.safestring import mark_safe from ..consts import PackageListingReviewStatus from ..forms import PackageListingAdminForm +from ..models.community import Community from ..models.package_listing import PackageListing @@ -31,6 +33,18 @@ def approve_listing(modeladmin, request, queryset: QuerySet[PackageListing]): approve_listing.short_description = "Approve" +class CommunityFilter(admin.SimpleListFilter): + title = "Community" + parameter_name = "community" + + def lookups(self, request: HttpRequest, model_admin): + return Community.objects.order_by("name").values_list("identifier", "name") + + def queryset(self, request: HttpRequest, queryset: QuerySet[PackageListing]): + if self.value(): + return queryset.exclude(~Q(community__identifier=self.value())) + + @admin.register(PackageListing) class PackageListingAdmin(admin.ModelAdmin): form = PackageListingAdminForm @@ -44,7 +58,7 @@ class PackageListingAdmin(admin.ModelAdmin): "has_nsfw_content", "is_review_requested", "review_status", - "community", + CommunityFilter, ) list_display = ( "id",