def handle_uploaded_file(file, file_name): """ 弃用 """ length = len(file_name) if file_name[length - 4: length] == '.csv': DIR = DATA_DIR elif file_name[length - 4: length] == '.jmx': DIR = JMX_DIR else: return ResponseEntity('error', status.HTTP_400_BAD_REQUEST, errmsg='文件类型后缀错误') file_path = DIR + '/' + file_name try: f = open(file_path, 'wb+') if f.writable() is False: return ResponseEntity('error', status.HTTP_400_BAD_REQUEST, errmsg='无法写入') f.write(file) return ResponseEntity('success') except Exception as e: # print(e) return ResponseEntity('error', status.HTTP_400_BAD_REQUEST, errmsg=e) finally: f.close()
def get(self, request): """ """ queryset = Task.objects.all() serializer = TaskSerializer(queryset, many=True) # return response.Response(serializer.data) return json_response.JsonResponse(ResponseEntity(serializer.data))
def put(self, request, pk): """ """ queryset = Task.objects.all() task = get_object_or_404(queryset, pk=pk) serializer = TaskSerializer(task) return json_response.JsonResponse(ResponseEntity(serializer.data))
def upload(self): file_path = self.file_path try: f = open(file_path, 'wb+') if f.writable() is False: return ResponseEntity('error', status.HTTP_400_BAD_REQUEST, errmsg='无法写入') f.write(self.file) self._save_file(file_path) return ResponseEntity('success') except Exception as e: # print(e) return ResponseEntity('error', status.HTTP_400_BAD_REQUEST, errmsg=e.args) finally: f.close()
def get(self, request, name=None): """ """ queryset = Files.objects.all() file = get_object_or_404(queryset, name=name) """ 到这里说明可以找到file """ file_u = file_upload.FileUtil(name) """ 当文件状态属性更新时同时更新数据库 """ if file_u.is_exists is not file.status: file.status = file_u.is_exists file.save() serializer = FilesSerializer(file) return json_response.JsonResponse(ResponseEntity(serializer.data))
def form_errors(self): return ResponseEntity(self.form.data, status_code.HTTP_400_BAD_REQUEST, FormErrors.errors_message(self.form))
def get(self, request): """ """ queryset = Files.objects.all() serializer = FilesSerializer(queryset, many=True) return json_response.JsonResponse(ResponseEntity(serializer.data))