From ae058732faddfc08a6548a9031e58e2ef6a46862 Mon Sep 17 00:00:00 2001 From: Andrei Satsevich Date: Fri, 22 Dec 2023 17:15:05 +0300 Subject: [PATCH 1/4] Prevent translation object duplicates --- wagtail_localize/operations.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wagtail_localize/operations.py b/wagtail_localize/operations.py index edbf0804..1df4ac2f 100644 --- a/wagtail_localize/operations.py +++ b/wagtail_localize/operations.py @@ -50,7 +50,7 @@ def create_translations(self, instance, include_related_objects=True): ) # Support disabling the out of the box translation mode. - # The value set on the model takes precendence over the global setting. + # The value set on the model takes precedence over the global setting. if hasattr(instance, "localize_default_translation_mode"): translation_mode = instance.localize_default_translation_mode else: @@ -61,9 +61,13 @@ def create_translations(self, instance, include_related_objects=True): # Set up translation records for target_locale in self.target_locales: + # Skip if target_locale is the same as source locale + if target_locale == source.locale: + continue + # Create translation if it doesn't exist yet, re-enable if translation was disabled # Note that the form won't show this locale as an option if the translation existed - # in this langauge, so this shouldn't overwrite any unmanaged translations. + # in this language, so this shouldn't overwrite any unmanaged translations. translation, created = Translation.objects.update_or_create( source=source, target_locale=target_locale, From 79944062981062649081a9f5389545927e1ea957 Mon Sep 17 00:00:00 2001 From: Andrei Satsevich Date: Sat, 23 Dec 2023 16:23:10 +0300 Subject: [PATCH 2/4] add operations test --- wagtail_localize/tests/test_operations.py | 96 +++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 wagtail_localize/tests/test_operations.py diff --git a/wagtail_localize/tests/test_operations.py b/wagtail_localize/tests/test_operations.py new file mode 100644 index 00000000..d7f124c0 --- /dev/null +++ b/wagtail_localize/tests/test_operations.py @@ -0,0 +1,96 @@ +from django.contrib.auth import get_user_model +from django.contrib.contenttypes.models import ContentType +from django.test import TestCase +from wagtail.models import Locale, Page + +from wagtail_localize.models import Translation, TranslationSource +from wagtail_localize.operations import TranslationCreator +from wagtail_localize.segments import RelatedObjectSegmentValue +from wagtail_localize.test.models import TestPage + + +def create_test_page(**kwargs): + parent = kwargs.pop("parent", None) or Page.objects.get(id=1) + page = parent.add_child(instance=TestPage(**kwargs)) + page_revision = page.save_revision() + page_revision.publish() + page.refresh_from_db() + + source, created = TranslationSource.get_or_create_from_instance(page) + prepare_source(source) + + return page + + +def prepare_source(source): + # Recurse into any related objects + for segment in source.relatedobjectsegment_set.all(): + if isinstance(segment, RelatedObjectSegmentValue): + related_source, created = TranslationSource.get_or_create_from_instance( + segment.get_instance(source.locale) + ) + prepare_source(related_source) + + +class TranslationOperationsTest(TestCase): + def setUp(self): + # Create a Belarusian locale for testing + self.be_locale = Locale.objects.create(language_code="be") + + # Create a test page + self.page = create_test_page( + title="Test page", + slug="test-page", + test_charfield="This is some test content", + ) + + # Create a user + self.user = get_user_model().objects.create(username="testuser") + + # Instantiate TranslationCreator with the default and Belarusian locales for target_locales + self.target_locales = [self.page.locale, self.be_locale] + self.translation_creator = TranslationCreator( + user=self.user, target_locales=self.target_locales + ) + + # Check if TranslationSource already exists + source, created = TranslationSource.objects.get_or_create( + object_id=self.page.translation_key, + specific_content_type=ContentType.objects.get_for_model(TestPage), + locale=self.page.locale, + ) + + # If not created, prepare the source + if created: + prepare_source(source) + + def test_create_translations_skips_duplicate(self): + # Call create_translations() to check that only one translation has been created + self.translation_creator.create_translations(self.page) + + # Assert statements for better readability + self.assertEqual( + TranslationSource.objects.filter( + object_id=self.page.translation_key + ).count(), + 1, + "Only one TranslationSource should be created for the source page", + ) + + self.assertEqual( + Translation.objects.filter( + source__object_id=self.page.translation_key, + target_locale=self.be_locale, + ).count(), + 1, + "Only one Translation object should be created for the Belarusian locale", + ) + + self.assertEqual( + Translation.objects.filter( + source__object_id=self.page.translation_key, + target_locale=self.page.locale, + ).count(), + 0, + "No Translation object should be created for the default locale", + ) From 5448b91c16b0a5eda440ba085e0c6655e2795ebf Mon Sep 17 00:00:00 2001 From: Andrei Satsevich Date: Tue, 26 Dec 2023 16:01:21 +0300 Subject: [PATCH 3/4] Remove check if TranslationSource already exists --- wagtail_localize/tests/test_operations.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/wagtail_localize/tests/test_operations.py b/wagtail_localize/tests/test_operations.py index d7f124c0..a7084f2c 100644 --- a/wagtail_localize/tests/test_operations.py +++ b/wagtail_localize/tests/test_operations.py @@ -53,17 +53,6 @@ def setUp(self): user=self.user, target_locales=self.target_locales ) - # Check if TranslationSource already exists - source, created = TranslationSource.objects.get_or_create( - object_id=self.page.translation_key, - specific_content_type=ContentType.objects.get_for_model(TestPage), - locale=self.page.locale, - ) - - # If not created, prepare the source - if created: - prepare_source(source) - def test_create_translations_skips_duplicate(self): # Call create_translations() to check that only one translation has been created self.translation_creator.create_translations(self.page) From 635cfedb0a9bdd1f9313f149dd3166497edc2699 Mon Sep 17 00:00:00 2001 From: Andrei Satsevich <63553768+ACK1D@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:56:45 +0300 Subject: [PATCH 4/4] Update wagtail_localize/tests/test_operations.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dan BraghiÈ™ --- wagtail_localize/tests/test_operations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/wagtail_localize/tests/test_operations.py b/wagtail_localize/tests/test_operations.py index a7084f2c..3e786411 100644 --- a/wagtail_localize/tests/test_operations.py +++ b/wagtail_localize/tests/test_operations.py @@ -1,5 +1,4 @@ from django.contrib.auth import get_user_model -from django.contrib.contenttypes.models import ContentType from django.test import TestCase from wagtail.models import Locale, Page