From 0cbdf3ed4708c937679a7c8a9313b2eaafa11086 Mon Sep 17 00:00:00 2001 From: Zaki Benaissa Date: Tue, 9 Jan 2024 19:22:13 +0100 Subject: [PATCH 1/2] Add count helper methods to User model --- accounts/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/accounts/models.py b/accounts/models.py index 1baa3fd..2b92825 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -81,6 +81,18 @@ class User(AbstractUser): full_name = self.first_name + " " + self.last_name return full_name + @classmethod + def get_student_count(cls): + return cls.objects.filter(is_student=True).count() + + @classmethod + def get_lecturer_count(cls): + return cls.objects.filter(is_lecturer=True).count() + + @classmethod + def get_superuser_count(cls): + return cls.objects.filter(is_superuser=True).count() + def __str__(self): return "{} ({})".format(self.username, self.get_full_name) From d6307aa077438dab2fbff17ec849629a88621635 Mon Sep 17 00:00:00 2001 From: Zaki Benaissa Date: Tue, 9 Jan 2024 19:22:43 +0100 Subject: [PATCH 2/2] Display actual user counts in the dashboards --- core/views.py | 8 +++++++- templates/core/dashboard.html | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/views.py b/core/views.py index 0ce1803..ea40adf 100644 --- a/core/views.py +++ b/core/views.py @@ -4,6 +4,7 @@ from django.contrib.auth.decorators import login_required from django.conf import settings from accounts.decorators import admin_required, lecturer_required +from accounts.models import User from .forms import SessionForm, SemesterForm, NewsAndEventsForm from .models import * @@ -297,4 +298,9 @@ def semester_delete_view(request, pk): @login_required @admin_required def dashboard_view(request): - return render(request, "core/dashboard.html") + context = { + "student_count": User.get_student_count(), + "lecturer_count": User.get_lecturer_count(), + "superuser_count": User.get_superuser_count(), + } + return render(request, "core/dashboard.html", context) diff --git a/templates/core/dashboard.html b/templates/core/dashboard.html index df4ad6c..da06ecb 100644 --- a/templates/core/dashboard.html +++ b/templates/core/dashboard.html @@ -174,7 +174,7 @@

Students -

12,040

+

{{ student_count }}

@@ -183,7 +183,7 @@

Lecturers -

1,350

+

{{ lecturer_count }}

@@ -201,7 +201,7 @@

Administrators -

125

+

{{ superuser_count }}