Skip to content

Commit

Permalink
Admin filter for anonymized users, defaulting to not anonymized
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilouf committed Dec 31, 2024
1 parent 0a63260 commit dd880d8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lemarche/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ def queryset(self, request, queryset):
return queryset


class IsAnonymizedFilter(admin.SimpleListFilter):
"""Custom admin filter to target users who are anonymized"""

title = "Est anonymisé"
parameter_name = "is_anonymized"

def lookups(self, request, model_admin):
return ("Yes", "Oui"), (None, "Non")

def queryset(self, request, queryset):
value = self.value()
if value == "Yes":
return queryset.filter(is_anonymized=True)
return queryset.filter(is_anonymized=False)

def choices(self, changelist):
"""Removed the first yield from the base method to only have 2 choices, defaulting too No"""
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == lookup,
"query_string": changelist.get_query_string({self.parameter_name: lookup}),
"display": title,
}


class UserNoteInline(GenericTabularInline):
model = Note
fields = ["text", "author", "created_at", "updated_at"]
Expand Down Expand Up @@ -181,6 +206,7 @@ class UserAdmin(FieldsetsInlineMixin, UserAdmin):
HasApiKeyFilter,
"is_staff",
"is_superuser",
IsAnonymizedFilter,
]
search_fields = ["id", "email", "first_name", "last_name"]
search_help_text = "Cherche sur les champs : ID, E-mail, Prénom, Nom"
Expand Down

0 comments on commit dd880d8

Please sign in to comment.