Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Feat download #107

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ApiManager/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import logging
import os
import platform
import tarfile
from json import JSONDecodeError

import yaml
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "读写异常,请重试"
2 changes: 1 addition & 1 deletion ApiManager/utils/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
38 changes: 37 additions & 1 deletion ApiManager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

5 changes: 5 additions & 0 deletions templates/module_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@
data-am-popover="{content: '删除', trigger: 'hover focus'}"
onclick="invalid('{{ foo.id }}')"><span
class="am-icon-trash-o"></span></button>
<button type="button"
class="am-btn am-btn-default am-btn-xs am-text-danger am-round"
data-am-popover="{content: '批量导出', trigger: 'hover focus'}"
onclick="post('/api/file_down/',{'id':'{{ foo.id }}','type':'module'})"><span
class="am-icon-download"></span></button>
</div>
</div>
</td>
Expand Down
6 changes: 6 additions & 0 deletions templates/project_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<button type="button" class="am-btn am-btn-danger am-round am-btn-xs am-icon-bug"
onclick="run_test('batch', '/api/run_batch_test/', 'project')">运行
</button>

</dl>
</div>

Expand Down Expand Up @@ -251,6 +252,11 @@
data-am-popover="{content: '删除', trigger: 'hover focus'}"
onclick="invalid('{{ foo.id }}')"><span
class="am-icon-trash-o"></span></button>
<button type="button"
class="am-btn am-btn-default am-btn-xs am-text-danger am-round"
data-am-popover="{content: '批量导出', trigger: 'hover focus'}"
onclick="post('/api/file_down/',{'id':'{{ foo.id }}','type':'project'})"><span
class="am-icon-download"></span></button>
</div>
</div>
</td>
Expand Down