Exemplo n.º 1
0
def auto_translation(request, project, component, lang):
    translation = get_translation(request, project, component, lang)
    project = translation.component.project
    if not request.user.has_perm('translation.auto', project):
        raise PermissionDenied()

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

    if translation.component.locked or not autoform.is_valid():
        messages.error(request, _('Failed to process form!'))
        show_form_errors(request, autoform)
        return redirect(translation)

    auto = AutoTranslate(request.user,
                         translation,
                         autoform.cleaned_data['type'],
                         request=request)

    if autoform.cleaned_data['auto_source'] == 'mt':
        auto.process_mt(
            autoform.cleaned_data['engines'],
            autoform.cleaned_data['threshold'],
        )
    else:
        auto.process_others(autoform.cleaned_data['component'], )

    import_message(
        request, auto.updated,
        _('Automatic translation completed, no strings were updated.'),
        ungettext('Automatic translation completed, %d string was updated.',
                  'Automatic translation completed, %d strings were updated.',
                  auto.updated))

    return redirect(translation)
Exemplo n.º 2
0
def auto_translation(request, project, component, lang):
    translation = get_translation(request, project, component, lang)
    project = translation.component.project
    if not request.user.has_perm("translation.auto", project):
        raise PermissionDenied()

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

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

    args = (
        request.user.id,
        translation.id,
        autoform.cleaned_data["mode"],
        autoform.cleaned_data["filter_type"],
        autoform.cleaned_data["auto_source"],
        autoform.cleaned_data["component"],
        autoform.cleaned_data["engines"],
        autoform.cleaned_data["threshold"],
    )

    if settings.CELERY_TASK_ALWAYS_EAGER:
        messages.success(request, auto_translate(*args))
    else:
        task = auto_translate.delay(*args)
        messages.success(request, _("Automatic translation in progress"),
                         f"task:{task.id}")

    return redirect(translation)
Exemplo n.º 3
0
def auto_translation(request, project, subproject, lang):
    translation = get_translation(request, project, subproject, lang)
    project = translation.subproject.project
    if not can_automatic_translation(request.user, project):
        raise PermissionDenied()

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

    if translation.subproject.locked or not autoform.is_valid():
        messages.error(request, _('Failed to process form!'))
        return redirect(translation)

    updated = auto_translate(request.user, translation,
                             autoform.cleaned_data['subproject'],
                             autoform.cleaned_data['inconsistent'],
                             autoform.cleaned_data['overwrite'])

    import_message(
        request, updated,
        _('Automatic translation completed, no strings were updated.'),
        ungettext('Automatic translation completed, %d string was updated.',
                  'Automatic translation completed, %d strings were updated.',
                  updated))

    return redirect(translation)
Exemplo n.º 4
0
 def autotranslate(self, request, **kwargs):
     translation = self.get_object()
     if not request.user.has_perm("translation.auto", translation):
         self.permission_denied(request, message="Can not auto translate")
     autoform = AutoForm(translation.component, request.data)
     if translation.component.locked or not autoform.is_valid():
         return Response(
             data={
                 "result": "Unsuccessful",
                 "detail": "Failed to process autotranslation data!",
             },
             status=status.HTTP_400_BAD_REQUEST,
         )
     args = (
         request.user.id,
         translation.id,
         autoform.cleaned_data["mode"],
         autoform.cleaned_data["filter_type"],
         autoform.cleaned_data["auto_source"],
         autoform.cleaned_data["component"],
         autoform.cleaned_data["engines"],
         autoform.cleaned_data["threshold"],
     )
     return Response(
         data={
             "result": "Success",
             "details": auto_translate(*args)
         },
         status=status.HTTP_200_OK,
     )
Exemplo n.º 5
0
def auto_translation(request, project, subproject, lang):
    translation = get_translation(request, project, subproject, lang)
    project = translation.subproject.project
    if not can_automatic_translation(request.user, project):
        raise PermissionDenied()

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

    if translation.subproject.locked or not autoform.is_valid():
        messages.error(request, _('Failed to process form!'))
        return redirect(translation)

    updated = auto_translate(
        request.user,
        translation,
        autoform.cleaned_data['subproject'],
        autoform.cleaned_data['inconsistent'],
        autoform.cleaned_data['overwrite']
    )

    import_message(
        request, updated,
        _('Automatic translation completed, no strings were updated.'),
        ungettext(
            'Automatic translation completed, %d string was updated.',
            'Automatic translation completed, %d strings were updated.',
            updated
        )
    )

    return redirect(translation)
Exemplo n.º 6
0
def auto_translation(request, project, subproject, lang):
    translation = get_translation(request, project, subproject, lang)
    translation.commit_pending(request)
    autoform = AutoForm(translation, request.POST)
    change = None
    if not translation.subproject.locked and autoform.is_valid():
        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
        )
        if autoform.cleaned_data['subproject'] == '':
            sources = sources.filter(
                translation__subproject__project=translation.subproject.project
            ).exclude(
                translation=translation
            )
        else:
            subprj = SubProject.objects.get(
                project=translation.subproject.project,
                slug=autoform.cleaned_data['subproject']
            )
            sources = sources.filter(translation__subproject=subprj)

        for unit in units.iterator():
            update = sources.filter(checksum=unit.checksum)
            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.'))
    else:
        messages.error(request, _('Failed to process form!'))

    return redirect(translation)
Exemplo n.º 7
0
def auto_translation(request, project, subproject, lang):
    obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project, enabled = True)
    obj.commit_pending()
    autoform = AutoForm(obj, request.POST)
    change = None
    if not obj.subproject.locked and autoform.is_valid():
        if autoform.cleaned_data['inconsistent']:
            units = obj.unit_set.filter_type('inconsistent')
        elif autoform.cleaned_data['overwrite']:
            units = obj.unit_set.all()
        else:
            units = obj.unit_set.filter(translated = False)

        sources = Unit.objects.filter(translation__language = obj.language, translated = True)
        if autoform.cleaned_data['subproject'] == '':
            sources = sources.filter(translation__subproject__project = obj.subproject.project).exclude(translation = obj)
        else:
            subprj = SubProject.objects.get(project = obj.subproject.project, slug = autoform.cleaned_data['subproject'])
            sources = sources.filter(translation__subproject = subprj)

        for unit in units.iterator():
            update = sources.filter(checksum = unit.checksum)
            if update.count() > 0:
                # 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(unit = unit, user = request.user)
                # Save unit to backend
                unit.save_backend(request, False, False)

        messages.info(request, _('Automatic translation completed.'))
    else:
        messages.error(request, _('Failed to process form!'))

    return HttpResponseRedirect(obj.get_absolute_url())
Exemplo n.º 8
0
def auto_translation(request, project, component, lang):
    translation = get_translation(request, project, component, lang)
    project = translation.component.project
    if not request.user.has_perm('translation.auto', project):
        raise PermissionDenied()

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

    if translation.component.locked or not autoform.is_valid():
        messages.error(request, _('Failed to process form!'))
        show_form_errors(request, autoform)
        return redirect(translation)

    auto = AutoTranslate(
        request.user,
        translation,
        autoform.cleaned_data['type'],
        request=request
    )

    if autoform.cleaned_data['auto_source'] == 'mt':
        auto.process_mt(
            autoform.cleaned_data['engines'],
            autoform.cleaned_data['threshold'],
        )
    else:
        auto.process_others(
            autoform.cleaned_data['component'],
        )

    import_message(
        request, auto.updated,
        _('Automatic translation completed, no strings were updated.'),
        ungettext(
            'Automatic translation completed, %d string was updated.',
            'Automatic translation completed, %d strings were updated.',
            auto.updated
        )
    )

    return redirect(translation)
Exemplo n.º 9
0
 def autotranslate(self, request, **kwargs):
     translation = self.get_object()
     if not request.user.has_perm("translation.auto", translation):
         self.permission_denied(request, "Can not auto translate")
     autoform = AutoForm(translation.component, request.data)
     if translation.component.locked or not autoform.is_valid():
         raise ParseError("Failed to process autotranslation data!", "invalid")
     args = (
         request.user.id,
         translation.id,
         autoform.cleaned_data["mode"],
         autoform.cleaned_data["filter_type"],
         autoform.cleaned_data["auto_source"],
         autoform.cleaned_data["component"],
         autoform.cleaned_data["engines"],
         autoform.cleaned_data["threshold"],
     )
     return Response(
         data={"details": auto_translate(*args)},
         status=HTTP_200_OK,
     )