Example #1
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     task = self.perform_create(serializer)
     re_dict = modelToJson(task)
     # headers = self.get_success_headers(serializer.data)
     return Response(re_dict, status=status.HTTP_201_CREATED)
Example #2
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.get_object()
     cases_re_set = CaseReleteCaseSet.objects.filter(case_set=instance)
     re_dict_case = modelToJson(instance)
     case_list = list()
     for case_re_set in cases_re_set:
         case_dict = modelToJson(case_re_set.case)
         case_dict['sort'] = case_re_set.sort
         case_list.append(case_dict)
     re_dict_case["cases"] = sorted(case_list, key=lambda case: case["sort"])
     return Response(re_dict_case)
Example #3
0
 def post(self, request):
     """
     开始执行任务
     :param request:
     :return:
     """
     processed_dict = {}
     for key, value in request.data.items():
         processed_dict[key] = value
     task_id = processed_dict.pop('task_id', None)
     execut_user = request.user
     if (task_id):
         test_task = TestTask.objects.filter(id=task_id)[0]
         task_state = test_task.task_state
         if (task_state == 'executing'):
             return Response(status=status.HTTP_201_CREATED,
                             data={"任务正在执行"})
         else:
             env_configs = EnvConfig.objects.filter(task=test_task)
             if (not env_configs):
                 Response(status=status.HTTP_201_CREATED, data={"请配置环境数据"})
             env_config = env_configs[0]
             apkConfigs = ApKConfig.objects.filter(env=env_config)
             mq_dict = {}
             script_type = env_config.app_script_type
             mq_dict['env_config'] = env_config.id
             mq_dict['script_type'] = script_type
             mq_dict['option'] = mq.Option.START.value
             web_configs = WebConfig.objects.filter(env=env_config)
             # web自动化设备
             webs = []
             if web_configs:
                 for web_config in web_configs:
                     wb = {}
                     pc = web_config.device
                     wb['host'] = pc.source
                     wb['name'] = pc.name
                     webs.append(wb)
             mq_dict['webs'] = webs
             # app自动化设备
             apps = []
             if apkConfigs:
                 for apkConfig in apkConfigs:
                     ac = {}
                     app_path = ""
                     if apkConfig.app:
                         app_path = apkConfig.app.url
                     test_app_path = ""
                     if apkConfig.test_app:
                         test_app_path = apkConfig.test_app.url
                     re_devices = DeviceRelateApK.objects.filter(
                         apKConfig=apkConfig)
                     if (not re_devices):
                         Response(status=status.HTTP_201_CREATED,
                                  data={"请选择设备"})
                     ac['app'] = app_path
                     ac['package_name'] = apkConfig.package_name
                     ac['test_app'] = test_app_path
                     ac['devices'] = [
                         modelToJson(re_device.device)
                         for re_device in re_devices
                     ]
                     apps.append(ac)
             mq_dict['apps'] = apps
             test_task.task_state = "executing"
             test_task.execut_start_time = datetime.now()
             # test_task.execut_user = execut_user
             test_task.save()
             router = ROUTER_PER
             if (apkConfigs or web_configs):
                 mq.send(exchange=EXCHANGE,
                         routing_key=router + '.start',
                         body=json.dumps(mq_dict))
             else:
                 case_execute.delay(task_id=task_id)
             return Response(status=status.HTTP_201_CREATED,
                             data={'success'})
     return Response(status=status.HTTP_201_CREATED, data={'fail'})