def create_upload_task(self, key, size_limit=-1): r, s = self.client.create_upload_task(key, size_limit=size_limit) if s == WebStorageClientStatus.KEY_OCCUPIED: raise MessageException( 'Key has exist in WebStorage LargeFileStorage.create_upload_task() ' ) if (s != WebStorageClientStatus.KEY_OCCUPIED) and ( s != WebStorageClientStatus.SUCCESS): raise MessageException( 'Error in delete file list in LargeFileStorage.create_upload_task() ' ) return r
def list(self): r, s = self.client.list() if s != WebStorageClientStatus.SUCCESS: raise MessageException( 'Error in get file list in LargeFileStorage.list() ') #r 是文件列表 return r
def get_queryset(self): try: return FileModel.objects.all().order_by("-uploaddate") except MessageException as e: logger.error(traceback.format_exc()) raise e # rethrow exception except Exception as e: logger.error(traceback.format_exc()) raise MessageException('出现未知错误 !')
def create(self, request): try: serializerClass = self.get_serializer_class() serializer = serializerClass(data=request.data, context={'request': request, 'view': self}) if not serializer.is_valid(raise_exception=True): raise MessageException('保存出错!') instance = serializer.save() return Response(serializerClass(instance).data, status=status.HTTP_201_CREATED) except ValidationError as e: # 验证异常类 logger.error(traceback.print_exc()) first_validate_error_message = list(e.detail.values())[0][0] return Response({'error_message': first_validate_error_message}, status=status.HTTP_400_BAD_REQUEST) except MessageException as e: return Response({'error_message': str(e)}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.error(traceback.print_exc()) return Response({}, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, id): try: category_instance = self.get_queryset().get(id=id) seralizer_class = self.get_serializer_class() seralizer = seralizer_class(instance=category_instance, data=request.data) if not seralizer.is_valid(raise_exception=True): raise MessageException('data save error') instance = seralizer.save() return Response(seralizer_class(instance).data, status=status.HTTP_200_OK) except FileModel.DoesNotExist: return Response({'error_message': 'instance not find'}, status=status.HTTP_400_BAD_REQUEST) except ValidationError as e: logger.error(traceback.print_exc()) first_validate_error_message = list(e.detail.values())[0][0] return Response({'error_message': first_validate_error_message}, status=status.HTTP_400_BAD_REQUEST) except MessageException as e: return Response({'error_message': str(e)}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.error(traceback.print_exc()) return Response({'error_message': 'error in software'}, status=status.HTTP_400_BAD_REQUEST)
def delete(self, key): s = self.client.delete(key) if s != WebStorageClientStatus.SUCCESS: raise MessageException( 'Error in delete file list in LargeFileStorage.delete() ')
def get_queryset(self): try: return FileModel.objects.all() except Exception as e: logger.error(traceback.format_exc()) raise MessageException('出现未知错误 !')