Exemple #1
0
 def update(self, request, *args, **kwargs):
     try:
         f = FileRepository.get(id=kwargs.get("pk"))
     except FileNotFoundException:
         return Response({"details": "Not Found"},
                         status=status.HTTP_404_NOT_FOUND)
     serializer = UpdateFileSerializer(f.file,
                                       data=request.data,
                                       context={"request": request},
                                       partial=request.method == "PATCH")
     if serializer.is_valid():
         serializer.save()
         f = FileRepository.get(id=kwargs.get("pk"))
         response = FileSerializer(f)
         return Response(response.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
 def retrieve(self, request, *args, **kwargs):
     try:
         f = FileRepository.get(kwargs["pk"])
     except FileNotFoundException as e:
         return Response({"details": str(e)},
                         status=status.HTTP_404_NOT_FOUND)
     serializer = FileSerializer(f)
     return Response(serializer.data, status=status.HTTP_200_OK)
Exemple #3
0
    def post(self, request):
        patch_files = request.data.get("patch_files", [])
        sid = transaction.savepoint()
        current_file_id = None
        current_file_data = None
        file_count = len(patch_files)
        try:
            for single_file_patch in patch_files:
                current_file_id = single_file_patch["id"]
                current_file_data = single_file_patch["patch"]
                f = FileRepository.get(id=current_file_id)
                serializer = UpdateFileSerializer(f.file,
                                                  data=current_file_data,
                                                  partial=True)
                if serializer.is_valid():
                    serializer.save()
                else:
                    transaction.savepoint_rollback(sid)
                    return Response(serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)
            transaction.savepoint_commit(sid)
        except FileNotFoundException:
            transaction.savepoint_rollback(sid)
            error_message = "File {} not found".format(current_file_id)
            return Response({"details": error_message},
                            status=status.HTTP_404_NOT_FOUND)
        except IntegrityError:
            transaction.savepoint_rollback(sid)
            error_message = "Integrity error"
            return Response({"details": error_message},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        except Exception as e:
            transaction.savepoint_rollback(sid)
            error_message = "An unexpected error occured: " + repr(e)
            return Response({"details": error_message},
                            status=status.HTTP_400_BAD_REQUEST)

        success_message = "Successfully updated {} files".format(file_count)
        return Response(success_message, status=status.HTTP_200_OK)