Files
Simple_Drawing/chart/views.py
2021-09-07 23:12:03 +08:00

150 lines
4.5 KiB
Python

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 *
url_ = 'http://127.0.0.1:8000'
# 处理页面返回问题 页面重定向
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': db.name.replace(" ", "_"),
'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")
col_name = request.POST.get("col_name", "color")
if col_name == "NO":
file_path, file_id = path_out("chart")
keep_file(file, file_path)
data = {"flag": file_id}
return JsonResponse(data)
file_path, file_id = path_out("chart")
keep_file(file, file_path)
col_list = pick_col(file_path, col_name)
data = {"flag": file_id, 'data': [{"title": f"color_{i}", 'name': i, } for i in col_list][:8]}
return JsonResponse(data)
def download(request):
file_name = request.GET.get("name", None)
sort = request.GET.get("sort", "chart")
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 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 输出文件的路径
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"})
def tools(request):
file = request.FILES.get("file", None) # 标题
mg = request.POST.get('mg', None)
data = ""
if file is None and mg is None:
return JsonResponse({"http": "内容为空"})
if file is None:
data = 反向互补序列(mg)
else:
p = file.read()
p.decode(encoding='utf-8')
data = 反向互补序列(p.decode(encoding='utf-8'))
file_path, file_id = path_out("tools")
keep_file(data, file_path, ui=1)
return JsonResponse({"work_id": file_id})