Skip to content

Commit

Permalink
Add question_count & quiz_count in User Django Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 8, 2022
1 parent 8e332c1 commit d408af6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class UserAdmin(UserAdmin):
"is_contributor",
"is_super_contributor",
"is_administrator",
"question_count",
"quiz_count",
"last_login",
"created",
)
Expand All @@ -49,6 +51,8 @@ class UserAdmin(UserAdmin):
"is_staff",
"is_superuser",
"last_login",
"question_count",
"quiz_count",
"created",
"updated",
]
Expand All @@ -65,13 +69,26 @@ class UserAdmin(UserAdmin):
)
},
),
(
"Stats",
{
"fields": (
"question_count",
"quiz_count",
)
},
),
("Dates", {"fields": ("last_login", "created", "updated")}),
)
add_fieldsets = (
(None, {"fields": ("first_name", "last_name", "email", "password1", "password2")}),
("Permissions", {"fields": ("roles",)}),
)

def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.prefetch_related("questions", "quizs")

def is_contributor(self, user) -> bool:
return "✅" if user.has_role_contributor else "❌"

Expand All @@ -87,5 +104,17 @@ def is_administrator(self, user) -> bool:

is_administrator.short_description = "Administrateur ?"

def question_count(self, user):
return user.question_count

question_count.short_description = "Nbr de questions"
question_count.admin_order_field = "question_count"

def quiz_count(self, user):
return user.quiz_count

quiz_count.short_description = "Nbr de quizs"
quiz_count.admin_order_field = "quiz_count"


admin_site.register(User, UserAdmin)
2 changes: 1 addition & 1 deletion users/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class ContributorTable(tables.Table):

class Meta:
model = User
fields = ["first_name", "last_name", "email", "question_count", "quiz_count", "roles", "last_login", "created"]
fields = ["first_name", "last_name", "email", "roles", "question_count", "quiz_count", "last_login", "created"]
template_name = DEFAULT_TEMPLATE
attrs = DEFAULT_ATTRS

0 comments on commit d408af6

Please sign in to comment.