Skip to content

Commit

Permalink
User: update annotated count
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 25, 2023
1 parent 1a2a53f commit dc64bda
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
26 changes: 13 additions & 13 deletions lemarche/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
),
},
Expand Down Expand Up @@ -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'<a href="{url}">{getattr(user, "siae_count", 0)}</a>')
return format_html(f'<a href="{url}">{getattr(user, "siae_count_annotated", 0)}</a>')

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'<a href="{url}">{getattr(user, "tender_count", 0)}</a>')
return format_html(f'<a href="{url}">{getattr(user, "tender_count_annotated", 0)}</a>')

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}"
Expand Down
4 changes: 2 additions & 2 deletions lemarche/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 13 additions & 11 deletions lemarche/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit dc64bda

Please sign in to comment.