Ejemplo n.º 1
0
def upload_translation(request, project, subproject, lang):
    '''
    Handling of translation uploads.
    '''
    obj = get_translation(request, project, subproject, lang)

    # Check method and lock
    if obj.is_locked(request.user):
        messages.error(request, _('Access denied.'))
        return redirect(obj)

    # Get correct form handler based on permissions
    form = get_upload_form(request.user, obj.subproject.project)(request.POST,
                                                                 request.FILES)

    # Check form validity
    if not form.is_valid():
        messages.error(request, _('Please fix errors in the form.'))
        return redirect(obj)

    # Create author name
    author = None
    if (can_author_translation(request.user, obj.subproject.project)
            and form.cleaned_data['author_name'] != ''
            and form.cleaned_data['author_email'] != ''):
        author = '%s <%s>' % (form.cleaned_data['author_name'],
                              form.cleaned_data['author_email'])

    # Check for overwriting
    overwrite = False
    if can_overwrite_translation(request.user, obj.subproject.project):
        overwrite = form.cleaned_data['overwrite']

    # Do actual import
    try:
        ret, count = obj.merge_upload(
            request,
            request.FILES['file'],
            overwrite,
            author,
            merge_header=form.cleaned_data['merge_header'],
            merge_comments=form.cleaned_data['merge_comments'],
            method=form.cleaned_data['method'],
            fuzzy=form.cleaned_data['fuzzy'],
        )
        messages.info(
            request,
            ungettext('Processed %d string from the uploaded files.',
                      'Processed %d strings from the uploaded files.', count) %
            count)
        if not ret:
            messages.warning(request,
                             _('There were no new strings in uploaded file!'))
    except Exception as error:
        messages.error(request,
                       _('File content merge failed: %s') % unicode(error))
        report_error(error, sys.exc_info(), request)

    return redirect(obj)
Ejemplo n.º 2
0
def get_upload_form(user, project):
    '''
    Returns correct upload form based on user permissions.
    '''
    if can_author_translation(user, project):
        return ExtraUploadForm
    elif can_overwrite_translation(user, project):
        return UploadForm
    else:
        return SimpleUploadForm
Ejemplo n.º 3
0
def get_upload_form(user, project):
    '''
    Returns correct upload form based on user permissions.
    '''
    if can_author_translation(user, project):
        return ExtraUploadForm
    elif can_overwrite_translation(user, project):
        return UploadForm
    else:
        return SimpleUploadForm
Ejemplo n.º 4
0
def get_upload_form(user, translation, *args):
    '''
    Returns correct upload form based on user permissions.
    '''
    project = translation.subproject.project
    if can_author_translation(user, project):
        form = ExtraUploadForm
    elif can_overwrite_translation(user, project):
        form = UploadForm
    else:
        form = SimpleUploadForm
    result = form(*args)
    if not can_translate(user, translation):
        result.remove_translation_choice('translate')
        result.remove_translation_choice('fuzzy')
    if not can_suggest(user, translation):
        result.remove_translation_choice('suggest')
    return result
Ejemplo n.º 5
0
def upload_translation(request, project, subproject, lang):
    '''
    Handling of translation uploads.
    '''
    obj = get_translation(request, project, subproject, lang)

    if not can_upload_translation(request.user, obj):
        raise PermissionDenied()

    # Check method and lock
    if obj.is_locked(request.user):
        messages.error(request, _('Access denied.'))
        return redirect(obj)

    # Get correct form handler based on permissions
    form = get_upload_form(request.user, obj.subproject.project)(
        request.POST, request.FILES
    )

    # Check form validity
    if not form.is_valid():
        messages.error(request, _('Please fix errors in the form.'))
        return redirect(obj)

    # Create author name
    author = None
    if (can_author_translation(request.user, obj.subproject.project) and
            form.cleaned_data['author_name'] != '' and
            form.cleaned_data['author_email'] != ''):
        author = '%s <%s>' % (
            form.cleaned_data['author_name'],
            form.cleaned_data['author_email']
        )

    # Check for overwriting
    overwrite = False
    if can_overwrite_translation(request.user, obj.subproject.project):
        overwrite = form.cleaned_data['overwrite']

    # Do actual import
    try:
        ret, count = obj.merge_upload(
            request,
            request.FILES['file'],
            overwrite,
            author,
            merge_header=form.cleaned_data['merge_header'],
            merge_comments=form.cleaned_data['merge_comments'],
            method=form.cleaned_data['method'],
            fuzzy=form.cleaned_data['fuzzy'],
        )
        import_message(
            request, count,
            _('No strings were imported from the uploaded file.'),
            ungettext(
                'Processed %d string from the uploaded files.',
                'Processed %d strings from the uploaded files.',
                count
            )
        )
        if not ret:
            messages.warning(
                request,
                _('There were no new strings in uploaded file!')
            )
    except Exception as error:
        messages.error(
            request, _('File content merge failed: %s') % force_text(error)
        )
        report_error(error, sys.exc_info(), request)

    return redirect(obj)