Skip to content

Commit

Permalink
BB2-3232: Filter inactive accounts (#1208)
Browse files Browse the repository at this point in the history
* Add user account status filter

* Update param name
  • Loading branch information
jimmyfagan authored Jun 17, 2024
1 parent 91298bf commit f495561
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions apps/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
Expand All @@ -9,7 +10,6 @@
UserIdentificationLabel,
)


admin.site.register(ActivationKey)
admin.site.register(ValidPasswordResetKey)

Expand All @@ -30,8 +30,36 @@ def queryset(self, request, queryset):
return queryset


class UserAdmin(DjangoUserAdmin):
class ActiveAccountFilter(admin.SimpleListFilter):
title = "User activation status"
parameter_name = "status"

def lookups(self, request, model_admin):
return [
("active", "Active"),
("inactive_all", "Inactive"),
("inactive_expired", "Inactive (expired activation key)")
]

def queryset(self, request, queryset):
if self.value() == "inactive_expired":
return queryset.filter(
is_active=False,
activationkey__key_status="expired",
) | queryset.filter(
# Since the activation keys only reach "expired" status when they are
# used post-expiration, we need to check the "created" status as well
is_active=False,
activationkey__key_status="created",
activationkey__expires__lt=(datetime.today()),
)
elif self.value() == "inactive_all":
return queryset.filter(is_active=False)
elif self.value() == "active":
return queryset.filter(is_active=True)


class UserAdmin(DjangoUserAdmin):
list_display = (
"username",
"get_type",
Expand All @@ -43,7 +71,7 @@ class UserAdmin(DjangoUserAdmin):
"date_joined",
)

list_filter = (UserTypeFilter,)
list_filter = (UserTypeFilter, ActiveAccountFilter,)

@admin.display(
description="Type",
Expand Down

0 comments on commit f495561

Please sign in to comment.