From 74454336f2413f1b8523c939fe63915ae2955253 Mon Sep 17 00:00:00 2001 From: alexkiro <1538458+alexkiro@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:36:44 +0200 Subject: [PATCH] Fix getting progress when there are no strings (#829) Unlike COUNT, SUM will actually return NULL not 0 when there are no rows, so we need to coalesce to 0 to avoid such cases. This issued caused both the progress and status values to be incorrect for translations. Since `0 != None` an empty page will have its translations marked as "waiting for translations." --- wagtail_localize/models.py | 4 +++- .../tests/test_translation_model.py | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index af3528ce..f983a3d2 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -24,6 +24,7 @@ Value, When, ) +from django.db.models.functions import Coalesce from django.db.models.signals import post_delete, post_save from django.dispatch import receiver from django.urls import reverse @@ -1141,7 +1142,8 @@ def get_progress(self): output_field=IntegerField(), ) ).aggregate( - total_segments=Count("pk"), translated_segments=Sum("is_translated_i") + total_segments=Count("pk"), + translated_segments=Coalesce(Sum("is_translated_i"), 0), ) return aggs["total_segments"], aggs["translated_segments"] diff --git a/wagtail_localize/tests/test_translation_model.py b/wagtail_localize/tests/test_translation_model.py index 3fc1c4d6..14b7edcd 100644 --- a/wagtail_localize/tests/test_translation_model.py +++ b/wagtail_localize/tests/test_translation_model.py @@ -155,6 +155,18 @@ def test_get_progress_with_translations_in_different_context(self): progress = self.translation.get_progress() self.assertEqual(progress, (2, 0)) + def test_get_progress_with_no_strings(self): + # Check getting progress when there are no strings to be translated + page = create_test_page(title="Empty page", slug="empty-page") + source, created = TranslationSource.get_or_create_from_instance(page) + translation = Translation.objects.create( + source=source, + target_locale=self.fr_locale, + ) + + progress = translation.get_progress() + self.assertEqual(progress, (0, 0)) + class TestExportPO(TestCase): def setUp(self): @@ -577,6 +589,17 @@ def test_get_status_display_with_translations(self): self.assertEqual(self.translation.get_status_display(), "Up to date") + def test_get_status_with_no_strings(self): + # Check getting status when there are no strings to be translated + page = create_test_page(title="Empty page", slug="empty-page") + source, created = TranslationSource.get_or_create_from_instance(page) + translation = Translation.objects.create( + source=source, + target_locale=self.fr_locale, + ) + + self.assertEqual(translation.get_status_display(), "Up to date") + class TestSaveTarget(TestCase): def setUp(self):