import datetime import os from django.http import HttpResponse from rest_framework.views import APIView import util_doc from TestLaboratory.settings import SOFTWARE_ROOT from apps.software.models import Software import logging from apps.task.models import BeidouCase from apps.user.models import User from util_xlsx import write_to_xlsx logger = logging.getLogger('django') class Report(APIView): authentication_classes = [] @staticmethod def convert_to_string(word_list): for i in range(len(word_list)): if word_list[i] is None: word_list[i] = '' elif not isinstance(word_list[i], str): word_list[i] = str(word_list[i]) return word_list # generate report @staticmethod # @RoleControl def get(request, software_id, *args, **kwargs): software = Software.objects.filter(id=software_id, delete=False) if not software: logger.error("软件已删除或不存在") return HttpResponse(status=404, content='软件已删除或不存在') software = software[0] beidou_cases = BeidouCase.objects.order_by('-create_time') beidou_cases = beidou_cases.filter(software_id=software_id) version = request.GET.get('version') if version: beidou_cases = beidou_cases.filter(version=version) export_type = request.GET.get('export_type') file_name = software.name + '-全部测试用例.xls' if export_type: if export_type == '2': file_name = software.name + '-不合格测试用例.xls' beidou_cases = beidou_cases.filter(state=2) if not beidou_cases: logger.error("无符合要求的测试用例") return HttpResponse(status=404, content='无符合要求的测试用例') update_time = datetime.datetime.now() file_dir = SOFTWARE_ROOT + "/" + str(update_time) if not os.path.exists(file_dir): os.makedirs(file_dir) file_path = file_dir + "/" + file_name cases_info = [ ['测试项目', '版本', '测试内容', '定位精度', '定时精度', '伪距测量精度', '载波测量精度', '测速精度', '捕获灵敏度', '接收灵敏度', '跟踪灵敏度', '功率范围', '自主完好性', '系统完好性', '首次捕获时间', '失锁重捕时间', '定位测速更新率', '跟踪通道数', '最大速度', '最大加速度', '执行结果', '测试人员', '备注']] for case in beidou_cases: power = '' if case.power_range_low and case.power_range_high: power = str(case.power_range_low) + '-' + str(case.power_range_high) state = '成功' if case.state == 2: state = '失败' executor_name = User.objects.filter(id=case.executor_id)[0].username cases_info.append(Report.convert_to_string( [software.name, case.version, case.title, case.positioning_accuracy, case.timing_accuracy, case.pseudo_range_measurement_accuracy, case.carrier_measurement_accuracy, case.speed_measurement_accuracy, case.sensitivity_capture, case.receiving_sensitivity, case.sensitivity_track, power, case.self_integrity, case.system_integrity, case.capture_time, case.recapture_time, case.update_rate, case.trace_channels, case.max_velocity, case.max_acceleration, state, executor_name, case.remark] )) sio = write_to_xlsx(file_path, cases_info) sio.seek(0) response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel') # response['Content-Disposition'] = 'attachment; filename*=UTF-8''{}'.format(escape_uri_path(file_name)) response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8', 'ISO-8859-1') response.write(sio.getvalue()) # executor, action, method = get_log(request) # gen_log(action, "测试任务", task.title, method, executor) return response