Skip to content

Commit

Permalink
Conversation: update annotated count
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 25, 2023
1 parent e251258 commit 1a2a53f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 17 additions & 8 deletions lemarche/conversations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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",
Expand All @@ -77,7 +86,7 @@ class ConversationAdmin(admin.ModelAdmin):
"Contenu de la conversation",
{
"fields": (
"answer_count",
"answer_count_annotated",
"data_display",
)
},
Expand All @@ -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):
Expand All @@ -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"):
Expand Down
6 changes: 4 additions & 2 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1a2a53f

Please sign in to comment.