原生sql

# 元组
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='文件不存在')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值