diff --git a/ApiManager/utils/common.py b/ApiManager/utils/common.py index 2d50f75..2dc5891 100644 --- a/ApiManager/utils/common.py +++ b/ApiManager/utils/common.py @@ -3,7 +3,7 @@ import json import logging import os -import platform +import tarfile from json import JSONDecodeError import yaml @@ -13,7 +13,7 @@ from ApiManager.models import ModuleInfo, TestCaseInfo, TestReports, TestSuite from ApiManager.utils.operation import add_project_data, add_module_data, add_case_data, add_config_data, \ - add_register_data + add_register_data, env_data_logic from ApiManager.utils.task_opt import create_task @@ -548,6 +548,14 @@ def upload_file_logic(files, project, module, account): if 'config' in test_case.keys(): test_case.get('config')['config_info'] = test_dict add_config_data(type=True, **test_case) + if test_case['config'].get('request').get('base_url', '') is '': + pass + else: + env_config = dict() + env_config['index'] = 'add' + env_config['env_name'] = test_case['config']['name'] + env_config['base_url'] = test_case['config']['request']['base_url'] + env_data_logic(**env_config) if 'test' in test_case.keys(): # 忽略config test_case.get('test')['case_info'] = test_dict @@ -644,3 +652,11 @@ def timestamp_to_datetime(summary, type=True): except Exception: pass return summary + + +def make_targz(output_filename, source_dir): + try: + with tarfile.open(output_filename, "w:gz") as tar: + tar.add(source_dir, arcname=os.path.basename(source_dir)) + except IOError: + return "读写异常,请重试" diff --git a/ApiManager/utils/testcase.py b/ApiManager/utils/testcase.py index c7acea6..aa6bfbd 100644 --- a/ApiManager/utils/testcase.py +++ b/ApiManager/utils/testcase.py @@ -18,7 +18,7 @@ def dump_yaml_file(yaml_file, data): """ load yaml file and check file content format """ with io.open(yaml_file, 'w', encoding='utf-8') as stream: - yaml.dump(data, stream, indent=4, default_flow_style=False, encoding='utf-8') + yaml.dump(data, stream, indent=4, default_flow_style=False, encoding='utf-8', allow_unicode=True) def _dump_json_file(json_file, data): diff --git a/ApiManager/views.py b/ApiManager/views.py index b9752f9..b275ac3 100644 --- a/ApiManager/views.py +++ b/ApiManager/views.py @@ -17,7 +17,7 @@ from ApiManager.tasks import main_hrun from ApiManager.utils.common import module_info_logic, project_info_logic, case_info_logic, config_info_logic, \ set_filter_session, get_ajax_msg, register_info_logic, task_logic, load_modules, upload_file_logic, \ - init_filter_session, get_total_values, timestamp_to_datetime + init_filter_session, get_total_values, timestamp_to_datetime, make_targz from ApiManager.utils.operation import env_data_logic, del_module_data, del_project_data, del_test_data, copy_test_data, \ del_report_data, add_suite_data, copy_suite_data, del_suite_data, edit_suite_data, add_test_reports from ApiManager.utils.pagination import get_pager_info @@ -259,6 +259,7 @@ def run_batch_test(request): if request.is_ajax(): kwargs = json.loads(request.body.decode('utf-8')) test_list = kwargs.pop('id') + print(test_list) base_url = kwargs.pop('env_name') type = kwargs.pop('type') report_name = kwargs.get('report_name', None) @@ -799,3 +800,38 @@ def echo(request): for i, line in enumerate(stdout): request.websocket.send(bytes(line, encoding='utf8')) client.close() + + +def file_down(request): + if os.path.exists(os.path.join(os.getcwd(), "download")): + shutil.rmtree(os.path.join(os.getcwd(), "download")) + download_dir_path = os.path.join(os.getcwd(), "download") + zip_dir_path = os.path.join(download_dir_path, get_time_stamp()) + the_file_name = 'export.tar.gz' + if request.method == 'POST': + kwargs = request.POST + id = kwargs.get('id') + base_url = kwargs.get('env_name', '') + type = kwargs.get('type') + run_test_by_type(id, base_url, zip_dir_path, type) + status = make_targz(download_dir_path+'/'+the_file_name, zip_dir_path) + if status != None: + if type == 'project': + return HttpResponse(get_ajax_msg(status, 'ok')) + else: + return HttpResponse(get_ajax_msg(status, 'ok')) + + def file_iterator(file_name, chunk_size=512): + with open(file_name, 'rb') as f: + while True: + c = f.read(chunk_size) + if c: + yield c + else: + break + response = StreamingHttpResponse(file_iterator(download_dir_path+'/'+the_file_name)) + response['Content-Type'] = 'application/octet-stream' + # 如果文件名中带有中文,必须使用如下代码进行编码,否则会使用默认名字 + response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(the_file_name) + return response + diff --git a/templates/module_list.html b/templates/module_list.html index e876309..7f2efcd 100644 --- a/templates/module_list.html +++ b/templates/module_list.html @@ -232,6 +232,11 @@ data-am-popover="{content: '删除', trigger: 'hover focus'}" onclick="invalid('{{ foo.id }}')"> + diff --git a/templates/project_list.html b/templates/project_list.html index 5f7ef25..de4e6d4 100644 --- a/templates/project_list.html +++ b/templates/project_list.html @@ -159,6 +159,7 @@ + @@ -251,6 +252,11 @@ data-am-popover="{content: '删除', trigger: 'hover focus'}" onclick="invalid('{{ foo.id }}')"> +