From a5576578ebc247cac1c6e439bc8b426677ce10ef Mon Sep 17 00:00:00 2001 From: ssorin Date: Thu, 19 Dec 2024 09:46:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(dashboard)=20adjust=20manager=20or?= =?UTF-8?q?der=20in=20Consent=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reordered `objects` and `active_objects` in the Consent model to ensure compatibility with Django admin expectations. Introduce a test to ensure the default Django manager is used in the admin setup, avoiding issues with custom managers. --- src/dashboard/apps/consent/models.py | 3 +- .../apps/consent/tests/test_admin.py | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/dashboard/apps/consent/tests/test_admin.py diff --git a/src/dashboard/apps/consent/models.py b/src/dashboard/apps/consent/models.py index 59053879..769ef110 100644 --- a/src/dashboard/apps/consent/models.py +++ b/src/dashboard/apps/consent/models.py @@ -46,8 +46,9 @@ class Consent(DashboardBase): end = models.DateTimeField(_("end date")) revoked_at = models.DateTimeField(_("revoked at"), null=True, blank=True) - active_objects = ConsentManager() + # models.Manager() must be in first place to ensure django admin expectations. objects = models.Manager() + active_objects = ConsentManager() class Meta: # noqa: D106 ordering = ["delivery_point"] diff --git a/src/dashboard/apps/consent/tests/test_admin.py b/src/dashboard/apps/consent/tests/test_admin.py new file mode 100644 index 00000000..19da282b --- /dev/null +++ b/src/dashboard/apps/consent/tests/test_admin.py @@ -0,0 +1,31 @@ +"""Dashboard consent admin tests.""" + +import pytest +from django.contrib.admin.sites import AdminSite +from django.urls import reverse + +from apps.consent.admin import ConsentAdmin +from apps.consent.models import Consent + + +@pytest.mark.django_db +def test_admin_manager_name(rf): + """Tests the name of the manager used by the Django admin to retrieve objects. + + For the Django admin, the manager used must be the default django manager + (`objects`) and not a custom manager, which could lead to an unwanted loss of + display of data. + """ + # Initialize admin + site = AdminSite() + admin = ConsentAdmin(Consent, site) + + # Request admin consent + request = rf.get(reverse("admin:qcd_consent_consent_changelist")) + + # Get the queryset used by admin + queryset = admin.get_queryset(request) + + # The manager name must be the default Django manager: `objects`. + manager_name = queryset.model._default_manager.name + assert manager_name == "objects"