Skip to content

Commit

Permalink
Move TranslationSource revision publishing to `transaction.on_commi…
Browse files Browse the repository at this point in the history
…t` (#720)

Co-authored-by: Abdul Dridi <[email protected]>
  • Loading branch information
zerolab and Abdul-Dridi authored Oct 1, 2023
1 parent 7e75cbd commit f4e312b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion wagtail_localize/tests/test_edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import tempfile
import uuid

from unittest.mock import patch

import polib

from django.contrib.admin.utils import quote
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
39 changes: 31 additions & 8 deletions wagtail_localize/tests/test_submit_translations.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
)
Expand Down
6 changes: 4 additions & 2 deletions wagtail_localize/tests/test_translation_model.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f4e312b

Please sign in to comment.