导入校验能力

This commit is contained in:
caoqianming 2021-07-29 10:46:29 +08:00
parent 064e44f6fc
commit bc2e397af3
1 changed files with 72 additions and 18 deletions

View File

@ -391,28 +391,20 @@ class CorrectViewSet(RecordMixin, ModelViewSet):
ret.append({'text':i[group_by]+'('+ str(i['count']) +')','value':i[group_by]})
return Response(ret)
@action(methods=['post'], detail=False)
@action(methods=['post'], detail=False, url_path="import")
def correct_import(self, request, pk=None):
"""
导入校验能力
"""
filepath = request.data['path']
fullpath = settings.BASE_DIR + filepath
import os
if fullpath.endswith('.zip'):
fulldir = fullpath.replace('.zip','')
os.mkdir(fulldir)
os.chdir(fulldir)
# CMA.objects.filter(type='sub').delete()
with zipfile.ZipFile(fullpath,'r') as zzz:
zzz.extractall(fulldir)
for root, dirs, files in os.walk(fulldir):
for f in files:
if f.endswith('.xlsx'):
import_inspection(f.encode('cp437').decode('gbk'), os.path.join(root,f))
else:
return Response('不支持非xlsx格式', status = status.HTTP_400_BAD_REQUEST)
return Response(status = status.HTTP_200_OK)
if fullpath.endswith('.xlsx'):
ret = import_correct(fullpath)
if ret[0]:
return Response()
return Response(ret[1], status=status.HTTP_400_BAD_REQUEST)
else:
return Response('不支持非xlsx格式', status = status.HTTP_400_BAD_REQUEST)
def import_qualification(path):
wb = load_workbook(path)
@ -645,8 +637,70 @@ def import_inspection(filename, path):
i = i + 1
Inspection.objects.bulk_create(datalist)
def import_correct(filename, path):
pass
def import_correct(path):
wb = load_workbook(path,data_only=True)
sheet = wb.worksheets[0]
datalist = []
ssgs = sheet['k3'].value
try:
ssbm = Organization.objects.get(name=ssgs)
except:
return False , ssgs+'不存在'
if Correct.objects.filter(ssbm=ssbm).exists():
Correct.objects.filter(ssbm=ssbm).delete()
i = 3
max_row = sheet.max_row
defaultv = {}
while i<max_row+1:
data = {}
if sheet['a'+str(i)].value:
data['dlxh'] = sheet['a'+str(i)].value
defaultv['dlxh'] = data['dlxh']
else:
data['dlxh'] = defaultv['dlxh']
if sheet['b'+str(i)].value:
data['dlmc'] = sheet['b'+str(i)].value
defaultv['dlmc'] = data['dlmc']
else:
data['dlmc'] = defaultv['dlmc']
if sheet['c'+str(i)].value:
data['lbxh'] = sheet['c'+str(i)].value
defaultv['lbxh'] = data['lbxh']
else:
data['lbxh'] = defaultv['lbxh']
if sheet['d'+str(i)].value:
data['lbmc'] = sheet['d'+str(i)].value
defaultv['lbmc'] = data['lbmc']
else:
data['lbmc'] = defaultv['lbmc']
if sheet['e'+str(i)].value:
data['bclxh'] = sheet['e'+str(i)].value
defaultv['bclxh'] = data['bclxh']
else:
data['bclxh'] = defaultv['bclxh']
if sheet['f'+str(i)].value:
data['bclmc'] = sheet['f'+str(i)].value
defaultv['bclmc'] = data['bclmc']
else:
data['bclmc'] = defaultv['bclmc']
if sheet['g'+str(i)].value:
data['jzgc'] = sheet['g'+str(i)].value
defaultv['jzgc'] = data['jzgc']
else:
data['jzgc'] = defaultv['jzgc']
if sheet['h'+str(i)].value:
data['clfw'] = sheet['h'+str(i)].value
defaultv['clfw'] = data['clfw']
else:
data['clfw'] = defaultv['clfw']
data['zqddj'] = sheet['i'+str(i)].value if (sheet['i'+str(i)].value and sheet['i'+str(i)].value !='') else None
data['note'] = sheet['j'+str(i)].value if (sheet['j'+str(i)].value and sheet['j'+str(i)].value !='') else None
data['ssgs'] = ssgs
data['ssbm'] = ssbm
datalist.append(Correct(**data))
i = i + 1
Correct.objects.bulk_create(datalist)
return True, ''
from django.db.models.functions import Cast
def merge_cnas(request):