Ejemplo n.º 1
0
def upload_file(request):
    account = request.session["now_account"]
    if request.method == 'POST':
        try:
            project_name = request.POST.get('project')
            module_name = request.POST.get('module')
        except KeyError as e:
            return JsonResponse({"status": e})

        if project_name == '请选择' or module_name == '请选择':
            return JsonResponse({"status": '项目或模块不能为空'})

        upload_path = os.path.split(os.path.realpath(__file__))[0] + separator + 'upload' + separator

        if os.path.exists(upload_path):
            shutil.rmtree(upload_path)

        os.mkdir(upload_path)

        upload_obj = request.FILES.getlist('upload')
        file_list = []
        for i in range(len(upload_obj)):
            temp_path = upload_path + upload_obj[i].name
            file_list.append(temp_path)
            try:
                with open(temp_path, 'wb') as data:
                    for line in upload_obj[i].chunks():
                        data.write(line)
            except IOError as e:
                return JsonResponse({"status": e})

        upload_file_logic(file_list, project_name, module_name, account)

        return JsonResponse({'status': '/api/test_list/1/'})
Ejemplo n.º 2
0
def upload_file(request):
    if request.session.get('login_status'):
        account = request.session["now_account"]
        separator = '\\' if platform.system() == 'Windows' else '/'
        if request.method == 'POST':
            try:
                project_name = request.POST.get('project')
                module_name = request.POST.get('module')
            except KeyError as e:
                return JsonResponse({"status": e})

            if project_name == '请选择' or module_name == '请选择':
                return JsonResponse({"status": '项目或模块不能为空'})

            upload_path = sys.path[0] + separator + 'upload' + separator

            if os.path.exists(upload_path):
                shutil.rmtree(upload_path)

            os.mkdir(upload_path)

            upload_obj = request.FILES.getlist('upload')
            file_list = []
            for i in range(len(upload_obj)):
                temp_path = upload_path + upload_obj[i].name
                file_list.append(temp_path)
                try:
                    with open(temp_path, 'wb') as data:
                        for line in upload_obj[i].chunks():
                            data.write(line)
                except IOError as e:
                    return JsonResponse({"status": e})

            upload_file_logic(file_list, project_name, module_name, account)

            return JsonResponse({'status': '/api/test_list/1/'})
        else:
            return HttpResponseRedirect("/api/login/")
Ejemplo n.º 3
0
def upload_file(request):
    account = request.session["now_account"]
    if request.method == 'POST':
        try:
            project_name = request.POST.get('project')
            module_name = request.POST.get('module')
        except KeyError as e:
            return JsonResponse({"status": e})

        if project_name == '请选择' or module_name == '请选择':
            return JsonResponse({"status": '项目或模块不能为空'})

        upload_path = sys.path[0] + separator + 'upload' + separator

        if os.path.exists(upload_path):
            shutil.rmtree(upload_path)

        os.mkdir(upload_path)

        excel_file = request.FILES['upload']
        type_excel = excel_file.name.split('.')[1]
        if 'xlsx' == type_excel or 'xls' == type_excel:
            try:
                # 开始处理上传的excel表格
                wb = xlrd.open_workbook(filename=None,
                                        file_contents=excel_file.read())
                table = wb.sheet_by_index(0)
                nrows = table.nrows  # 行数
                oec = ExcelCase.objects
                for rowCount in range(1, nrows):
                    ec = ExcelCase()
                    ec.author = account
                    name = table.cell_value(rowCount, 4)
                    count = oec.get_case_name(name, module_name, project_name)
                    if count < 1:
                        ec.belong_module = ModuleInfo.objects.get_module_name(
                            module_name, type=False)
                        ec.belong_project = project_name
                        # ec.sub_module_name = table.cell_value(rowCount, 1)
                        ec.case_level = table.cell_value(rowCount, 2)
                        ec.name = table.cell_value(rowCount, 4)
                        ec.case_condition = table.cell_value(rowCount, 5)
                        ec.operating_steps = table.cell_value(rowCount, 6)
                        ec.expect = table.cell_value(rowCount, 7)
                        ec.result = table.cell_value(rowCount, 9)
                        ec.save()
                        logger.info('{name}---用例添加成功'.format(name=name))
                    else:
                        logger.info('{name}---用例未导入因为已存在'.format(name=name))

            except IOError as e:
                return JsonResponse({"status": e})
            else:
                wb.release_resources()
                del wb
            return JsonResponse({'status': '/api/excel_test_list/1/'})
        else:
            upload_obj = request.FILES.getlist('upload')
            file_list = []
            for i in range(len(upload_obj)):
                temp_path = upload_path + upload_obj[i].name
                file_list.append(temp_path)
                try:
                    with open(temp_path, 'wb') as data:
                        for line in upload_obj[i].chunks():
                            data.write(line)
                except IOError as e:
                    return JsonResponse({"status": e})

            upload_file_logic(file_list, project_name, module_name, account)

            return JsonResponse({'status': '/api/test_list/1/'})