Skip to content

Commit

Permalink
Add auto translation as "approved" (Closes: #9048) (#9174)
Browse files Browse the repository at this point in the history
* Add auto translation as "approved" (Closes: 9048)

* Only show approved choice if user has the permission

* fixup! Only show approved choice if user has the permission
  • Loading branch information
jspricke committed May 3, 2023
1 parent 7a50cce commit 6ef6420
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion weblate/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ def autotranslate(self, request, **kwargs):
if translation.component.locked:
self.permission_denied(request, "Component is locked")

autoform = AutoForm(translation.component, request.data)
autoform = AutoForm(translation.component, request.user, request.data)
if not autoform.is_valid():
errors = {}
for field in autoform:
Expand Down
8 changes: 6 additions & 2 deletions weblate/trans/autotranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from weblate.machinery.models import MACHINERY
from weblate.trans.models import Change, Component, Suggestion, Unit
from weblate.trans.util import split_plural
from weblate.utils.state import STATE_FUZZY, STATE_TRANSLATED
from weblate.utils.state import STATE_APPROVED, STATE_FUZZY, STATE_TRANSLATED


class AutoTranslate:
Expand All @@ -31,7 +31,11 @@ def __init__(
self.mode = mode
self.updated = 0
self.progress_steps = 0
self.target_state = STATE_FUZZY if mode == "fuzzy" else STATE_TRANSLATED
self.target_state = STATE_TRANSLATED
if mode == "fuzzy":
self.target_state = STATE_FUZZY
elif mode == "approved":
self.target_state = STATE_APPROVED
self.component_wide = component_wide

def get_units(self, filter_mode=True):
Expand Down
16 changes: 10 additions & 6 deletions weblate/trans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,6 @@ class AutoForm(forms.Form):

mode = forms.ChoiceField(
label=_("Automatic translation mode"),
choices=[
("suggest", _("Add as suggestion")),
("translate", _("Add as translation")),
("fuzzy", _('Add as "Needing edit"')),
],
initial="suggest",
)
filter_type = FilterField(
Expand Down Expand Up @@ -908,7 +903,7 @@ class AutoForm(forms.Form):
label=_("Score threshold"), initial=80, min_value=1, max_value=100
)

def __init__(self, obj, *args, **kwargs):
def __init__(self, obj, user=None, *args, **kwargs):
"""Generate choices for other components in the same project."""
super().__init__(*args, **kwargs)
self.obj = obj
Expand Down Expand Up @@ -969,6 +964,15 @@ def __init__(self, obj, *args, **kwargs):
x for x in self.fields["filter_type"].choices if x[0] in use_types
]

choices = [
("suggest", _("Add as suggestion")),
("translate", _("Add as translation")),
("fuzzy", _('Add as "Needing edit"')),
]
if user is not None and user.has_perm("unit.review", obj):
choices.append(("approved", _("Add as approved translation")))
self.fields["mode"].choices = choices

self.helper = FormHelper(self)
self.helper.layout = Layout(
Field("mode"),
Expand Down
7 changes: 7 additions & 0 deletions weblate/trans/tests/test_autotranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def setUp(self):
# Need extra power
self.user.is_superuser = True
self.user.save()
self.project.translation_review = True
self.project.save()
self.component2 = Component.objects.create(
name="Test 2",
slug="test-2",
Expand Down Expand Up @@ -83,6 +85,11 @@ def test_suggest(self):
self.perform_auto(mode="suggest")
self.perform_auto(0, 1, mode="suggest")

def test_approved(self):
"""Test for automatic suggestion."""
self.perform_auto(mode="approved")
self.perform_auto(0, 1, mode="approved")

def test_inconsistent(self):
self.perform_auto(0, filter_type="check:inconsistent")

Expand Down
7 changes: 6 additions & 1 deletion weblate/trans/views/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,12 @@ def show_translation(request, project, component, lang):
"form": form,
"download_form": DownloadForm(obj, auto_id="id_dl_%s"),
"autoform": optional_form(
AutoForm, user, "translation.auto", obj, obj=component
AutoForm,
user,
"translation.auto",
obj,
obj=component,
user=user,
),
"search_form": search_form,
"replace_form": optional_form(ReplaceForm, user, "unit.edit", obj),
Expand Down
2 changes: 1 addition & 1 deletion weblate/trans/views/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def auto_translation(request, project, component, lang):
if not request.user.has_perm("translation.auto", project):
raise PermissionDenied

autoform = AutoForm(translation.component, request.POST)
autoform = AutoForm(translation.component, request.user, request.POST)

if translation.component.locked or not autoform.is_valid():
messages.error(request, _("Failed to process form!"))
Expand Down

0 comments on commit 6ef6420

Please sign in to comment.