Exemple #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):
            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)
Exemple #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)
        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)