def media_upload(request): form_id = request.POST["form_id"] action = request.POST["action"] if action == "upload_file": files = request.FILES.getlist("file") if files: file_name = save_file_to_db(files[0], form_id) return JsonResponse({"status": "success", "filename": file_name}) else: return HttpResponseBadRequest elif action == "get_files": # Just return the files (including size and original name) associated with the form_id mediafiles = MediaFile.objects.filter(form_id=form_id) files = [] for item in mediafiles: entry = { "server_filename": (os.path.basename(item.file.name)), "original_filename": item.original_filename, "size": item.size } files.append(entry) return JsonResponse({"status": "success", "files": files}) elif action == "delete_file": server_filename = "./" + request.POST["server_filename"] f_object = MediaFile.objects.filter(file=server_filename, form_id=form_id) if f_object: f_object[0].file.delete() f_object[0].delete() return JsonResponse({"status": "success"}) else: return HttpResponseBadRequest
def post(self, request, format=None): # TODO: (for future releases) add API key rules serializer = FeedbackDetailSerializer(data=request.data) if serializer.is_valid(raise_exception=True): new_feedback = serializer.save() # save files in the same manner as it's done in feedback form if request.FILES: for filename, file in request.FILES.items(): save_file_to_db(file, new_feedback.service_request_id) files = MediaFile.objects.filter(form_id=new_feedback.service_request_id) if files: attach_files_to_feedback(request, new_feedback, files) response_data = { 'service_request_id': new_feedback.service_request_id, 'service_notice': '' } return Response(response_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)