示例#1
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.module.objects.create(**form.cleaned_data)
            if not service:
                return response_failed(code=self.code, message='创建数据失败')

        return response_success()
示例#2
0
def get_user_info(request, *args, **kwargs):
    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='用户未登录')
示例#3
0
def get_user_info(request, *args, **kwargs):
    """
   获取已登录的用户信息
   """
    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="用户未登录")
示例#4
0
    def post(self, request, *args, **kwargs):
        """ 这个是创建数据 """
        body = request.body
        data = json.loads(body, encoding='utf-8')
        form = self.form(data)
        if not form.is_valid():
            return response_failed()

        # name = form.cleaned_data['name']
        # description = form.cleaned_data['description']
        # service = self.module.objects.create(name=name, description=description)
        service = self.model.objects.create(**form.cleaned_data)
        if service:
            return response_success(model_to_dict(service))
        else:
            return response_failed(code=self.code, message='创建服务失败')
示例#5
0
 def delete(self, request, base_id, *args, **kwargs):
     """ 这个是删除数据 """
     interface = self.module.objects.filter(id=base_id)
     if not interface:
         return response_failed(code=self.code, message='数据不存在')
     interface.delete()
     return response_success()
示例#6
0
    def get(self, request, base_id, *args, **kwargs):

        """ 这个是获取单个数据 """
        interface = self.module.objects.filter(pk=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)
示例#7
0
def loginUser(request, *args, **kwargs):
    print("登录方法")
    """
    登录
    """
    body = request.body
    data = json.loads(body, encoding='utf=8')
    form = UserForm(data)
    result = form.is_valid()
    if not result:
        return response_failed(message="表单校验失败")
    # authenticate()仅仅是验证用户的登录状态,存在返回该对象,反之返回none
    user = authenticate(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 put(self, request, service_id, *args, **kwargs):
     '''
     全量修改单个服务
     :param request:
     :param args:
     :param kwargs:
     :return:
     '''
     body = request.body
     data = json.loads(body, encoding="utf-8")
     form = self.form(data)
     result = form.is_valid()
     if not result:
         return response_failed(message="表单校验失败")
     # service = self.model.objects.filter(id=service_id).update(name=form.cleaned_data['name'], description=form.cleaned_data['description'])
     service = self.model.objects.filter(id=service_id).update(
         **form.cleaned_data)
     if not service:
         return response_failed(code=self.code.auth, message="获取数据失败")
     return response_success(model_to_dict(service))
示例#9
0
def user_register(request, *args, **kwargs):
    """ 注册 """
    body = request.body
    data = json.loads(body, encoding='utf-8')

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

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

    user = User.objects.create_user(username=username, password=password)
    if user:
        login(request, user)
        return response_success()
    else:
        return response_failed(code=ErrorCode.auth, message='登录失败')
示例#10
0
def register(request, *args, **kwargs):
    print("注册方法")
    """
    注册
    """
    body = request.body
    data = json.loads(body, encoding='utf-8')
    form = UserForm(data)
    result = form.is_valid()
    if not result:
        return response_failed(message="表单校验失败")
    if User.objects.filter(username=form.cleaned_data["username"]).exists():
        return response_failed(code=ErrorCode.auth, message="该用户已存在")
    else:
        user = User.objects.create_user(username=form.cleaned_data["username"],
                                        password=form.cleaned_data["password"])
        if not user:
            return response_failed(message="注册失败")
        else:
            login(request, user)  # 登录持久化
            return response_success({"message": "注册成功"})
示例#11
0
 def post(self, request, *args, **kwargs):
     '''
     创建数据
     :param request:
     :param args:
     :param kwargs:
     :return:
     '''
     body = request.body  #获取请求数据的身体
     data = json.loads(body, encoding='utf-8')  #把请求回来的数据用 loads转换成json
     form = self.form(data)  #用formData的方式校验数据是否符合要求
     if not form.is_valid():  #判断校验结果,为空返回错误
         return response_failed()
     # 正确的情况下使用类Service中的object下的create创造这条数据,
     # service = Service.objects.create(name=form.cleaned_data['name'], description=form.cleaned_data['description'])
     service = self.model.objects.create(**form.cleaned_data)
     if not service:
         return response_failed(code=self.code, message="创建服务失败")
     else:
         print(service)
         return response_success(model_to_dict(service))
    def get(self, request, service_id, *args, **kwargs):
        '''
        获取单个服务
        :param request:
        :param args:
        :param kwargs:
        :return:
        '''
        service = self.model.objects.filter(id=service_id).first()
        if not service:
            return response_failed(code=self.code, message="获取数据失败")

        return response_success(model_to_dict(service))
示例#13
0
def user_login(request, *args, **kwargs):
    """ 登录 """
    body = request.body
    print('body---------------->', body)
    data = json.loads(body, encoding='utf-8')
    print('data--------------->', data)

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

    username = form.cleaned_data['username']
    password = form.cleaned_data['password']
    print('等待user验证......................')
    user = authenticate(username=username, password=password)
    print('user-------------->', user)
    if not user:
        print('没有进到user')
        return response_failed(code=ErrorCode.auth, message='登录失败')
    else:
        login(request, user)
        print('进入到user,并且登录成功')
        return response_success()
    def post(self, request, *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.module.objects.create(**form.cleaned_data)
        # interface = self.module.objects.create(name=name,
        #                                       description=description,
        #                                       service_id=service_id,
        #                                       context=context)
        if not interface:
            return response_failed(code=self.code, message='创建服务失败')
        else:
            ret = model_to_dict(interface)
            ret['context'] = json.loads(ret['context'], encoding='utf-8')
            return response_success(ret)
示例#15
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.module.objects.filter(id=base_id).first()
        if not interface:
            return response_failed(code=self.code, message='数据不存在')

        self.module.objects.filter(id=base_id).update(**form.cleaned_data)
        interface = self.module.objects.filter(id=base_id).first()

        ret = model_to_dict(interface)
        ret['context'] = json.loads(ret['context'], encoding='utf-8')
        return response_success(ret)