-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathupdate_translations.py
More file actions
212 lines (176 loc) · 7.84 KB
/
Copy pathupdate_translations.py
File metadata and controls
212 lines (176 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import contextlib
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.utils import quote
from django.core.exceptions import PermissionDenied, ValidationError
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
from django.views.generic import TemplateView
from django.views.generic.detail import SingleObjectMixin
from wagtail.admin.views.pages.utils import get_valid_next_url_from_request
from wagtail.models import Page
from wagtail.snippets.models import get_snippet_models
from wagtail.utils.version import get_main_version
from wagtail_localize.machine_translators import get_machine_translator
from wagtail_localize.models import TranslationSource
from wagtail_localize.views.edit_translation import apply_machine_translation
from wagtail_localize.views.submit_translations import TranslationComponentManager
def update_translations(
request,
instance,
translations,
use_machine_translation=False,
publish_translations=False,
):
instance.update_from_db()
if use_machine_translation:
machine_translator = get_machine_translator()
for translation in translations.select_related("target_locale"):
apply_machine_translation(translation.id, request.user, machine_translator)
if publish_translations:
for translation in translations.select_related("target_locale"):
with contextlib.suppress(ValidationError):
translation.save_target(user=request.user, publish=True)
else:
for translation in translations.select_related("source", "target_locale"):
with contextlib.suppress(ValidationError):
translation.source.update_target_view_restrictions(
translation.target_locale
)
class UpdateTranslationsForm(forms.Form):
publish_translations = forms.BooleanField(
label=gettext_lazy("Publish immediately"),
required=False,
)
_has_machine_translator = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._has_machine_translator = get_machine_translator() is not None
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):
template_name = "wagtail_localize/admin/update_translations.html"
title = gettext_lazy("Update existing translations")
def get_object(self):
return get_object_or_404(
TranslationSource, id=self.kwargs["translation_source_id"]
)
def get_title(self):
return self.title
def get_subtitle(self):
return self.object.object_repr
def get_form(self):
if self.request.method == "POST":
return UpdateTranslationsForm(self.request.POST)
else:
return UpdateTranslationsForm()
def get_success_message(self):
return _("Successfully updated translations for '{object}'").format(
object=self.object.object_repr
)
def get_success_url(self):
return get_valid_next_url_from_request(self.request)
def get_default_success_url(self):
instance = self.object.get_source_instance()
if isinstance(instance, Page):
return reverse("wagtailadmin_explore", args=[instance.get_parent().id])
elif instance._meta.model in get_snippet_models():
return reverse(
f"wagtailsnippets_{instance._meta.app_label}_{instance._meta.model_name}:edit",
args=[quote(instance.pk)],
)
elif "wagtail_localize.modeladmin" in settings.INSTALLED_APPS:
return reverse(
f"{instance._meta.app_label}_{instance._meta.model_name}_modeladmin_index"
)
def get_edit_url(self, instance):
if isinstance(instance, Page):
return reverse("wagtailadmin_pages:edit", args=[instance.id])
elif instance._meta.model in get_snippet_models():
return reverse(
f"wagtailsnippets_{instance._meta.app_label}_{instance._meta.model_name}:edit",
args=[quote(instance.pk)],
)
elif "wagtail_localize.modeladmin" in settings.INSTALLED_APPS:
return reverse(
f"{instance._meta.app_label}_{instance._meta.model_name}_modeladmin_edit",
args=[quote(instance.pk)],
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(
{
"translations": [
{
"title": str(translation.get_target_instance()),
"locale": translation.target_locale,
"edit_url": self.get_edit_url(
translation.get_target_instance()
),
}
for translation in self.object.translations.filter(
enabled=True
).select_related("target_locale")
],
"last_sync_date": self.object.last_updated_at,
"form": self.get_form(),
"next_url": self.get_success_url(),
"back_url": self.get_success_url() or self.get_default_success_url(),
"components": self.components,
"wagtail_version": get_main_version(),
}
)
return context
def post(self, request, **kwargs):
form = self.get_form()
if form.is_valid() and self.components.is_valid():
return self.form_valid(form)
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
@transaction.atomic
def form_valid(self, form):
enabled_translations = self.object.translations.filter(enabled=True)
update_translations(
self.request,
self.object,
enabled_translations,
use_machine_translation=form.cleaned_data.get(
"use_machine_translation", False
),
publish_translations=form.cleaned_data["publish_translations"],
)
self.components.save(
self.object,
sources_and_translations={self.object: list(enabled_translations)},
)
# TODO: Button that links to page in translations report when we have it
messages.success(self.request, self.get_success_message())
return redirect(self.get_success_url() or self.get_default_success_url())
def dispatch(self, request, *args, **kwargs):
if not request.user.has_perms(["wagtail_localize.submit_translation"]):
raise PermissionDenied
self.object = self.get_object()
self.components = TranslationComponentManager.from_request(
self.request, source_object_instance=self.object
)
return super().dispatch(request, *args, **kwargs)