Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Dec 20, 2024
1 parent ee1f5b0 commit 5c836c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
</div>
<p class="help">{{ field.help_text }}</p>
</li>
{% elif field.name == 'use_machine_translation' %}
<li class="use-machine-translation-field">
<div class="field boolean_field checkbox_input">
<div class="field-content">
<div class="input">
<input type="checkbox" name="{{ field.name }}" id="{{ field.auto_id }}">
<label class="use-machine-translation-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
</div>
</div>
</div>
<p class="help">{{ field.help_text }}</p>
</li>
{% else %}
<li>{% include "wagtailadmin/shared/field.html" %}</li>
{% endif %}
Expand Down
26 changes: 22 additions & 4 deletions wagtail_localize/tests/test_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.forms.widgets import CheckboxInput, HiddenInput
from django.forms.widgets import CheckboxInput
from django.test import TestCase, override_settings
from django.urls import reverse
from wagtail.models import Locale, Page, PageViewRestriction
Expand All @@ -19,6 +19,7 @@
)
from wagtail_localize.test.models import NonTranslatableSnippet, TestSnippet

from ..views.update_translations import UpdateTranslationsForm
from .utils import assert_permission_denied, make_test_page


Expand Down Expand Up @@ -632,10 +633,20 @@ def test_update_translations_form_without_machine_translator(self):

self.assertEqual(response.status_code, 200)

self.assertIsInstance(
response.context["form"].fields["use_machine_translation"].widget,
HiddenInput,
self.assertNotContains(
response,
"Apply the updates and publish immediately. The changes will use the original language until translated.",
)
self.assertNotContains(response, "use_machine_translation")
self.assertNotIn("use_machine_translation", response.context["form"].fields)

def test_update_translations_form_validation_without_machine_translator(self):
form = UpdateTranslationsForm(data={"use_machine_translation": True})
with mock.patch(
"wagtail_localize.views.update_translations.HAS_MACHINE_TRANSLATOR", False
):
self.assertFalse(form.is_valid())
self.assertFormError(form, None, ["Could not find any machine translator."])

def test_update_translations_form_with_machine_translator(self):
response = self.client.get(
Expand All @@ -652,6 +663,13 @@ def test_update_translations_form_with_machine_translator(self):
CheckboxInput,
)

self.assertContains(
response,
"Apply the updates and publish immediately. The changes will use "
"the original language until translated unless you also select "
"&quot;Use machine translation&quot;.",
)

def test_post_update_page_translation_with_publish_translations_and_use_machine_translation(
self,
):
Expand Down
17 changes: 8 additions & 9 deletions wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,40 @@
HAS_MACHINE_TRANSLATOR = get_machine_translator() is not None

if HAS_MACHINE_TRANSLATOR:
PUBLISH_TRANSLATIONS_HELP = (
PUBLISH_TRANSLATIONS_HELP = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated unless you also select "
'"Use machine translation".'
)
else:
PUBLISH_TRANSLATIONS_HELP = (
PUBLISH_TRANSLATIONS_HELP = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated."
)

USE_MACHINE_TRANSLATION_HELP = "Apply machine translations to the incoming changes."


class UpdateTranslationsForm(forms.Form):
publish_translations = forms.BooleanField(
label=gettext_lazy("Publish immediately"),
help_text=gettext_lazy(PUBLISH_TRANSLATIONS_HELP),
help_text=PUBLISH_TRANSLATIONS_HELP,
required=False,
)
use_machine_translation = forms.BooleanField(
label=gettext_lazy("Use machine translation"),
help_text=gettext_lazy(USE_MACHINE_TRANSLATION_HELP),
help_text=gettext_lazy("Apply machine translations to the incoming changes."),
required=False,
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not HAS_MACHINE_TRANSLATOR:
self.fields["use_machine_translation"].widget = forms.HiddenInput()
del self.fields["use_machine_translation"]

def clean(self):
cleaned_data = super().clean()
if cleaned_data.get("use_machine_translation") and not HAS_MACHINE_TRANSLATOR:
raise ValidationError(_("A machine translator could not be found."))
if not HAS_MACHINE_TRANSLATOR and cleaned_data.get("use_machine_translation"):
raise ValidationError(_("Could not find any machine translator."))
return cleaned_data


class UpdateTranslationsView(SingleObjectMixin, TemplateView):
Expand Down

0 comments on commit 5c836c9

Please sign in to comment.