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 cd7d64b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 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
24 changes: 18 additions & 6 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 Down Expand Up @@ -620,9 +620,12 @@ def test_post_update_page_translation_will_run_update_target_view_restrictions(
self.assertEqual(update_target_view_restrictions.call_count, 2)

@mock.patch(
"wagtail_localize.views.update_translations.HAS_MACHINE_TRANSLATOR", False
"wagtail_localize.views.update_translations.get_machine_translator",
return_value=None,
)
def test_update_translations_form_without_machine_translator(self):
def test_update_translations_form_without_machine_translator(
self, mocked_get_machine_translator
):
response = self.client.get(
reverse(
"wagtail_localize:update_translations",
Expand All @@ -632,10 +635,12 @@ 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.assertContains(
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_with_machine_translator(self):
response = self.client.get(
Expand All @@ -652,6 +657,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
49 changes: 20 additions & 29 deletions wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,35 @@
from wagtail_localize.views.submit_translations import TranslationComponentManager


HAS_MACHINE_TRANSLATOR = get_machine_translator() is not None

if HAS_MACHINE_TRANSLATOR:
PUBLISH_TRANSLATIONS_HELP = (
"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 = (
"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),
required=False,
)
use_machine_translation = forms.BooleanField(
label=gettext_lazy("Use machine translation"),
help_text=gettext_lazy(USE_MACHINE_TRANSLATION_HELP),
required=False,
)
_has_machine_translator = False

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

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 self._has_machine_translator:
self.fields["publish_translations"].help_text = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated unless you also select "
'"Use machine translation".'
)
self.fields["use_machine_translation"] = forms.BooleanField(
label=gettext_lazy("Use machine translation"),
help_text=gettext_lazy(
"Apply machine translations to the incoming changes."
),
required=False,
)
else:
self.fields["publish_translations"].help_text = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated."
)


class UpdateTranslationsView(SingleObjectMixin, TemplateView):
Expand Down

0 comments on commit cd7d64b

Please sign in to comment.