Exemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        """
        获取某个服务下的接口
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        service_id = request.GET.get("service_id", 0)
        ret = []
        if service_id == "0":
            interfaces_lists = self.model.objects.all()
            for s in interfaces_lists:
                t = {"id": s.id,
                     "name": s.name,
                     "description": s.description,
                     "service_id": s.service_id,
                     "service_name": Service.objects.filter(id=s.service_id).first().name,
                     "context": json.loads(s.context)
                     }
                ret.append(t)
        else:
            interfaces = self.model.objects.filter(service_id=service_id)
            service_name = Service.objects.filter(id=service_id).first().name

            for s in interfaces:
                t = {"id": s.id,
                     "name": s.name,
                     "description": s.description,
                     "service_id": s.service_id,
                     "service_name": service_name,
                     "context": json.loads(s.context)
                     }
                ret.append(t)
        return response_success(ret)
Exemplo n.º 2
0
 def get(self, request, base_id, *args, **kwargs):
     interface = self.model.objects.filter(id=base_id).first()
     if not interface:
         return response_failed(code=self.code, message="数据不存在!")
     ret = model_to_dict(interface)
     ret["context"] = json.loads(ret["context"], encoding="utf-8")
     return response_success(ret)
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        """
        获取列表
        :param args:
        :param kwargs:
        :return:
        """
        services = self.model.objects.all()
        page = request.GET.get('page')
        page_size = request.GET.get('pageSize')
        paginator = Paginator(services, page_size)
        count = len(services)

        try:
            contacts = paginator.page(page)
        except PageNotAnInteger:
            # 如果页数不是整型, 取第一页.
            contacts = paginator.page(1)
        except EmptyPage:
            # 如果页数超出查询范围,取最后一页
            contacts = paginator.page(paginator.num_pages)

        ret = []
        for s in contacts:
            t = model_to_dict(s)
            ret.append(t)
        return response_success(ret,
                                page=page,
                                pagesize=page_size,
                                count=count)
Exemplo n.º 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')

    user_form = UserForm(data)

    if not user_form.is_valid():
        return response_failed()

    # username = data.get("username", "")
    # password = data.get("password", "")
    #
    # if not username or not password:
    #     return response_failed()

    # authenticate函数是用来校验用户名密码是否正常
    user = authenticate(username=user_form.cleaned_data['username'],
                        password=user_form.cleaned_data['password'])
    if not user:
        return response_failed(code=ErrorCode.auth, message="登录失败")
    else:
        login(request, user)
        return response_success()
Exemplo n.º 5
0
    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"])
        form = self.form(data)
        if not form.is_valid():
            return response_failed(code=self.code)

        interface = self.model.objects.create(**form.cleaned_data)

        if not interface:
            return response_failed(code=ErrorCode.interface)
        else:
            ret = model_to_dict(interface)
            ret["context"] = json.loads(ret["context"])
            return response_success(ret)
Exemplo n.º 6
0
def get_task_report_list(request, task_id):
    task_reports_path = os.path.join(settings.BASE_DIR, "task_test", "reports", str(task_id))
    ret = []
    for root, dirs, files in os.walk(task_reports_path):
        for file in files:
            if os.path.splitext(file)[1] == ".html":
                ret.append(file)
    ret.reverse()
    return response_success(ret)
Exemplo n.º 7
0
 def get(self, request, *args, **kwargs):
     task_id = request.GET.get("task_id", "")  # 从url里边获取task_id
     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)
Exemplo n.º 8
0
    def put(self, request, base_id, *args, **kwargs):
        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=ErrorCode.common, message="数据不存在!")

        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))
Exemplo n.º 9
0
    def post(self, request, *args, **kwargs):
        body = request.body
        data = json.loads(body, encoding="utf-8")

        if not isinstance(data, list):
            return response_failed(data)

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

        return response_success()
Exemplo n.º 10
0
def get_user_info(request, *args, **kwargs):
    """
    获取已登录的用户信息
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    user = request.user
    print(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="用户未登录")
Exemplo n.º 11
0
    def post(self, request, *args, **kwargs):
        """
        新增数据
        :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(code=self.code)

        service = self.model.objects.create(**form.cleaned_data)

        if not service:
            return response_failed(code=ErrorCode.service)
        else:
            return response_success(model_to_dict(service))
Exemplo n.º 12
0
    def put(self, request, base_id, *args, **kwargs):
        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=ErrorCode.interface, 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)
Exemplo n.º 13
0
def user_register(request, *args, **kwargs):
    body = request.body
    data = json.loads(body, encoding='utf-8')

    user_form = UserForm(data)

    if not user_form.is_valid():
        return response_failed()
    username = user_form.cleaned_data['username']
    password = user_form.cleaned_data['password']
    #
    # if not username or not password:
    #     return response_failed()

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

    user = User.objects.create_user(username=username, password=password)
    if not user:
        return response_failed(code=ErrorCode.auth, message="注册失败")
    else:
        login(request, user)
        return response_success()
Exemplo n.º 14
0
 def get(self, request, base_id, *args, **kwargs):
     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))
Exemplo n.º 15
0
def user_logout(request, *args, **kwargs):
    logout(request)
    return response_success()
Exemplo n.º 16
0
 def delete(self, request, base_id, *args, **kwargs):
     self.model.objects.filter(id=base_id).delete()
     return response_success()
Exemplo n.º 17
0
def run_task(request, task_id):
    _create_task_report_dir(task_id)
    _add_task_to_run_model(task_id)
    _run_py_test(task_id)
    return response_success()