from django.http import HttpResponse from rest_framework.views import APIView import util_doc from apps.software.models import Software import logging logger = logging.getLogger('django') class Report(APIView): authentication_classes = [] # 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] executor_id = request.GET.get('executor_id') if not executor_id: logger.error("测试人员id不能为空") return HttpResponse(status=404, content="测试人员id不能为空") cases_info = {} cases = software.testcase_set.filter(executor_id=executor_id, delete=False) executors = set() if cases: for case in cases: state = '未执行' if case.state == 1: state = '执行成功' elif case.state == 2: state = '执行失败' cases_info[case.id_in_task] = { 'id': case.id_in_task, 'version': case.version, 'name': case.name, 'requisite': case.requisite, 'priority': case.priority, 'environment': case.environment, 'type': case.type, 'process': case.process, 'expected_result': case.expected_result, 'actual_result': case.actual_result, 'state': state, 'writer': case.writer, 'assessor': case.assessor, 'executor': case.executor, 'remark': case.remark, 'execute_time': str(case.update_time)[:10] } executors.add(case.executor) creators = [] plans = software.testplan_set.filter(delete=False) if plans: for plan in plans: creators.append(plan.creator.name) version_number = software.version_set.order_by('-create_time')[0].number file_name = '软件测试报告.docx' print(str(software.create_time).split(' ')[0]) document = util_doc.process_doc(software.name, software.type, version_number, str(software.create_time) .split(' ')[0], executors, cases_info) response = HttpResponse(content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8', 'ISO-8859-1') document.save(response) # executor, action, method = get_log(request) # gen_log(action, "测试报告", software_id+"-测试报告", method, executor) return response