Exemple #1
0
    def put(self, request, base_id, *args, **kwargs):
        """
        这个是全量修改数据
        :param request:
        :param base_id:
        :param args:
        :param kwargs:
        :return:
        """
        body = request.body
        data = json.loads(body, encoding='utf-8')

        if "context" not in data:
            return response_failed()
        data['context'] = json.dumps(data['context'])

        form = self.form(data)
        if not form.is_valid():
            return response_failed()

        interface = self.model.objects.filter(id=base_id).first()
        if not interface:
            return response_failed(code=self.code, message='数据不存在')

        self.model.objects.filter(
            id=base_id).update(**form.cleaned_data)  # 这里返回的id
        interface = self.model.objects.filter(id=base_id).first()

        ret = model_to_dict(interface)
        ret['context'] = json.loads(ret['context'], encoding="utf-8")
        return response_success(ret)
Exemple #2
0
def user_register(request, *args, **kwargs):
    """
    注册
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    body = request.body
    data = json.loads(body, encoding='utf-8')
    # username = data.get('username', '')
    # password = data.get('password', '')
    # if not username or not password:
    #     return response_failed()

    form = UserForm(data)
    if not form.is_valid():
        return response_failed()

    if User.objects.filter(username=form.cleaned_data["username"]).exists():
        return response_failed(code=ErrorCode.auth, message='用户已存在')

    user = User.objects.create_user(username=form.cleaned_data["username"],
                                    password=form.cleaned_data["password"])
    if not user:
        return response_failed(code=ErrorCode.auth, message='注册失败')
    else:
        login(request, user)  # 登录持久化
        return response_success()
    def post(self, request, *args, **kwargs):
        """
        这个是创建数据
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        body = request.body
        data = json.loads(body, encoding='utf-8')
        if "context" not in data:
            return response_failed()

        data['context'] = json.dumps(data['context'])
        print(data)
        form = self.form(data)
        if not form.is_valid():
            return response_failed()

        service = self.model.objects.create(**form.cleaned_data)
        if not service:
            return response_failed(code=self.code, message='创建数据失败')
        else:
            ret = model_to_dict(service)
            ret['context'] = json.loads(ret['context'], encoding="utf-8")
            return response_success(ret)
Exemple #4
0
def user_login(request, *args, **kwargs):
    """
    登录
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    body = request.body
    data = json.loads(body, encoding='utf-8')

    form = UserForm(data)
    if not form.is_valid():
        return response_failed()
    user = authenticate(username=form.cleaned_data["username"],
                        password=form.cleaned_data["password"])
    # username = data.get('username', '')
    # password = data.get('password', '')
    # if not username or not password:
    #     return response_failed()
    # user = authenticate(username=username, password=password)
    if not user:
        return response_failed(code=ErrorCode.auth, message='登录失败')
    else:
        login(request, user)  # 登录持久化
        return response_success()
Exemple #5
0
    def post(self, request, *args, **kwargs):
        """
        这个是创建数据, 我们需要处理一下,因为我们是批量添加
        所以我们的要求的参数就变成了一个列表:

        [{task_id:1, interface_id:1}, {task_id:1, interface_id:1}, {task_id:1, interface_id:1}]
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        body = request.body
        data = json.loads(body, encoding='utf-8')

        # 数组参数校验,是否是数组
        if not isinstance(data, list):
            return response_failed()

        for item in data:
            form = self.form(item)
            if not form.is_valid():
                return response_failed()

            service = self.model.objects.create(**form.cleaned_data)
            if not service:
                return response_failed(code=self.code, message='创建数据失败')

        return response_success()
Exemple #6
0
def get_task_report_list(request, task_id):  # 获取任务的report列表
    task_reports_path = os.path.join(settings.BASE_DIR, "task_test", "reports",
                                     str(task_id))
    list_name = []
    for file in os.listdir(task_reports_path):
        if os.path.splitext(file)[1] == '.html':
            list_name.append(file)
    return response_success(list_name)
Exemple #7
0
def user_logout(request, *args, **kwargs):
    """
    注销
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    logout(request)
    return response_success()
Exemple #8
0
 def delete(self, request, base_id, *args, **kwargs):
     """
     这个是删除数据
     :param request:
     :param base_id:
     :param args:
     :param kwargs:
     :return:
     """
     self.model.objects.filter(id=base_id).delete()
     return response_success()
Exemple #9
0
 def get(self, request, base_id, *args, **kwargs):
     """
     这个是单个数据
     :param request:
     :param base_id:
     :param args:
     :param kwargs:
     :return:
     """
     service = self.model.objects.filter(id=base_id).first()
     if not service:
         return response_failed(code=self.code, message='数据不存在')
     return response_success(model_to_dict(service))
Exemple #10
0
 def get(self, request, base_id, *args, **kwargs):
     """
     这个是单个数据
     :param request:
     :param base_id:
     :param args:
     :param kwargs:
     :return:
     """
     service = self.model.objects.filter(id=base_id).first()
     if not service:
         return response_failed(code=self.code, message='数据不存在')
     ret = model_to_dict(service)
     ret['context'] = json.loads(ret['context'], encoding="utf-8")
     return response_success(ret)
Exemple #11
0
def get_user_info(request, *args, **kwargs):
    """
    获取已登录的用户信息
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    user = request.user
    if not user:
        return response_failed(code=ErrorCode.auth, message='用户未登录')
    if user.is_authenticated:  # 判断用户是否已经通过校验
        return response_success(data={'id': user.id, 'name': user.username})
    else:
        return response_failed(code=ErrorCode.auth, message='用户未登录')
Exemple #12
0
    def get(self, request, *args, **kwargs):
        """
        这个是获取列表, 需要一个参数:task_id 在url里面
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        task_id = request.GET.get('task_id', 0)

        task_interfaces = TaskInterface.objects.filter(
            task_id=task_id)  # 拿到任务所有的接口
        ret = []
        for s in task_interfaces:
            interface = Interface.objects.filter(id=s.interface_id).first()
            t = model_to_dict(interface)
            t['context'] = json.loads(t['context'], encoding="utf-8")
            t['task_id'] = task_id
            t['task_interface_id'] = s.id
            ret.append(t)
        return response_success(ret)
 def get(self, request, *args, **kwargs):
     """
     这个是获取某个的服务下的接口列表,get方法是怎么传的
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     service_id = request.GET.get('service_id', 0)  # 0代表默认值
     interfaces = self.model.objects.filter(service_id=service_id)
     ret = []
     for s in interfaces:
         # t = dict()
         # t['id'] = s.id
         # t['name'] = s.name
         # t['description'] = s.description
         # t['service_id'] = s.service_id
         # t['context'] = json.loads(s.context, encoding="utf-8") # 把字符串改为字典传到前端
         t = model_to_dict(s)
         t['context'] = json.loads(t['context'], encoding="utf-8")
         ret.append(t)
     return response_success(ret)
Exemple #14
0
    def put(self, request, base_id, *args, **kwargs):
        """
        这个是全量修改数据
        :param request:
        :param base_id:
        :param args:
        :param kwargs:
        :return:
        """
        body = request.body
        data = json.loads(body, encoding='utf-8')
        form = self.form(data)
        if not form.is_valid():
            return response_failed()

        service = self.model.objects.filter(id=base_id).first()
        if not service:
            return response_failed(code=self.code, message='数据不存在')

        # service = self.model.objects.filter(id=service_id).update(name=form.cleaned_data["name"], description=form.cleaned_data["description"])
        self.model.objects.filter(id=base_id).update(**form.cleaned_data) # 这里返回的id
        service = self.model.objects.filter(id=base_id).first()
        return response_success(model_to_dict(service))
Exemple #15
0
def run_task(request, task_id):  # 执行任务
    _create_task_report_dir(task_id)  # 1. 创建目录
    _add_task_to_run_modal(task_id)  # 2. 把任务id添加到执行表里面去
    _run_py_test(task_id)  # 3. 执行pytest
    return response_success()