Commit f0c783ae authored by Michal Čihař's avatar Michal Čihař

Move auto translation to separate module

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent b65b1c0e
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2016 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from weblate.trans.models import Unit, Change, SubProject
from django.core.exceptions import PermissionDenied
def auto_translate(request, translation, source, inconsistent, overwrite):
change = None
updated = 0
if inconsistent:
units = translation.unit_set.filter_type(
'inconsistent', translation
)
elif overwrite:
units = translation.unit_set.all()
else:
units = translation.unit_set.filter(translated=False)
sources = Unit.objects.filter(
translation__language=translation.language,
translated=True
)
if source:
subprj = SubProject.objects.get(id=source)
if not subprj.has_acl(user):
raise PermissionDenied()
sources = sources.filter(translation__subproject=subprj)
else:
sources = sources.filter(
translation__subproject__project=translation.subproject.project
).exclude(
translation=translation
)
translation.commit_pending(None)
for unit in units.iterator():
update = sources.filter(source=unit.source)
if update.exists():
# Get first entry
update = update[0]
# No save if translation is same
if unit.fuzzy == update.fuzzy and unit.target == update.target:
continue
# Copy translation
unit.fuzzy = update.fuzzy
unit.target = update.target
# Create signle change object for whole merge
if change is None:
change = Change.objects.create(
action=Change.ACTION_AUTO,
translation=unit.translation,
user=request.user,
author=request.user
)
# Save unit to backend
unit.save_backend(request, False, False)
updated += 1
return updated
......@@ -18,12 +18,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import unicode_literals
import uuid
import time
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext as _, ungettext
from django.utils.encoding import force_text
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import messages
......@@ -32,7 +34,7 @@ from django.utils import formats
from django.core.exceptions import PermissionDenied
from weblate.trans.models import (
SubProject, Unit, Change, Comment, Suggestion, Dictionary,
Unit, Change, Comment, Suggestion, Dictionary,
get_related_units,
)
from weblate.trans.autofixes import fix_target
......@@ -44,6 +46,7 @@ from weblate.trans.forms import (
from weblate.trans.views.helper import get_translation
from weblate.trans.checks import CHECKS
from weblate.trans.util import join_plural
from weblate.trans.autotranslate import auto_translate
from weblate.trans.permissions import (
can_translate, can_suggest, can_accept_suggestion, can_delete_suggestion,
can_vote_suggestion, can_delete_comment, can_automatic_translation,
......@@ -605,64 +608,33 @@ def auto_translation(request, project, subproject, lang):
if not can_automatic_translation(request.user, project):
raise PermissionDenied()
translation.commit_pending(request)
autoform = AutoForm(translation, request.user, request.POST)
change = None
if translation.subproject.locked or not autoform.is_valid():
messages.error(request, _('Failed to process form!'))
return redirect(translation)
if autoform.cleaned_data['inconsistent']:
units = translation.unit_set.filter_type(
'inconsistent', translation
)
elif autoform.cleaned_data['overwrite']:
units = translation.unit_set.all()
else:
units = translation.unit_set.filter(translated=False)
sources = Unit.objects.filter(
translation__language=translation.language,
translated=True
updated = auto_translate(
request,
translation,
autoform.cleaned_data['subproject'],
autoform.cleaned_data['inconsistent'],
autoform.cleaned_data['overwrite']
)
if autoform.cleaned_data['subproject'] == '':
sources = sources.filter(
translation__subproject__project=translation.subproject.project
).exclude(
translation=translation
if updated == 0:
messages.info(
request, _('Automatic translation completed, no strings updated.')
)
else:
subprj = SubProject.objects.get(
id=autoform.cleaned_data['subproject']
messages.success(
request,
ungettext(
'Automatic translation completed, {0} string udated.',
'Automatic translation completed, {0} strings udated.',
updated
).format(updated)
)
if not subprj.has_acl(request.user):
raise PermissionDenied()
sources = sources.filter(translation__subproject=subprj)
for unit in units.iterator():
update = sources.filter(source=unit.source)
if update.exists():
# Get first entry
update = update[0]
# No save if translation is same
if unit.fuzzy == update.fuzzy and unit.target == update.target:
continue
# Copy translation
unit.fuzzy = update.fuzzy
unit.target = update.target
# Create signle change object for whole merge
if change is None:
change = Change.objects.create(
action=Change.ACTION_AUTO,
translation=unit.translation,
user=request.user,
author=request.user
)
# Save unit to backend
unit.save_backend(request, False, False)
messages.success(request, _('Automatic translation completed.'))
return redirect(translation)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment