From dc64bda46d3977a72d9364696c1d4fde36db9400 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Fri, 20 Oct 2023 11:56:52 +0200 Subject: [PATCH] User: update annotated count --- lemarche/users/admin.py | 26 +++++++++++++------------- lemarche/users/models.py | 4 ++-- lemarche/users/tests.py | 24 +++++++++++++----------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lemarche/users/admin.py b/lemarche/users/admin.py index 5fe578f59..61a3801af 100644 --- a/lemarche/users/admin.py +++ b/lemarche/users/admin.py @@ -159,8 +159,8 @@ class UserAdmin(FieldsetsInlineMixin, UserAdmin): "last_name", "kind", "company_name", - "siae_count_with_link", - "tender_count_with_link", + "siae_count_annotated_with_link", + "tender_count_annotated_with_link", "last_login", "created_at", ] @@ -188,8 +188,8 @@ class UserAdmin(FieldsetsInlineMixin, UserAdmin): + [f"{field}_last_updated" for field in User.TRACK_UPDATE_FIELDS] + [field.name for field in User._meta.fields if field.name.endswith("_last_seen_date")] + [ - "siae_count_with_link", - "tender_count_with_link", + "siae_count_annotated_with_link", + "tender_count_annotated_with_link", "favorite_list_count_with_link", "last_login", "image_url", @@ -234,7 +234,7 @@ class UserAdmin(FieldsetsInlineMixin, UserAdmin): "Dépôt de besoin", { "fields": ( - "tender_count_with_link", + "tender_count_annotated_with_link", "can_display_tender_contact_details", ), }, @@ -330,19 +330,19 @@ def save_formset(self, request, form, formset, change): form.instance.author = request.user super().save_formset(request, form, formset, change) - def siae_count_with_link(self, user): + def siae_count_annotated_with_link(self, user): url = reverse("admin:siaes_siae_changelist") + f"?users__in={user.id}" - return format_html(f'{getattr(user, "siae_count", 0)}') + return format_html(f'{getattr(user, "siae_count_annotated", 0)}') - siae_count_with_link.short_description = "Nombre de structures" - siae_count_with_link.admin_order_field = "siae_count" + siae_count_annotated_with_link.short_description = "Nombre de structures" + siae_count_annotated_with_link.admin_order_field = "siae_count_annotated" - def tender_count_with_link(self, user): + def tender_count_annotated_with_link(self, user): url = reverse("admin:tenders_tender_changelist") + f"?author__id__exact={user.id}" - return format_html(f'{getattr(user, "tender_count", 0)}') + return format_html(f'{getattr(user, "tender_count_annotated", 0)}') - tender_count_with_link.short_description = "Nombre de besoins déposés" - tender_count_with_link.admin_order_field = "tender_count" + tender_count_annotated_with_link.short_description = "Nombre de besoins déposés" + tender_count_annotated_with_link.admin_order_field = "tender_count_annotated" def favorite_list_count_with_link(self, user): url = reverse("admin:favorites_favoritelist_changelist") + f"?users__in={user.id}" diff --git a/lemarche/users/models.py b/lemarche/users/models.py index ce623b56b..6cd25c44e 100644 --- a/lemarche/users/models.py +++ b/lemarche/users/models.py @@ -39,10 +39,10 @@ def has_api_key(self): return self.filter(api_key__isnull=False) def with_siae_stats(self): - return self.prefetch_related("siaes").annotate(siae_count=Count("siaes", distinct=True)) + return self.prefetch_related("siaes").annotate(siae_count_annotated=Count("siaes", distinct=True)) def with_tender_stats(self): - return self.prefetch_related("tenders").annotate(tender_count=Count("tenders", distinct=True)) + return self.prefetch_related("tenders").annotate(tender_count_annotated=Count("tenders", distinct=True)) class UserManager(BaseUserManager): diff --git a/lemarche/users/tests.py b/lemarche/users/tests.py index cad7fcefb..17fabefe1 100644 --- a/lemarche/users/tests.py +++ b/lemarche/users/tests.py @@ -49,50 +49,52 @@ class UserModelQuerysetTest(TestCase): def setUpTestData(cls): cls.user = UserFactory() - def test_has_siae_queryset(self): + def test_has_siae(self): user_2 = UserFactory() siae = SiaeFactory() siae.users.add(user_2) self.assertEqual(User.objects.count(), 1 + 1) self.assertEqual(User.objects.has_siae().count(), 1) - def test_has_tender_queryset(self): + def test_has_tender(self): user_2 = UserFactory() TenderFactory(author=user_2) self.assertEqual(User.objects.count(), 1 + 1) self.assertEqual(User.objects.has_tender().count(), 1) - def test_has_favorite_list_queryset(self): + def test_has_favorite_list(self): user_2 = UserFactory() FavoriteListFactory(user=user_2) self.assertEqual(User.objects.count(), 1 + 1) self.assertEqual(User.objects.has_favorite_list().count(), 1) - def test_has_api_key_queryset(self): + def test_has_api_key(self): UserFactory(api_key="coucou") self.assertEqual(User.objects.count(), 1 + 1) self.assertEqual(User.objects.has_api_key().count(), 1) - def test_with_siae_stats_queryset(self): + def test_with_siae_stats(self): user_2 = UserFactory() siae = SiaeFactory() siae.users.add(user_2) self.assertEqual(User.objects.count(), 1 + 1) - self.assertEqual(User.objects.with_siae_stats().filter(id=self.user.id).first().siae_count, 0) - self.assertEqual(User.objects.with_siae_stats().filter(id=user_2.id).first().siae_count, 1) + self.assertEqual(User.objects.with_siae_stats().filter(id=self.user.id).first().siae_count_annotated, 0) + self.assertEqual(User.objects.with_siae_stats().filter(id=user_2.id).first().siae_count_annotated, 1) - def test_with_tender_stats_queryset(self): + def test_with_tender_stats(self): user_2 = UserFactory() TenderFactory(author=user_2) self.assertEqual(User.objects.count(), 1 + 1) - self.assertEqual(User.objects.with_tender_stats().filter(id=self.user.id).first().tender_count, 0) - self.assertEqual(User.objects.with_tender_stats().filter(id=user_2.id).first().tender_count, 1) + self.assertEqual(User.objects.with_tender_stats().filter(id=self.user.id).first().tender_count_annotated, 0) + self.assertEqual(User.objects.with_tender_stats().filter(id=user_2.id).first().tender_count_annotated, 1) def test_chain_querysets(self): user_2 = UserFactory(api_key="chain") siae = SiaeFactory() siae.users.add(user_2) - self.assertEqual(User.objects.has_api_key().with_siae_stats().filter(id=user_2.id).first().siae_count, 1) + self.assertEqual( + User.objects.has_api_key().with_siae_stats().filter(id=user_2.id).first().siae_count_annotated, 1 + ) class UserModelSaveTest(TestCase):