Exemplo n.º 1
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        user = request.user
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not user.has_perm('upload.perform', obj)
                or obj.component.locked):
            raise PermissionDenied()

        if 'file' not in request.data:
            raise ParseError('Missing file parameter')

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        if data['overwrite'] and not user.has_perm('upload.overwrite', obj):
            raise PermissionDenied()

        if (not user.has_perm('unit.edit', obj)
                and data['method'] in ('translate', 'fuzzy')):
            raise PermissionDenied()
        if (not user.has_perm('suggestion.add', obj)
                and data['method'] == 'suggest'):
            raise PermissionDenied()
        if (not user.has_perm('unit.review', obj)
                and data['method'] == 'approve'):
            raise PermissionDenied()

        author_name = None
        author_email = None
        if request.user.has_perm('upload.authorship', obj):
            author_name = data.get('author_name')
            author_email = data.get('author_email')

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            data['file'],
            data['overwrite'],
            author_name,
            author_email,
            data['method'],
            data['fuzzy'],
        )

        return Response(data={
            'not_found': not_found,
            'skipped': skipped,
            'accepted': accepted,
            'total': total,
            # Compatibility with older less detailed API
            'result': accepted > 0,
            'count': total,
        })
Exemplo n.º 2
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        user = request.user
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not user.has_perm('upload.perform', obj) or
                obj.component.locked):
            raise PermissionDenied()

        if 'file' not in request.data:
            raise ParseError('Missing file parameter')

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        if data['overwrite'] and not user.has_perm('upload.overwrite', obj):
            raise PermissionDenied()

        if (not user.has_perm('unit.edit', obj) and
                data['method'] in ('translate', 'fuzzy')):
            raise PermissionDenied()
        if (not user.has_perm('suggestion.add', obj) and
                data['method'] == 'suggest'):
            raise PermissionDenied()
        if (not user.has_perm('unit.review', obj) and
                data['method'] == 'approve'):
            raise PermissionDenied()

        author_name = None
        author_email = None
        if request.user.has_perm('upload.authorship', obj):
            author_name = data.get('author_name')
            author_email = data.get('author_email')

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            data['file'],
            data['overwrite'],
            author_name,
            author_email,
            data['method'],
            data['fuzzy'],
        )

        return Response(data={
            'not_found': not_found,
            'skipped': skipped,
            'accepted': accepted,
            'total': total,
            # Compatibility with older less detailed API
            'result': accepted > 0,
            'count': total,
        })
Exemplo n.º 3
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        user = request.user
        if request.method == "GET":
            fmt = self.format_kwarg or request.query_params.get("format")
            return download_translation_file(obj, fmt)

        if not user.has_perm("upload.perform", obj):
            raise PermissionDenied()

        if "file" not in request.data:
            raise ParseError("Missing file parameter")

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.check_perms(request.user, obj)

        data = serializer.validated_data

        author_name = None
        author_email = None
        if request.user.has_perm("upload.authorship", obj):
            author_name = data.get("author_name")
            author_email = data.get("author_email")

        try:
            not_found, skipped, accepted, total = obj.merge_upload(
                request,
                data["file"],
                data["overwrite"],
                author_name,
                author_email,
                data["method"],
                data["fuzzy"],
            )

            return Response(
                data={
                    "not_found": not_found,
                    "skipped": skipped,
                    "accepted": accepted,
                    "total": total,
                    # Compatibility with older less detailed API
                    "result": accepted > 0,
                    "count": total,
                })
        except Exception as error:
            report_error(cause="Upload error")
            return Response(data={
                "result": False,
                "detail": force_str(error)
            },
                            status=400)
Exemplo n.º 4
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        user = request.user
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not user.has_perm('upload.perform', obj) or obj.component.locked):
            raise PermissionDenied()

        if 'file' not in request.data:
            raise ParseError('Missing file parameter')

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.check_perms(request.user, obj)

        data = serializer.validated_data

        author_name = None
        author_email = None
        if request.user.has_perm('upload.authorship', obj):
            author_name = data.get('author_name')
            author_email = data.get('author_email')

        try:
            not_found, skipped, accepted, total = obj.merge_upload(
                request,
                data['file'],
                data['overwrite'],
                author_name,
                author_email,
                data['method'],
                data['fuzzy'],
            )

            return Response(
                data={
                    'not_found': not_found,
                    'skipped': skipped,
                    'accepted': accepted,
                    'total': total,
                    # Compatibility with older less detailed API
                    'result': accepted > 0,
                    'count': total,
                })
        except Exception as error:
            return Response(data={
                'result': False,
                'detail': force_text(error)
            },
                            status=400)
Exemplo n.º 5
0
def download_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)

    kwargs = {}

    if "format" in request.GET or "q" in request.GET:
        form = DownloadForm(request.GET)
        if not form.is_valid():
            show_form_errors(request, form)
            return redirect(obj)

        kwargs["units"] = obj.unit_set.search(form.cleaned_data.get("q", ""))
        kwargs["fmt"] = form.cleaned_data["format"]

    return download_translation_file(obj, **kwargs)
Exemplo n.º 6
0
def download_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)
    if not request.user.has_perm("translation.download", obj):
        raise PermissionDenied()

    kwargs = {}

    if "format" in request.GET or "q" in request.GET:
        form = DownloadForm(request.GET)
        if not form.is_valid():
            show_form_errors(request, form)
            return redirect(obj)

        kwargs["query_string"] = form.cleaned_data.get("q", "")
        kwargs["fmt"] = form.cleaned_data["format"]

    return download_translation_file(request, obj, **kwargs)
Exemplo n.º 7
0
def download_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)

    kwargs = {}

    if 'format' in request.GET or 'type' in request.GET:
        form = DownloadForm(request.GET)
        if not form.is_valid():
            show_form_errors(request, form)
            return redirect(obj)

        kwargs['units'] = obj.unit_set.search(
            form.cleaned_data,
            translation=obj,
        )
        kwargs['fmt'] = form.cleaned_data['format']

    return download_translation_file(obj, **kwargs)
Exemplo n.º 8
0
def download_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)

    kwargs = {}

    if 'format' in request.GET or 'type' in request.GET:
        form = DownloadForm(request.GET)
        if not form.is_valid():
            show_form_errors(request, form)
            return redirect(obj)

        kwargs['units'] = obj.unit_set.search(
            form.cleaned_data,
            translation=obj,
        )
        kwargs['fmt'] = form.cleaned_data['format']

    return download_translation_file(obj, **kwargs)
Exemplo n.º 9
0
def download_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)

    kwargs = {}

    if "format" in request.GET or "q" in request.GET:
        form = DownloadForm(request.GET)
        if not form.is_valid():
            show_form_errors(request, form)
            return redirect(obj)

        kwargs["units"] = (obj.unit_set.search(
            form.cleaned_data.get("q", ""),
            project=obj.component.project).distinct().order_by(
                "position").prefetch_full())
        kwargs["fmt"] = form.cleaned_data["format"]

    return download_translation_file(request, obj, **kwargs)
Exemplo n.º 10
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not request.user.has_perm('upload.perform', obj)
                or obj.component.locked):
            raise PermissionDenied()

        if 'file' not in request.data:
            raise ParseError('Missing file parameter')

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        if (serializer.validated_data['overwrite']
                and not request.user.has_perm('upload.overwrite', obj)):
            raise PermissionDenied()

        can_author = request.user.has_perm('upload.authorship', obj)

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            serializer.validated_data['file'],
            serializer.validated_data['overwrite'],
            serializer.validated_data.get('email', None)
            if can_author else None,
            serializer.validated_data.get('author', None)
            if can_author else None,
        )

        return Response(
            data={
                'not_found': not_found,
                'skipped': skipped,
                'accepted': accepted,
                'total': total,
                # Compatibility with older less detailed API
                'result': accepted > 0,
                'count': total,
            })
Exemplo n.º 11
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not request.user.has_perm('upload.perform', obj) or
                obj.component.locked):
            raise PermissionDenied()

        if 'file' not in request.data:
            raise ParseError('Missing file parameter')

        serializer = UploadRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        if (serializer.validated_data['overwrite'] and
                not request.user.has_perm('upload.overwrite', obj)):
            raise PermissionDenied()

        can_author = request.user.has_perm('upload.authorship', obj)

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            serializer.validated_data['file'],
            serializer.validated_data['overwrite'],
            serializer.validated_data.get('email', None) if can_author else None,
            serializer.validated_data.get('author', None) if can_author else None,
        )

        return Response(data={
            'not_found': not_found,
            'skipped': skipped,
            'accepted': accepted,
            'total': total,
            # Compatibility with older less detailed API
            'result': accepted > 0,
            'count': total,
        })