# 元组
def namedtuplefetchall(cursor):
"Return all rows from a cursor as a namedtuple"
desc = cursor.description
nt_result = namedtuple('Result', [col[0] for col in desc])
return [nt_result(*row) for row in cursor.fetchall()]
# 字典
def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]
# 字典类型
def redirect_laborder_list_v2_qa(request):
# QA 列表
qa_order_number = request.POST.get('qa_order_number', '')
print(qa_order_number)
sql = "SELECT accessories_type,object_url FROM qc_laborder_accessories WHERE lab_number = '{0}'"
qa_video_list = []
qa_img_list = []
qa_pdf_list = []
with connections['pg_oms_query'].cursor() as cursor:
sql = sql.format(qa_order_number)
cursor.execute(sql)
results = dictfetchall(cursor)
print(results)
if (results.__len__() != 0):
for i in range(len(results)):
type = str(results[i]["accessories_type"])
url = str(results[i]["object_url"])
if url.endswith('.jpg'):
qa_img_list.append(url)
elif url.endswith('.pdf'):
qa_pdf_list.append(url)
else:
qa_video_list.append(url)
else:
pass
return json_response(code=1,data={'qa_img_list':qa_img_list,
'qa_video_list':qa_video_list,
'qa_pdf_list':qa_pdf_list})
# 元组类型
def oms_file_download(request):
""" 下载QA文件 """
qa_url = request.GET.get('qa_url', '')
sql = "SELECT * from qc_laborder_accessories WHERE object_url ='{}';"
nowTime = "_"+ str(datetime.datetime.now())[-5:-1]
try:
with connections['pg_oms_query'].cursor() as cursor:
sql = sql.format(qa_url)
cursor.execute(sql)
results = namedtuplefetchall(cursor)
if (results.__len__()!=0):
file_path = str(results[0].object_url)
name = results[0].lab_number
type = results[0].accessories_type
res = requests.get(file_path)
response = HttpResponse(res, content_type="application/octet-stream")
from django.utils.encoding import escape_uri_path
if file_path.endswith('.jpg'):
response['Content-Disposition'] = "attachment; filename={};".format(escape_uri_path(name + nowTime + '.jpg'))
elif file_path.endswith('.pdf'):
response['Content-Disposition'] = "attachment; filename={};".format(escape_uri_path(name + nowTime + '.pdf'))
else:
response['Content-Disposition'] = "attachment; filename={};".format(escape_uri_path(name + nowTime + '.mp4'))
return response
else:
return json_response(code=-1, msg='文件不存在')
except Exception as e:
return json_response(code=-1,msg='文件不存在')
# 元组def namedtuplefetchall(cursor): "Return all rows from a cursor as a namedtuple" desc = cursor.description nt_result = namedtuple('Result', [col[0] for col in desc]) return [nt_result(*row) for row in cursor.fetchall()]# 字典def dictfe