From 4b163291ff0bd0feeeb5e68518a87cfc6242eda0 Mon Sep 17 00:00:00 2001 From: Abdul Dridi Date: Tue, 5 Sep 2023 10:30:34 +0100 Subject: [PATCH] Moved revision publishing in `TranslationSource` transaction.on_commit --- wagtail_localize/models.py | 2 +- .../tests/test_edit_translation.py | 8 +++- .../tests/test_submit_translations.py | 39 +++++++++++++++---- .../tests/test_translation_model.py | 6 ++- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index 4eef6416..b9eacfbe 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -798,7 +798,7 @@ def create_or_update_translation( self.sync_view_restrictions(original, translation) if publish: - page_revision.publish() + transaction.on_commit(page_revision.publish) else: # Note: we don't need to run full_clean for Pages as Wagtail does that in Page.save() diff --git a/wagtail_localize/tests/test_edit_translation.py b/wagtail_localize/tests/test_edit_translation.py index 30bcc449..0acaa410 100644 --- a/wagtail_localize/tests/test_edit_translation.py +++ b/wagtail_localize/tests/test_edit_translation.py @@ -2,6 +2,8 @@ import tempfile import uuid +from unittest.mock import patch + import polib from django.contrib.admin.utils import quote @@ -10,6 +12,7 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.messages import get_messages from django.core.files.uploadedfile import SimpleUploadedFile +from django.db import transaction from django.test import TestCase, override_settings from django.urls import reverse from django.utils import timezone @@ -149,7 +152,10 @@ def setUp(self): source=self.page_source, target_locale=self.fr_locale, ) - self.page_translation.save_target() + + with patch.object(transaction, "on_commit", side_effect=lambda func: func()): + self.page_translation.save_target() + self.fr_page = self.page.get_translation(self.fr_locale) self.fr_home_page = self.home_page.get_translation(self.fr_locale) diff --git a/wagtail_localize/tests/test_submit_translations.py b/wagtail_localize/tests/test_submit_translations.py index 8397f37a..7f14451b 100644 --- a/wagtail_localize/tests/test_submit_translations.py +++ b/wagtail_localize/tests/test_submit_translations.py @@ -1,7 +1,10 @@ +from unittest.mock import patch + from django.contrib.admin.utils import quote from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType +from django.db import transaction from django.test import TestCase, override_settings from django.urls import reverse from wagtail import VERSION as WAGTAIL_VERSION @@ -235,7 +238,8 @@ def test_get_submit_page_translation_on_root_page(self): self.assertEqual(response.status_code, 404) - def test_post_submit_page_translation(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation(self, _mock_on_commit): response = self.client.post( reverse( "wagtail_localize:submit_page_translation", @@ -276,7 +280,10 @@ def test_post_submit_page_translation_draft(self): translated_page = self.en_blog_index.get_translation(self.fr_locale) self.assertFalse(translated_page.live) - def test_post_submit_page_translation_submits_linked_snippets(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_submits_linked_snippets( + self, _mock_on_commit + ): self.en_blog_index.test_snippet = TestSnippet.objects.create( field="My test snippet" ) @@ -356,7 +363,10 @@ def test_post_submit_page_translation_including_subtree(self): # Check multiple translations were created self.assertEqual(Translation.objects.count(), 3) - def test_post_submit_page_translation_with_untranslated_parent(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_with_untranslated_parent( + self, _mock_on_commit + ): response = self.client.post( reverse( "wagtail_localize:submit_page_translation", @@ -387,7 +397,10 @@ def test_post_submit_page_translation_with_untranslated_parent(self): # Just check the translation was created under its parent self.assertEqual(translated_page.get_parent(), translated_parent_page.page_ptr) - def test_post_submit_page_translation_with_untranslated_grandparent(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_with_untranslated_grandparent( + self, _mock_on_commit + ): # This is the same as the previous test, except it's done with a new locale so the homepage doesn't exist yet. # This should create a translation request that contains the homepage, blog index and the blog post that was requested. es_locale = Locale.objects.create(language_code="es") @@ -459,7 +472,10 @@ def test_post_submit_page_translation_without_permissions(self): assert_permission_denied(self, response) - def test_post_submit_page_translation_reactivates_deleted_translation(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_reactivates_deleted_translation( + self, _mock_on_commit + ): # Create a disabled translation record # This simulates the case where the page was previously translated into that locale but later deleted source, created = TranslationSource.get_or_create_from_instance( @@ -494,8 +510,9 @@ def test_post_submit_page_translation_reactivates_deleted_translation(self): response, reverse("wagtailadmin_pages:edit", args=[translated_page.id]) ) + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) def test_post_submit_page_translation_doesnt_reactivate_deactivated_translation( - self, + self, _mock_on_commit ): # Like the previous test, this creates a disabled translation record, but this # time, the translation has not been deleted. It should not reactivate in this case @@ -531,7 +548,10 @@ def test_post_submit_page_translation_doesnt_reactivate_deactivated_translation( self.assertTrue(translated_page.live) @override_settings(WAGTAIL_LOCALIZE_DEFAULT_TRANSLATION_MODE="simple") - def test_post_submit_page_translation_with_global_disabled_mode(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_with_global_disabled_mode( + self, _mock_on_commit + ): response = self.client.post( reverse( "wagtail_localize:submit_page_translation", @@ -597,7 +617,10 @@ def test_post_submit_page_translation_with_global_mode_disabled_but_enabled_per_ translation = Translation.objects.get() self.assertTrue(translation.enabled) - def test_post_submit_page_translation_from_page_with_privacy_settings(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_post_submit_page_translation_from_page_with_privacy_settings( + self, _mock_on_commit + ): view_restriction = PageViewRestriction.objects.create( restriction_type="login", page=self.en_blog_index ) diff --git a/wagtail_localize/tests/test_translation_model.py b/wagtail_localize/tests/test_translation_model.py index 5bcc07a8..4d898069 100644 --- a/wagtail_localize/tests/test_translation_model.py +++ b/wagtail_localize/tests/test_translation_model.py @@ -1,9 +1,10 @@ from unittest import mock +from unittest.mock import patch import polib from django.conf import settings -from django.db import OperationalError +from django.db import OperationalError, transaction from django.db.migrations.recorder import MigrationRecorder from django.test import TestCase, override_settings from django.utils import timezone @@ -602,7 +603,8 @@ def setUp(self): self.test_content_string = String.objects.get(data="Test content") self.more_test_content_string = String.objects.get(data="More test content") - def test_save_target(self): + @patch.object(transaction, "on_commit", side_effect=lambda func: func()) + def test_save_target(self, _mock_on_commit): self.translation.save_target() # Should create the page with English content