Пример #1
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()
Пример #2
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)
Пример #3
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()
Пример #4
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'])
        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)
Пример #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()
Пример #6
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='用户未登录')
Пример #7
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))
Пример #8
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))
Пример #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='数据不存在')
     ret = model_to_dict(service)
     ret['context'] = json.loads(ret['context'], encoding="utf-8")
     return response_success(ret)