From b088e3d0bf69dd6541ea7efb86a8ad3e6225894d Mon Sep 17 00:00:00 2001 From: guohongze Date: Fri, 10 May 2019 16:12:14 +0800 Subject: [PATCH] fix source export charter --- branches/resource.py | 54 ++++++++------------------------------------ cmdb/api.py | 8 +++++++ 2 files changed, 18 insertions(+), 44 deletions(-) diff --git a/branches/resource.py b/branches/resource.py index 0565b9f9..ff087ca5 100644 --- a/branches/resource.py +++ b/branches/resource.py @@ -9,7 +9,7 @@ from accounts.permission import permission_verify import csv import datetime -from cmdb.api import str2gb +from cmdb.api import str2gb, str2gbk @login_required() @@ -74,42 +74,10 @@ def resource_edit(request, resource_id): } return render(request, 'branches/resource_base.html', results) - -@login_required -@permission_verify() -def resource_export(request): - export = request.GET.get("export", '') - resource_id_list = request.GET.getlist("id", '') - if export == "part": - if resource_id_list: - resource_find = [] - for resource_id in resource_id_list: - resource_item = Resource.objects.get(id=resource_id) - if resource_item: - resource_find.append(resource_item) - - if export == "all": - resource_find = Resource.objects.all() - - response = HttpResponse(content_type='text/csv') - now = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M') - file_name = 'adminset_resource_' + now + '.csv' - response['Content-Disposition'] = "attachment; filename="+file_name - writer = csv.writer(response) - writer.writerow([str2gb(u'资源名称'), str2gb(u'资源规格'), str2gb(u'预算金额'), str2gb(u'合同金额'), - str2gb(u'合同编号'), str2gb(u'合同开始'), str2gb(u'合同结束'), str2gb(u'供应商名'), - str2gb(u'服务电话'), str2gb(u'所属机构'), str2gb(u'备注说明')]) - for p in resource_find: - writer.writerow([str2gb(p.name), str2gb(p.spec), str2gb(p.budget), str2gb(p.paid), str2gb(p.contract), - str2gb(p.contract_start),str2gb(p.contract_end), str2gb(p.suppier), str2gb(p.service_phone), - str2gb(p.branch), str2gb(p.description)]) - return response - - @login_required @permission_verify() def resource_export(request): - resource = Resource.objects.all() + resource_find = Resource.objects.all() response = HttpResponse(content_type='text/csv') now = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M') file_name = 'adminset_resource_' + now + '.csv' @@ -117,18 +85,16 @@ def resource_export(request): writer = csv.writer(response) writer.writerow([str2gb('行政区域'), str2gb('分支机构'), str2gb('资源编码'), str2gb('资源名称'), str2gb('资源规格'), str2gb('预算资金'), str2gb('合同资金'), str2gb('合同编号'), str2gb('合同开始'), str2gb('合同结束'), - str2gb('供应商名'), str2gb('服务电话'), str2gb('客户经理'), str2gb('联系电话'), - str2gb('备注说明')]) - for r in resource: + str2gb('供应商名'), str2gb('服务电话'), str2gb('客户经理'), str2gb('联系电话'),]) + for r in resource_find: + br_name = "" if r.branch: br = Branch.objects.get(name=r.branch) - else: - br = None - - writer.writerow([str2gb(br.region.name if br else br), str2gb(r.branch.name if r.branch else None), r.sn, - str2gb(r.name), str2gb(r.spec),str2gb(r.budget), str2gb(r.paid), str2gb(r.contract), + if br.region: + br_name = br.region.name + writer.writerow([str2gb(br_name), str2gbk(r.branch.name if r.branch else None), str2gb(r.sn), + str2gb(r.name), str2gb(r.spec), str2gb(r.budget), str2gb(r.paid), str2gb(r.contract), str2gb(r.contract_start), str2gb(r.contract_end), str2gb(r.supplier), str2gb(r.service_phone), str2gb(r.owner if r.owner else None), - str2gb(r.owner.phone if r.owner else None), - str2gb(r.description)]) + str2gb(r.owner.phone if r.owner else None),]) return response diff --git a/cmdb/api.py b/cmdb/api.py index 72a2b849..28358e23 100644 --- a/cmdb/api.py +++ b/cmdb/api.py @@ -25,6 +25,14 @@ def str2gb(args): """ return str(args).encode('gb2312') +def str2gbk(args): + """ + :参数 args: + :返回: GB2312编码 + """ + return str(args).encode('gbk') + + def str2gb2utf8(args): """