diff --git a/server/apps/ability/views.py b/server/apps/ability/views.py index c79e214..824de1c 100644 --- a/server/apps/ability/views.py +++ b/server/apps/ability/views.py @@ -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