from django.http import JsonResponse, StreamingHttpResponse, HttpResponse from django.shortcuts import render from django.utils.safestring import mark_safe from chart import charts from chart.models import PlottingLibrary from chart.tools import * # 处理页面返回问题 页面重定向 def out_page(request, page): if page in ["index.html", 'about.html', 'tools.html']: return render(request, f'./page/{page}', {"url": url_}) # 验证访问的网页是否存在 防止eval漏洞 temp = page.replace("_", ' ') print(temp.replace('.html', '')) db = PlottingLibrary.objects.get(name=f"{temp.replace('.html', '')}") print(temp) if db is None: return HttpResponse("no") else: a = ChartParameter(db.parameter) data = { 'show_img': f"/{db.show_img}.png", 'name': db.name.replace(' ', '_'), 'title': db.name, 'Basics': mark_safe(a.html_out_Basics()), 'Senior': mark_safe(a.html_out_Senior()), 'example_file': str(BASE_DIR) + db.example_file, 'js': mark_safe(a.js_out()), 'url': url_, "color": mark_safe(a.color_js()) } return render(request, f'./chart/Basic.html', data) # 处理上传文件 返回颜色列表 def up_file(request): file = request.FILES.get("file") file_path, file_id = path_out("chart") keep_file(file, file_path) data = {"flag": file_id} return JsonResponse(data) def download(request): file_name = request.GET.get("name", None) sort = request.GET.get("sort", "chart") ee = request.GET.get("ee", None) if sort == "tools": file_id = request.GET.get("file_id") # 工作编号 name_path = os.path.join(BASE_DIR, "file_keep", "tools", "file", f"{file_id}") in_file = f"out.txt" elif ee == "1": file_downlode_type = request.GET.get("file_downlode_type") # 文件类型 file_id = request.GET.get("file_id") # 工作编号 name_path = os.path.join(BASE_DIR, "file_keep", "chart", "file", f"{file_id}.{file_downlode_type}") in_file = f"out.{file_downlode_type}" elif file_name is None: file_downlode_type = request.GET.get("file_downlode_type") # 文件类型 file_id = request.GET.get("file_id") # 工作编号 name_path = os.path.join(BASE_DIR, "file_keep", "chart", "img", f"{file_id}.{file_downlode_type}") in_file = f"out.{file_downlode_type}" else: name_path = os.path.join( BASE_DIR, "file_keep", "eg_file", "img", file_name) in_file = file_name print(name_path) response = StreamingHttpResponse(readFile(name_path)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="{0}"'.format( in_file) return response def readFile(filename, chunk_size=512): with open(filename, 'rb') as f: while True: c = f.read(chunk_size) if c: yield c else: break def chart_run(request, get_chart_sort): chart_name = get_chart_sort.replace("_run", '') file_id = request.GET.get("file_id") k = request.GET.get("k") data = {} temp = chart_name.replace("_", ' ') db = PlottingLibrary.objects.get(name=temp) if db is None: return HttpResponse("no") for i in request.GET.lists(): if i[0] not in ['_', 'file_id', 'k']: data[i[0]] = i[1][0] file_path = path_out("chart", file_id) # 返回文件路径 chart_out_path = path_out("chart", file_id, show=1) # 返回图片路径 out_path = path_out("chart", file_id, out=1) # 返回图片路径 # file_path 输入文件的路径 file # chart 展示图片的路径 # out_path 输出文件的路径 print(data) eval(f'charts.{chart_name}')(file_path[0], f"{chart_out_path}_{k}.png", out_path, **data) return JsonResponse({"img": f"/static/img_eg/show_chart/{file_id}_{k}.png"})