Skip to content

Commit

Permalink
Make sure field level validation runs when translating snippets
Browse files Browse the repository at this point in the history
Fixes #426
  • Loading branch information
kaedroho committed Jul 2, 2021
1 parent 1d76adf commit eecbb9a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- [Make sure field level validation runs when translating snippets](https://github.com/wagtail/wagtail-localize/pull/427)

[unreleased]: https://github.com/wagtail/wagtail-localize/compare/v1.0rc2...HEAD
3 changes: 3 additions & 0 deletions wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ def create_or_update_translation(self, locale, user=None, publish=True, copy_par
page_revision.publish()

else:
# Note: we don't need to run full_clean for Pages as Wagtail does that in Page.save()
translation.full_clean()

translation.save()
page_revision = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.4 on 2021-06-29 15:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wagtail_localize_test', '0014_auto_20210217_0924'),
]

operations = [
migrations.AddField(
model_name='testsnippet',
name='small_charfield',
field=models.CharField(blank=True, max_length=10),
),
]
4 changes: 3 additions & 1 deletion wagtail_localize/test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
@register_snippet
class TestSnippet(TranslatableMixin, models.Model):
field = models.TextField(gettext_lazy("field"))
# To test field level validation of snippets
small_charfield = models.CharField(max_length=10, blank=True)

translatable_fields = [TranslatableField("field")]
translatable_fields = [TranslatableField("field"), TranslatableField("small_charfield")]


@register_snippet
Expand Down
83 changes: 83 additions & 0 deletions wagtail_localize/tests/test_translationsource_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone
from wagtail.core.blocks import StreamValue
Expand Down Expand Up @@ -61,6 +62,7 @@ def test_create(self):
{
"pk": self.snippet.pk,
"field": "This is some test content",
"small_charfield": "",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
},
Expand All @@ -76,6 +78,7 @@ def test_update(self):
{
"pk": self.snippet.pk,
"field": "Some different content", # Changed
"small_charfield": "",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
}
Expand Down Expand Up @@ -109,6 +112,7 @@ def test_create(self):
{
"pk": self.snippet.pk,
"field": "This is some test content",
"small_charfield": "",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
},
Expand All @@ -124,6 +128,7 @@ def test_update(self):
{
"pk": self.snippet.pk,
"field": "Some different content", # Changed
"small_charfield": "",
"translation_key": str(self.snippet.translation_key),
"locale": self.snippet.locale_id,
}
Expand Down Expand Up @@ -553,6 +558,84 @@ def test_convert_alias(self):
self.assertTrue(PageLogEntry.objects.filter(page=new_page, action='wagtail.convert_alias').exists())


class TestCreateOrUpdateTranslationForSnippet(TestCase):
def setUp(self):
self.snippet = TestSnippet.objects.create(field="Test snippet content", small_charfield="Small text")
self.source, created = TranslationSource.get_or_create_from_instance(self.snippet)
self.source_locale = Locale.objects.get(language_code="en")
self.dest_locale = Locale.objects.create(language_code="fr")

# 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 de l'extrait",
)

# Add translation for small_charfield
self.small_string = String.from_value(
self.source_locale, StringValue.from_plaintext("Small text")
)
self.small_translation = StringTranslation.objects.create(
translation_of=self.small_string,
locale=self.dest_locale,
context=TranslationContext.objects.get(
object_id=self.snippet.translation_key, path="small_charfield"
),
data="Petit text", # Truncated to fit because the field is limited to 10 characters
)

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 de l'extrait")
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_create_validates_fields(self):
self.small_translation.data = "Petit texte" # More than 10 characters
self.small_translation.save()

with self.assertRaises(ValidationError) as e:
self.source.create_or_update_translation(self.dest_locale)

self.assertEqual(e.exception.messages, ['Ensure this value has at most 10 characters (it has 11).'])

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 de l'extrait")
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_validates_fields(self):
self.snippet.copy_for_translation(self.dest_locale).save()

self.small_translation.data = "Petit texte" # More than 10 characters
self.small_translation.save()

with self.assertRaises(ValidationError) as e:
self.source.create_or_update_translation(self.dest_locale)

self.assertEqual(e.exception.messages, ['Ensure this value has at most 10 characters (it has 11).'])


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

0 comments on commit eecbb9a

Please sign in to comment.