def copy(self, request, **kwargs): try: pk = kwargs['pk'] case_obj = models.Case.objects.get(id=pk) if not case_obj: return JsonResponse(code=4004, msg='this case not exist', status=status.HTTP_200_OK) steps = models.Step.objects.filter(case_id=pk) if not steps: return JsonResponse(code=2001, msg='this case no caseStep, please add it', status=status.HTTP_200_OK) case_obj.id = None case_obj.name = case_obj.name + '--copy' case_obj.save() steps = models.Step.objects.filter(case_id=pk) for step in steps: step.id = None step.case = case_obj # step.save() models.Step.objects.bulk_create(steps) return JsonResponse(code=2001, msg='success', status=status.HTTP_200_OK, data=[]) except: return JsonResponse(code=4004, msg='sorry,服务器开小差', status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): try: serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) except: return JsonResponse(status=status.HTTP_404_NOT_FOUND, msg="服务器开小差了") return JsonResponse(data=serializer.data, msg="success", code=2001, status=status.HTTP_201_CREATED, headers=headers)
def leading_api_list(self, request): try: url = request.data['url'] except ValueError as E: return JsonResponse(data=[], msg=E, code=4004, status=status.HTTP_400_BAD_REQUEST) leading_api.leading_api_list(url=url) return JsonResponse(code=2001, status=status.HTTP_201_CREATED, msg='导入中')
def copy(self, request, **kwargs): try: pk = kwargs['pk'] step_obj = models.Step.objects.get(id=pk) step_obj.id = None step_obj.name = step_obj.name + '--copy' step_obj.save() return JsonResponse(code=2001, msg='success', status=status.HTTP_200_OK, data=[]) except: return JsonResponse(code=4004, msg='the id not found', status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): try: partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} except: return JsonResponse(status=status.HTTP_404_NOT_FOUND, msg="服务器开小差了") return JsonResponse(data=serializer.data, msg="success", code=2001, status=status.HTTP_206_PARTIAL_CONTENT)
def runSuite(self, request, *args, **kwargs): """ 运行suite :param request: {'id':'', config:''} :param args: :param kwargs: :return: """ try: cases = [] data = request.data config = data['config'] suite_id = data['id'] suite_obj = models.TestSuite.objects.get(id=suite_id) case_obj = suite_obj.case.all().values('id') case_id_list_obj = map(lambda x: x['id'], case_obj) case_id_list = [case for case in case_id_list_obj] except: return JsonResponse(code=4004, msg='服务器开小差了', status=status.HTTP_404_NOT_FOUND) if not isinstance(case_id_list, list): case_id_list = [ case_id_list, ] # for case_id in case_id_list: steps_query = models.Step.objects.filter(case_id=case_id) if not steps_query: return JsonResponse(code=2001, msg='这个{}未添加步骤'.format(case_id), status=status.HTTP_404_NOT_FOUND) case = build_case_by_id(case_id) cases.append(case) summary = debug_cases(cases, config=config) # report = create_report_obj(summary, suite_obj) try: create_report_obj(summary, suite_id) except: return JsonResponse(status=status.HTTP_200_OK, code=4004, msg='生成报告失败', data=[]) return JsonResponse(status=status.HTTP_200_OK, code=2001, msg='报告生成中', data=[])
def post(self, request): try: data = json.loads(json.dumps(request.data)) config = data['config'] steps = data['steps'] case_name = data['caseName'] case_steps = build_case(steps, case_name=case_name) summary = debug_case(case_steps, config=config) return JsonResponse(data=summary, code=2001, status=status.HTTP_200_OK, msg='success') except ValueError: return JsonResponse(status=status.HTTP_404_NOT_FOUND, code=4001, msg='not found')
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return JsonResponse(data=serializer.data, code=2001, msg="success", status=status.HTTP_200_OK)
def runById(self, request, *args, **kwargs): """ 批量运行 :param request: {'ids':[], config:''} :param args: :param kwargs: :return: """ cases = [] data = request.data config = data['config'] case_id_list = data['ids'] if not case_id_list: return JsonResponse(code=4004, msg='服务器开小差了', status=status.HTTP_404_NOT_FOUND) if not isinstance(case_id_list, list): case_id_list = [ case_id_list, ] for case_id in case_id_list: steps_query = models.Step.objects.filter(case_id=case_id) if not steps_query: return JsonResponse(code=2001, msg='这个{}未添加步骤'.format(case_id), status=status.HTTP_404_NOT_FOUND) case = build_case_by_id(case_id) cases.append(case) summary = debug_cases(cases, config=config) try: create_report_obj(summary) except: return JsonResponse(status=status.HTTP_200_OK, code=4004, msg='生成报告失败', data=[]) return JsonResponse(status=status.HTTP_200_OK, code=2001, msg='报告生成中', data=[])
def getLots(self, request, *args, **kwargs): """ 批量查询 :param request: {'ids': []} :param args: :param kwargs: :return: """ try: ids_obj = request.query_params.get('ids', None) queryset = models.Case.objects.in_bulk(ids_obj) ser = self.serializer_class(instance=queryset.values(), many=True) except: return JsonResponse(code=4004, msg='服务器开小差了', status=status.HTTP_404_NOT_FOUND) return JsonResponse(status=status.HTTP_200_OK, code=2001, msg='success', data=ser.data)
def deleteLots(self, request, *args, **kwargs): """ 批量删除 :param request: {'ids': []} :param args: :param kwargs: :return: """ delete_id = request.query_params.get('ids', None) if not delete_id: return JsonResponse(code=4004, msg='服务器开小差了', status=status.HTTP_404_NOT_FOUND) # ids = delete_id.strip('[]') # for i in ids.split(','): # get_object_or_404(models.Case, pk=int(i)).delete() models.Case.objects.extra(where=["id IN (" + delete_id + ")"]).delete() logger.info('-' * 10 + ('删除case by:{} '.format(request.user)) + '-' * 10) return JsonResponse(status=status.HTTP_200_OK, code=2001, msg='delete success')
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return JsonResponse(data=serializer.data, code=2001, msg="success", status=status.HTTP_200_OK)
def destroy(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) self.perform_destroy(instance) return JsonResponse(data=[serializer.data], code=2001, msg="delete resource success", status=status.HTTP_200_OK)