Skip to content

Commit

Permalink
Sort community filter options alphabetically on package listing admin
Browse files Browse the repository at this point in the history
Custom filter was used rather than the ordering meta field of the
Community class since the latter might have some uninteded consequences
elsewhere.
  • Loading branch information
anttimaki committed Sep 9, 2024
1 parent f6683b2 commit 45f165e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions django/thunderstore/community/admin/package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -44,7 +58,7 @@ class PackageListingAdmin(admin.ModelAdmin):
"has_nsfw_content",
"is_review_requested",
"review_status",
"community",
CommunityFilter,
)
list_display = (
"id",
Expand Down

0 comments on commit 45f165e

Please sign in to comment.