Ejemplo n.º 1
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()

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            serializer.validated_data['file'],
            serializer.validated_data['overwrite'],
        )

        return Response(data={
            'not_found': not_found,
            'skipped': skipped,
            'accepted': accepted,
            'total': total,
            # Compatibility with older less detailed API
            'result': accepted > 0,
            'count': total,
        })
Ejemplo 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,
        })
Ejemplo 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)
                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,
        })
Ejemplo n.º 4
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,
            })
Ejemplo n.º 5
0
    def file(self, request, **kwargs):
        obj = self.get_object()
        project = obj.subproject.project
        if request.method == 'GET':
            fmt = self.format_kwarg or request.query_params.get('format')
            return download_translation_file(obj, fmt)

        if (not can_upload_translation(request.user, obj)
                or obj.subproject.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 can_overwrite_translation(request.user, project)):
            raise PermissionDenied()

        not_found, skipped, accepted, total = obj.merge_upload(
            request,
            request.data['file'],
            serializer.validated_data['overwrite'],
        )

        return Response(
            data={
                'not_found': not_found,
                'skipped': skipped,
                'accepted': accepted,
                'total': total,
                # Compatibility with older less detailed API
                'result': accepted > 0,
                'count': total,
            })
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)