Skip to content

Commit 8d99e16

Browse files
committed
ERU filters to Admin page
1 parent 1c75b84 commit 8d99e16

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

deployments/admin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ class MolnixTagGroupAdmin(CompareVersionAdmin, admin.ModelAdmin):
5353
search_fields = ("name",)
5454

5555

56+
class ERUOwnerCountryFilter(admin.SimpleListFilter):
57+
title = _("ERU owner country")
58+
parameter_name = "owner_country"
59+
60+
def lookups(self, request, model_admin):
61+
# Show each country only once (distinct), based on ERUs present
62+
qs = (
63+
model_admin.get_queryset(request)
64+
.exclude(eru_owner__national_society_country__isnull=True)
65+
.values_list(
66+
"eru_owner__national_society_country_id",
67+
"eru_owner__national_society_country__name",
68+
)
69+
.distinct()
70+
.order_by("eru_owner__national_society_country__name")
71+
)
72+
return [(str(cid), name) for cid, name in qs]
73+
74+
def queryset(self, request, queryset):
75+
cid = self.value()
76+
if cid:
77+
return queryset.filter(eru_owner__national_society_country_id=cid)
78+
return queryset
79+
80+
5681
@admin.register(models.ERU)
5782
class ERUAdmin(CompareVersionAdmin, admin.ModelAdmin):
5883
search_fields = (
@@ -63,6 +88,8 @@ class ERUAdmin(CompareVersionAdmin, admin.ModelAdmin):
6388
"eru_owner__national_society_country__name",
6489
)
6590
list_display = ["__str__", "country", "event", "eru_owner", "appeal", "start_date", "units", "eqp_units"]
91+
# Type and country-based owner filter (single entry per country) filter
92+
list_filter = ("type", ERUOwnerCountryFilter)
6693

6794
@admin.display(description=_("Country"), ordering="deployed_to__name")
6895
def country(self, obj):

0 commit comments

Comments
 (0)