diff --git a/lemarche/conversations/admin.py b/lemarche/conversations/admin.py index 7af678622..b9f2fab36 100644 --- a/lemarche/conversations/admin.py +++ b/lemarche/conversations/admin.py @@ -45,7 +45,16 @@ def queryset(self, request, queryset): @admin.register(Conversation, site=admin_site) class ConversationAdmin(admin.ModelAdmin): - list_display = ["id", "uuid", "sender_encoded", "is_validate", "title", "kind", "answer_count", "created_at"] + list_display = [ + "id", + "uuid", + "sender_encoded", + "is_validate", + "title", + "kind", + "answer_count_annotated", + "created_at", + ] list_filter = ["kind", HasAnswerFilter, IsValidatedFilter] search_fields = ["id", "uuid", "sender_email"] search_help_text = "Cherche sur les champs : ID, UUID, Initiateur (E-mail)" @@ -61,7 +70,7 @@ class ConversationAdmin(admin.ModelAdmin): "sender_first_name", "sender_last_name", "sender_email", - "answer_count", + "answer_count_annotated", "data_display", "created_at", "updated_at", @@ -77,7 +86,7 @@ class ConversationAdmin(admin.ModelAdmin): "Contenu de la conversation", { "fields": ( - "answer_count", + "answer_count_annotated", "data_display", ) }, @@ -92,7 +101,7 @@ class Media: def get_queryset(self, request): qs = super().get_queryset(request) - qs = qs.with_answer_count() + qs = qs.with_answer_stats() return qs def is_validate(self, conversation: Conversation): @@ -101,11 +110,11 @@ def is_validate(self, conversation: Conversation): is_validate.boolean = True is_validate.short_description = "Validé" - def answer_count(self, conversation): - return getattr(conversation, "answer_count", 0) + def answer_count_annotated(self, conversation): + return getattr(conversation, "answer_count_annotated", 0) - answer_count.short_description = "Nombre de réponses" - answer_count.admin_order_field = "answer_count" + answer_count_annotated.short_description = "Nombre de réponses" + answer_count_annotated.admin_order_field = "answer_count_annotated" def response_change(self, request, obj: Conversation): if request.POST.get("_validate_conversation"): diff --git a/lemarche/conversations/models.py b/lemarche/conversations/models.py index 2ad83f44d..4e6786499 100644 --- a/lemarche/conversations/models.py +++ b/lemarche/conversations/models.py @@ -13,8 +13,10 @@ class ConversationQuerySet(models.QuerySet): def has_answer(self): return self.exclude(data=[]) - def with_answer_count(self): - return self.annotate(answer_count=Func("data", function="jsonb_array_length", output_field=IntegerField())) + def with_answer_stats(self): + return self.annotate( + answer_count_annotated=Func("data", function="jsonb_array_length", output_field=IntegerField()) + ) def get_conv_from_uuid(self, conv_uuid: str, version=1): """get conv form diff --git a/lemarche/conversations/tests.py b/lemarche/conversations/tests.py index 8b372ae3c..81e232291 100644 --- a/lemarche/conversations/tests.py +++ b/lemarche/conversations/tests.py @@ -60,7 +60,7 @@ def setUpTestData(cls): def test_has_answer(self): self.assertEqual(Conversation.objects.has_answer().count(), 1) - def test_with_answer_count(self): - conversation_queryset = Conversation.objects.with_answer_count() - self.assertEqual(conversation_queryset.get(id=self.conversation.id).answer_count, 0) - self.assertEqual(conversation_queryset.get(id=self.conversation_with_answer.id).answer_count, 1) + def test_with_answer_stats(self): + conversation_queryset = Conversation.objects.with_answer_stats() + self.assertEqual(conversation_queryset.get(id=self.conversation.id).answer_count_annotated, 0) + self.assertEqual(conversation_queryset.get(id=self.conversation_with_answer.id).answer_count_annotated, 1)