def post(self, request): data = JSONParser().parse(request) serializer = MemorandumCreateSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, code=200, message="创建成功") return JSONResponse(serializer.errors, code=400, message="创建失败")
def delete(self, request, pk=None): try: file = File.objects.get(pk=pk) file.file.delete() file.delete() return JSONResponse(data=None, message='删除成功') except File.DoesNotExist: return JSONResponse(data=None, message='不存在的资源', code=404)
def post(self, request): data = { 'file': request.FILES['file'], 'name': request.FILES['file'].name } form = FileUploadSerializer(data=data) if form.is_valid(): form.save() return JSONResponse(data=form.data, message='上传成功') return JSONResponse(data=form.errors, message='上传失败', code=400)
def get(self, request): dirs = dirTree("C:/Users/24224/OneDrive/文章", [], "") result = { 'result': dirs, } return JSONResponse(result)
def handleDetail(self, request, pk=None): memorandum = Memorandum.objects.get(pk=pk) datas = MemorandumDetailSerializer(memorandum, many=False) result = { 'result': datas.data, } return JSONResponse(result)
def delete(self, request, pk=None): try: memorandum = Memorandum.objects.get(pk=pk) memorandum.delete() return JSONResponse(None, status=200, message="删除成功") except Memorandum.DoesNotExist: return HttpResponse(status=404)
def get(self, request, pk=None): queryset = Memorandum.objects.all().filter(category=pk).order_by('pk') datas = MemorandumSerializer(queryset, many=True) result = { 'result': datas.data, } return JSONResponse(result)
def get(self, request): queryset = MemorandumCategory.objects.all().order_by('pk') datas = MemorandumCategorySerializer(queryset, many=True) result = { 'result': datas.data, } return JSONResponse(result)
def handleDetail(self, request): try: filename = request.query_params['filename'] file = open(os.path.join("C:/Users/24224/OneDrive/文章", filename), 'r', encoding='utf-8') result = { 'result': file.read(), } return JSONResponse(result) finally: file.close()
def update(self, request, pk=None): data = JSONParser().parse(request) try: note = Memorandum.objects.get(pk=pk) note.content = data.get("content") note.save() return JSONResponse(MemorandumSerializer(note).data, code=200, message="修改成功") except Memorandum.DoesNotExist: return HttpResponse(status=404)
def downfile(self, request, pk=None): try: file = File.objects.get(pk=pk) response = FileResponse(file.file) arr = os.path.basename(file.file.name).split('.') ext = arr[-1] if len(arr) > 2: filename = '.'.join(str(item) for item in arr[0:-2]) else: filename = arr[0] response['content_type'] = "application/octet-stream" response[ 'Content-Disposition'] = 'attachment; filename=' + '{}.{}'.format( filename, ext) return response except File.DoesNotExist: return JSONResponse(data=None, message='不存在的资源', code=404)
def list(self, request): files = File.objects.all() return JSONResponse(data=FileListSerializer(files, many=True).data)