From 6156cd858dc7e4dc2b3186ba97e1dd1e8572387c Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Tue, 18 Jan 2022 18:22:36 +0100 Subject: [PATCH 1/2] Add failing test --- .../tests/test_translation_model.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/wagtail_localize/tests/test_translation_model.py b/wagtail_localize/tests/test_translation_model.py index d4f462f1..bc04b5d2 100644 --- a/wagtail_localize/tests/test_translation_model.py +++ b/wagtail_localize/tests/test_translation_model.py @@ -413,6 +413,48 @@ def test_import_po_with_invalid_translation_id(self): # Should delete both the translations self.assertFalse(StringTranslation.objects.exists()) + def test_import_po_with_valid_string_clears_has_error_flag(self): + translation = StringTranslation.objects.create( + translation_of=String.objects.get(data="This is some test content"), + context=TranslationContext.objects.get(path="test_charfield"), + locale=self.fr_locale, + data=( + "This value is way too long for a char field so it should fail to publish and add an error to the translation. " + "This value is way too long for a char field so it should fail to publish and add an error to the translation. " + "This value is way too long for a char field so it should fail to publish and add an error to the translation." + ), + has_error=True, + field_error="Ensure this value has at most 255 characters (it has 329).", + ) + self.assertTrue(translation.has_error) + + po = polib.POFile(wrapwidth=200) + po.metadata = { + "POT-Creation-Date": str(timezone.now()), + "MIME-Version": "1.0", + "Content-Type": "text/plain; charset=utf-8", + "X-WagtailLocalize-TranslationID": str(self.translation.uuid), + } + + po.append( + polib.POEntry( + msgid="This is some test content", + msgctxt="test_charfield", + msgstr="Contenu de test", + ) + ) + + warnings = self.translation.import_po(po) + self.assertEqual(warnings, []) + + translation.refresh_from_db() + self.assertEqual( + translation.context, TranslationContext.objects.get(path="test_charfield") + ) + self.assertEqual(translation.locale, self.fr_locale) + self.assertEqual(translation.data, "Contenu de test") + self.assertFalse(translation.has_error) + def test_warnings(self): String.from_value( self.en_locale, From 466ab00d1018a1eef2ed294c1e2019e7adf68d04 Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Tue, 18 Jan 2022 18:22:55 +0100 Subject: [PATCH 2/2] Reset has_error flag on update during import_po --- wagtail_localize/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index 1c9a8a9d..6b0bf90f 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -1280,6 +1280,7 @@ def import_po( string_translation.tool_name = tool_name string_translation.last_translated_by = user string_translation.updated_at = timezone.now() + string_translation.has_error = False # reset the error flag. string_translation.save() except TranslationContext.DoesNotExist: