Exemplo n.º 1
0
 def __init__(self, user, translation, unit, *args, **kwargs):
     if unit is not None:
         kwargs['initial'] = {
             'checksum': unit.checksum,
             'contentsum': hash_to_checksum(unit.content_hash),
             'translationsum': hash_to_checksum(unit.get_target_hash()),
             'target': unit,
             'fuzzy': unit.fuzzy,
             'review': unit.state,
         }
         kwargs['auto_id'] = 'id_{0}_%s'.format(unit.checksum)
     tabindex = kwargs.pop('tabindex', 100)
     super(TranslationForm, self).__init__(translation, *args, **kwargs)
     self.user = user
     self.fields['target'].widget.attrs['tabindex'] = tabindex
     self.fields['target'].widget.profile = user.profile
     self.fields['review'].widget.attrs['class'] = 'review_radio'
     # Avoid failing validation on not translated string
     if args:
         self.fields['review'].choices.append((STATE_EMPTY, ''))
     self.helper = FormHelper()
     self.helper.form_method = 'post'
     self.helper.layout = Layout(
         Field('checksum'),
         Field('target'),
         Field('fuzzy'),
         Field('contentsum'),
         Field('translationsum'),
         InlineRadios('review'),
     )
     if unit and can_review(user, unit.translation):
         self.fields['fuzzy'].widget = forms.HiddenInput()
     else:
         self.fields['review'].widget = forms.HiddenInput()
Exemplo n.º 2
0
 def edit_unit(self, source, target, language="cs", **kwargs):
     """Do edit single unit using web interface."""
     unit = self.get_unit(source, language)
     params = {
         "checksum": unit.checksum,
         "contentsum": hash_to_checksum(unit.content_hash),
         "translationsum": hash_to_checksum(unit.get_target_hash()),
         "target_0": target,
         "review": "20",
     }
     params.update(kwargs)
     return self.client.post(self.translate_url, params)
Exemplo n.º 3
0
 def edit_unit(self, source, target, **kwargs):
     """Do edit single unit using web interface."""
     unit = self.get_unit(source)
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': target,
         'review': '20',
     }
     params.update(kwargs)
     return self.client.post(self.get_translation().get_translate_url(), params)
Exemplo n.º 4
0
 def test_save_zen(self):
     unit = self.get_unit()
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': 'Zen translation',
         'review': '20',
     }
     response = self.client.post(
         reverse('save_zen', kwargs=self.kw_translation), params)
     self.assertContains(
         response, 'Following fixups were applied to translation: '
         'Trailing and leading whitespace')
Exemplo n.º 5
0
 def edit_unit(self, source, target, **kwargs):
     """Do edit single unit using web interface."""
     unit = self.get_unit(source)
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': target,
         'review': '20',
     }
     params.update(kwargs)
     return self.client.post(
         self.get_translation().get_translate_url(),
         params
     )
Exemplo n.º 6
0
 def test_save_zen_lock(self):
     self.component.locked = True
     self.component.save()
     unit = self.get_unit()
     params = {
         "checksum": unit.checksum,
         "contentsum": hash_to_checksum(unit.content_hash),
         "translationsum": hash_to_checksum(unit.get_target_hash()),
         "target_0": "Zen translation",
         "review": "20",
     }
     response = self.client.post(
         reverse("save_zen", kwargs=self.kw_translation), params)
     self.assertContains(
         response, "Insufficient privileges for saving translations.")
Exemplo n.º 7
0
 def test_save_zen_lock(self):
     self.subproject.locked = True
     self.subproject.save()
     unit = self.get_unit()
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': 'Zen translation',
         'review': '20',
     }
     response = self.client.post(
         reverse('save_zen', kwargs=self.kw_translation), params)
     self.assertContains(
         response, 'Insufficient privileges for saving translations.')
Exemplo n.º 8
0
 def test_save_zen(self):
     unit = self.get_unit()
     params = {
         "checksum": unit.checksum,
         "contentsum": hash_to_checksum(unit.content_hash),
         "translationsum": hash_to_checksum(unit.get_target_hash()),
         "target_0": "Zen translation",
         "review": "20",
     }
     response = self.client.post(
         reverse("save_zen", kwargs=self.kw_translation), params)
     self.assertContains(
         response,
         "Following fixups were applied to translation: "
         "Trailing and leading whitespace",
     )
Exemplo n.º 9
0
 def test_checksum(self):
     """Hash to checksum conversion"""
     text_hash = calculate_hash(None, 'Message')
     checksum = hash_to_checksum(text_hash)
     self.assertEqual(
         text_hash,
         checksum_to_hash(checksum)
     )
Exemplo n.º 10
0
 def test_save_zen(self):
     unit = self.get_unit()
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': 'Zen translation',
         'review': '20',
     }
     response = self.client.post(
         reverse('save_zen', kwargs=self.kw_translation),
         params
     )
     self.assertContains(
         response,
         'Following fixups were applied to translation: '
         'Trailing and leading whitespace'
     )
Exemplo n.º 11
0
 def test_save_zen_lock(self):
     self.component.locked = True
     self.component.save()
     unit = self.get_unit()
     params = {
         'checksum': unit.checksum,
         'contentsum': hash_to_checksum(unit.content_hash),
         'translationsum': hash_to_checksum(unit.get_target_hash()),
         'target_0': 'Zen translation',
         'review': '20',
     }
     response = self.client.post(
         reverse('save_zen', kwargs=self.kw_translation),
         params
     )
     self.assertContains(
         response, 'Insufficient privileges for saving translations.'
     )
Exemplo n.º 12
0
    def translate(self, language, text, unit, user):
        """Return list of machine translations."""
        if text == '':
            return []

        if self.is_rate_limited():
            return []

        language = self.convert_language(language)
        source = self.convert_language(
            unit.translation.subproject.project.source_language.code)
        if not self.is_supported(source, language):
            # Try without country code
            if '_' in language or '-' in language:
                language = language.replace('-', '_').split('_')[0]
                if source == language:
                    return []
                if not self.is_supported(source, language):
                    return []
            else:
                return []

        cache_key = None
        if self.cache_translations:
            cache_key = 'mt:{}:{}:{}:{}'.format(
                self.mtid,
                source,
                language,
                hash_to_checksum(calculate_hash(None, text)),
            )
            result = cache.get(cache_key)
            if result is not None:
                return result

        try:
            translations = self.download_translations(source, language, text,
                                                      unit, user)

            result = [{
                'text': trans[0],
                'quality': trans[1],
                'service': trans[2],
                'source': trans[3]
            } for trans in translations]
            if cache_key:
                cache.set(cache_key, result, 7 * 86400)
            return result
        except Exception as exc:
            if self.is_rate_limit_error(exc):
                self.set_rate_limit()

            self.report_error(
                exc,
                'Failed to fetch translations from %s',
            )
            raise MachineTranslationError('{0}: {1}'.format(
                exc.__class__.__name__, str(exc)))
Exemplo n.º 13
0
 def edit_unit(
     self,
     source: str,
     target: str,
     language: str = "cs",
     follow: bool = False,
     translation=None,
     **kwargs,
 ):
     """Do edit single unit using web interface."""
     unit = self.get_unit(source, language, translation=translation)
     params = {
         "checksum": unit.checksum,
         "contentsum": hash_to_checksum(unit.content_hash),
         "translationsum": hash_to_checksum(unit.get_target_hash()),
         "target_0": target,
         "review": "20",
     }
     params.update(kwargs)
     return self.client.post(unit.translation.get_translate_url(),
                             params,
                             follow=follow)
Exemplo n.º 14
0
def save_zen(request, project, component, lang):
    """Save handler for zen mode."""
    _obj, _project, unit_set = parse_params(request, project, component, lang)

    checksum_form = ChecksumForm(unit_set, request.POST)
    if not checksum_form.is_valid():
        show_form_errors(request, checksum_form)
        return HttpResponseBadRequest("Invalid checksum")

    unit = checksum_form.cleaned_data["unit"]
    translationsum = ""

    form = TranslationForm(request.user, unit, request.POST)
    if not form.is_valid():
        show_form_errors(request, form)
    elif not request.user.has_perm("unit.edit", unit):
        if request.user.has_perm("unit.flag", unit):
            update_explanation(unit, form)
        else:
            messages.error(
                request, _("Insufficient privileges for saving translations."))
    else:
        perform_translation(unit, form, request)

        translationsum = hash_to_checksum(unit.get_target_hash())

    response = {
        "messages": [],
        "state": "success",
        "translationsum": translationsum,
        "unit_state_class": unit_state_class(unit) if unit else "",
        "unit_state_title": unit_state_title(unit) if unit else "",
    }

    storage = get_messages(request)
    if storage:
        response["messages"] = [{
            "tags": m.tags,
            "kind": get_message_kind(m.tags),
            "text": m.message
        } for m in storage]
        tags = {m.tags for m in storage}
        if "error" in tags:
            response["state"] = "danger"
        elif "warning" in tags:
            response["state"] = "warning"
        elif "info" in tags:
            response["state"] = "info"

    return JsonResponse(data=response)
Exemplo n.º 15
0
 def __init__(self, user, translation, unit, *args, **kwargs):
     if unit is not None:
         kwargs['initial'] = {
             'checksum': unit.checksum,
             'contentsum': hash_to_checksum(unit.content_hash),
             'translationsum': hash_to_checksum(unit.get_target_hash()),
             'target': unit,
             'fuzzy': unit.fuzzy,
             'review': unit.state,
         }
         kwargs['auto_id'] = 'id_{0}_%s'.format(unit.checksum)
     tabindex = kwargs.pop('tabindex', 100)
     super(TranslationForm, self).__init__(
         translation, *args, **kwargs
     )
     self.user = user
     self.fields['target'].widget.attrs['tabindex'] = tabindex
     self.fields['target'].widget.profile = user.profile
     self.fields['review'].widget.attrs['class'] = 'review_radio'
     # Avoid failing validation on not translated string
     if args:
         self.fields['review'].choices.append((STATE_EMPTY, ''))
     self.helper = FormHelper()
     self.helper.form_method = 'post'
     self.helper.layout = Layout(
         Field('checksum'),
         Field('target'),
         Field('fuzzy'),
         Field('contentsum'),
         Field('translationsum'),
         InlineRadios('review'),
     )
     if unit and user.has_perm('unit.review', unit.translation):
         self.fields['fuzzy'].widget = forms.HiddenInput()
     else:
         self.fields['review'].widget = forms.HiddenInput()
Exemplo n.º 16
0
def save_zen(request, project, component, lang):
    """Save handler for zen mode."""
    def render_mesage(message):
        return render_to_string(
            'message.html',
            {'tags': message.tags, 'message': message.message}
        )

    translation = get_translation(request, project, component, lang)

    form = TranslationForm(
        request.user, translation, None, request.POST
    )
    translationsum = ''
    if not form.is_valid():
        show_form_errors(request, form)
    elif not request.user.has_perm('unit.edit', form.cleaned_data['unit']):
        messages.error(
            request, _('Insufficient privileges for saving translations.')
        )
    else:
        unit = form.cleaned_data['unit']

        perform_translation(unit, form, request)

        translationsum = hash_to_checksum(unit.get_target_hash())

    response = {
        'messages': '',
        'state': 'success',
        'translationsum': translationsum,
    }

    storage = get_messages(request)
    if storage:
        response['messages'] = '\n'.join([render_mesage(m) for m in storage])
        tags = {m.tags for m in storage}
        if 'error' in tags:
            response['state'] = 'danger'
        elif 'warning' in tags:
            response['state'] = 'warning'
        elif 'info' in tags:
            response['state'] = 'info'

    return JsonResponse(data=response)
Exemplo n.º 17
0
def save_zen(request, project, component, lang):
    """Save handler for zen mode."""
    def render_mesage(message):
        return render_to_string(
            'message.html',
            {'tags': message.tags, 'message': message.message}
        )

    translation = get_translation(request, project, component, lang)

    form = TranslationForm(
        request.user, translation, None, request.POST
    )
    translationsum = ''
    if not form.is_valid():
        show_form_errors(request, form)
    elif not request.user.has_perm('unit.edit', form.cleaned_data['unit']):
        messages.error(
            request, _('Insufficient privileges for saving translations.')
        )
    else:
        unit = form.cleaned_data['unit']

        perform_translation(unit, form, request)

        translationsum = hash_to_checksum(unit.get_target_hash())

    response = {
        'messages': '',
        'state': 'success',
        'translationsum': translationsum,
    }

    storage = get_messages(request)
    if storage:
        response['messages'] = '\n'.join([render_mesage(m) for m in storage])
        tags = {m.tags for m in storage}
        if 'error' in tags:
            response['state'] = 'danger'
        elif 'warning' in tags:
            response['state'] = 'warning'
        elif 'info' in tags:
            response['state'] = 'info'

    return JsonResponse(data=response)
Exemplo n.º 18
0
def save_zen(request, project, component, lang):
    """Save handler for zen mode."""
    translation = get_translation(request, project, component, lang)

    form = TranslationForm(request.user, translation, None, request.POST)
    unit = None
    translationsum = ""
    if not form.is_valid():
        show_form_errors(request, form)
    elif not request.user.has_perm("unit.edit", form.cleaned_data["unit"]):
        messages.error(request,
                       _("Insufficient privileges for saving translations."))
    else:
        unit = form.cleaned_data["unit"]

        perform_translation(unit, form, request)

        translationsum = hash_to_checksum(unit.get_target_hash())

    response = {
        "messages": [],
        "state": "success",
        "translationsum": translationsum,
        "unit_flags": get_state_css(unit) if unit is not None else [],
    }

    storage = get_messages(request)
    if storage:
        response["messages"] = [{
            "tags": m.tags,
            "text": m.message
        } for m in storage]
        tags = {m.tags for m in storage}
        if "error" in tags:
            response["state"] = "danger"
        elif "warning" in tags:
            response["state"] = "warning"
        elif "info" in tags:
            response["state"] = "info"

    return JsonResponse(data=response)
Exemplo n.º 19
0
def save_zen(request, project, component, lang):
    """Save handler for zen mode."""
    translation = get_translation(request, project, component, lang)

    form = TranslationForm(request.user, translation, None, request.POST)
    unit = None
    translationsum = ''
    if not form.is_valid():
        show_form_errors(request, form)
    elif not request.user.has_perm('unit.edit', form.cleaned_data['unit']):
        messages.error(request,
                       _('Insufficient privileges for saving translations.'))
    else:
        unit = form.cleaned_data['unit']

        perform_translation(unit, form, request)

        translationsum = hash_to_checksum(unit.get_target_hash())

    response = {
        'messages': [],
        'state': 'success',
        'translationsum': translationsum,
        'unit_flags': get_state_css(unit) if unit is not None else [],
    }

    storage = get_messages(request)
    if storage:
        response['messages'] = [{
            'tags': m.tags,
            'text': m.message
        } for m in storage]
        tags = {m.tags for m in storage}
        if 'error' in tags:
            response['state'] = 'danger'
        elif 'warning' in tags:
            response['state'] = 'warning'
        elif 'info' in tags:
            response['state'] = 'info'

    return JsonResponse(data=response)
Exemplo n.º 20
0
    def checksum(self):
        """Return unique hex identifier

        It's unsigned representation of id_hash in hex.
        """
        return hash_to_checksum(self.id_hash)
Exemplo n.º 21
0
def hash_text(name):
    """Hash text for use in HTML id."""
    return hash_to_checksum(siphash("Weblate URL hash", name.encode()))
Exemplo n.º 22
0
 def test_checksum(self):
     """Hash to checksum conversion."""
     text_hash = calculate_hash("Message")
     checksum = hash_to_checksum(text_hash)
     self.assertEqual(checksum, "f5351ff85ab23173")
     self.assertEqual(text_hash, checksum_to_hash(checksum))
Exemplo n.º 23
0
    def translate(self, language, text, unit, user):
        """Return list of machine translations."""
        if text == '':
            return []

        if self.is_rate_limited():
            return []

        language = self.convert_language(language)
        source = self.convert_language(
            unit.translation.component.project.source_language.code
        )
        if not self.is_supported(source, language):
            # Try without country code
            if '_' in language or '-' in language:
                language = language.replace('-', '_').split('_')[0]
                if source == language:
                    return []
                if not self.is_supported(source, language):
                    return []
            else:
                return []

        cache_key = None
        if self.cache_translations:
            cache_key = 'mt:{}:{}:{}'.format(
                self.mtid,
                calculate_hash(source, language),
                hash_to_checksum(calculate_hash(None, text)),
            )
            result = cache.get(cache_key)
            if result is not None:
                return result

        try:
            translations = self.download_translations(
                source, language, text, unit, user
            )

            result = [
                {
                    'text': trans[0],
                    'quality': trans[1],
                    'service': trans[2],
                    'source': trans[3]
                }
                for trans in translations
            ]
            if cache_key:
                cache.set(cache_key, result, 7 * 86400)
            return result
        except Exception as exc:
            if self.is_rate_limit_error(exc):
                self.set_rate_limit()

            self.report_error(
                exc,
                'Failed to fetch translations from %s',
            )
            raise MachineTranslationError('{0}: {1}'.format(
                exc.__class__.__name__,
                str(exc)
            ))
Exemplo n.º 24
0
    def checksum(self):
        """Return unique hex identifier

        It's unsigned representation of id_hash in hex.
        """
        return hash_to_checksum(self.id_hash)