Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,7 @@ def create_or_update_translation(
if publish:
transaction.on_commit(new_revision.publish)

# Note: DraftStateMixin requires RevisionMixin, so RevisionMixin is checked here for typing
elif isinstance(translation, RevisionMixin):
elif isinstance(translation, DraftStateMixin):
# We copied another instance which may be live, so we make sure this one matches the desired state
translation.live = publish
translation.save()
Expand All @@ -815,6 +814,13 @@ def create_or_update_translation(
if publish:
transaction.on_commit(new_revision.publish)

elif isinstance(translation, RevisionMixin):
translation.save()

# Create a new revision of the Snippet
# - Models which are not subclasses of DraftStateMixin are not publishable
new_revision = translation.save_revision(user=user)

else:
# Note: we don't need to run full_clean for DraftStateMixin objects or Pages as Wagtail does that in RevisionMixin.save_revision()
translation.full_clean()
Expand Down
48 changes: 46 additions & 2 deletions wagtail_localize/test/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.18 on 2025-01-31 15:00
# Generated by Django 4.2.19 on 2025-03-03 09:38

import uuid

Expand All @@ -23,8 +23,8 @@ class Migration(migrations.Migration):
dependencies = [
("wagtailcore", "0089_log_entry_data_json_null_to_object"),
("wagtailimages", "0025_alter_image_file_alter_rendition_file"),
("wagtaildocs", "0012_uploadeddocument"),
("wagtail_localize", "0016_rename_page_revision_translationlog_revision"),
("wagtaildocs", "0012_uploadeddocument"),
]

operations = [
Expand Down Expand Up @@ -1933,6 +1933,50 @@ class Migration(migrations.Migration):
"unique_together": {("translation_key", "locale")},
},
),
migrations.CreateModel(
name="TestRevisionsButNoDraftModel",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"translation_key",
models.UUIDField(default=uuid.uuid4, editable=False),
),
("field", models.CharField(blank=True, max_length=20)),
(
"latest_revision",
models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="wagtailcore.revision",
verbose_name="latest revision",
),
),
(
"locale",
models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.PROTECT,
related_name="+",
to="wagtailcore.locale",
),
),
],
options={
"abstract": False,
"unique_together": {("translation_key", "locale")},
},
),
migrations.CreateModel(
name="TestParentalSnippet",
fields=[
Expand Down
12 changes: 12 additions & 0 deletions wagtail_localize/test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ class TestNoDraftModel(TranslatableMixin):
]


class TestRevisionsButNoDraftModel(TranslatableMixin, RevisionMixin):
field = models.CharField(max_length=20, blank=True)

translatable_fields = [
TranslatableField("field"),
]

panels = [
FieldPanel("field"),
]


class TestSnippetOrderable(TranslatableMixin, Orderable):
parent = ParentalKey(
"TestSnippet",
Expand Down
22 changes: 22 additions & 0 deletions wagtail_localize/tests/test_translation_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TestNoDraftModel,
TestPage,
TestParentalSnippet,
TestRevisionsButNoDraftModel,
TestSnippet,
TestUUIDModel,
TestUUIDSnippet,
Expand Down Expand Up @@ -708,6 +709,27 @@ def test_save_target_snippet(self):
translated_snippet = snippet.get_translation(self.fr_locale)
self.assertEqual(translated_snippet.field, "Contenu de test")

def test_save_target_with_revisions_but_not_draft(self):
snippet = TestRevisionsButNoDraftModel.objects.create(field="Test content")
source, created = TranslationSource.get_or_create_from_instance(snippet)
translation = Translation.objects.create(
source=source,
target_locale=self.fr_locale,
)

field_context = TranslationContext.objects.get(path="field")
StringTranslation.objects.create(
translation_of=self.test_content_string,
context=field_context,
locale=self.fr_locale,
data="Contenu de test",
)

translation.save_target()

translated_snippet = snippet.get_translation(self.fr_locale)
self.assertEqual(translated_snippet.field, "Contenu de test")

def test_save_target_cant_save_non_draftable_as_draft(self):
snippet = TestNoDraftModel.objects.create(field="Test content")
source, _ = TranslationSource.get_or_create_from_instance(snippet)
Expand Down
174 changes: 173 additions & 1 deletion wagtail_localize/tests/test_translationsource_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from django.test import TestCase, TransactionTestCase, override_settings
from django.utils import timezone
from wagtail.blocks import StreamValue
from wagtail.models import Locale, Page, PageLogEntry
Expand All @@ -22,6 +22,7 @@
TestChildObject,
TestNonParentalChildObject,
TestPage,
TestRevisionsButNoDraftModel,
TestSnippet,
TestSynchronizedChildObject,
)
Expand Down Expand Up @@ -179,6 +180,118 @@ def test_update(self):
)


class TestRevisionsButNoDraftModelGetOrCreateInstance(TestCase):
def setUp(self):
self.snippet = TestRevisionsButNoDraftModel.objects.create(
field="This is some test content"
)

def test_create(self):
source, created = TranslationSource.get_or_create_from_instance(self.snippet)

self.assertTrue(created)
self.assertEqual(source.object_id, self.snippet.translation_key)
self.assertEqual(source.locale, self.snippet.locale)
self.assertEqual(
json.loads(source.content_json),
{
"pk": self.snippet.pk,
"latest_revision": None,
"field": "This is some test content",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
},
)
self.assertTrue(source.created_at)

def test_update(self):
source = TranslationSource.objects.create(
object_id=self.snippet.translation_key,
specific_content_type=ContentType.objects.get_for_model(
TestRevisionsButNoDraftModel
),
locale=self.snippet.locale,
content_json=json.dumps(
{
"pk": self.snippet.pk,
"field": "Some different content", # Changed
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
},
),
last_updated_at=timezone.now(),
)

new_source, created = TranslationSource.get_or_create_from_instance(
self.snippet
)

self.assertEqual(source, new_source)
self.assertEqual(
json.loads(source.content_json)["field"], "Some different content"
)
self.assertEqual(
json.loads(new_source.content_json)["field"], "Some different content"
)


class TestRevisionsButNoDraftModelUpdateOrCreateFromInstance(TestCase):
def setUp(self):
self.snippet = TestRevisionsButNoDraftModel.objects.create(
field="This is some test content"
)

def test_create(self):
source, created = TranslationSource.update_or_create_from_instance(self.snippet)

self.assertTrue(created)
self.assertEqual(source.object_id, self.snippet.translation_key)
self.assertEqual(source.locale, self.snippet.locale)
self.assertEqual(
json.loads(source.content_json),
{
"pk": self.snippet.pk,
"latest_revision": None,
"field": "This is some test content",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
},
)
self.assertTrue(source.created_at)

def test_update(self):
source = TranslationSource.objects.create(
object_id=self.snippet.translation_key,
specific_content_type=ContentType.objects.get_for_model(
TestRevisionsButNoDraftModel
),
locale=self.snippet.locale,
content_json=json.dumps(
{
"pk": self.snippet.pk,
"field": "Some different content", # Changed
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
}
),
last_updated_at=timezone.now(),
)

new_source, created = TranslationSource.update_or_create_from_instance(
self.snippet
)

self.assertFalse(created)

self.assertEqual(source, new_source)
self.assertEqual(
json.loads(source.content_json)["field"], "Some different content"
)
self.assertEqual(
json.loads(new_source.content_json)["field"], "This is some test content"
)


class TestAsInstanceForPage(TestCase):
def setUp(self):
self.page = create_test_page(title="Test page", slug="test-page")
Expand Down Expand Up @@ -706,6 +819,65 @@ def test_update_validates_fields(self):
)


class TestCreateOrUpdateTranslationForRevisionsWithoutDraftsSnippet(
TransactionTestCase
):
def setUp(self):
# The default locale created by Wagtail gets flushed when using TransactionTestCase
# however this is necessary to check the behaviour of on_commit hook in
# the update_or_create_translation method
self.source_locale, _ = Locale.objects.get_or_create(language_code="en")
self.dest_locale = Locale.objects.create(language_code="fr")

self.snippet = TestRevisionsButNoDraftModel.objects.create(
field="Test snippet content"
)
self.source, created = TranslationSource.get_or_create_from_instance(
self.snippet
)

# Add translation for field
self.string = String.from_value(
self.source_locale, StringValue.from_plaintext("Test snippet content")
)
self.translation = StringTranslation.objects.create(
translation_of=self.string,
locale=self.dest_locale,
context=TranslationContext.objects.get(
object_id=self.snippet.translation_key, path="field"
),
data="Tester le contenu",
)

def test_create(self):
new_snippet, created = self.source.create_or_update_translation(
self.dest_locale
)

self.assertTrue(created)
self.assertEqual(new_snippet.field, "Tester le contenu")
self.assertEqual(new_snippet.translation_key, self.snippet.translation_key)
self.assertEqual(new_snippet.locale, self.dest_locale)
self.assertTrue(
self.source.translation_logs.filter(locale=self.dest_locale).exists()
)

def test_update(self):
self.snippet.copy_for_translation(self.dest_locale).save()

new_snippet, created = self.source.create_or_update_translation(
self.dest_locale
)

self.assertFalse(created)
self.assertEqual(new_snippet.field, "Tester le contenu")
self.assertEqual(new_snippet.translation_key, self.snippet.translation_key)
self.assertEqual(new_snippet.locale, self.dest_locale)
self.assertTrue(
self.source.translation_logs.filter(locale=self.dest_locale).exists()
)


class TestGetEphemeralTranslatedInstance(TestCase):
def setUp(self):
self.page = create_test_page(
Expand Down