예제 #1
0
def user_register(request, *args, **kwargs):
    """
    用户注册
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    # user = UserForm(request.POST)
    body = request.body
    data = json.loads(body, encoding='utf-8')
    form = UserForm(data)
    if not form.is_valid():
        # print(user.cleaned_data['username'])
        # print(user.cleaned_data['password'])
        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 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(self, request, base_id, *args, **kwargs):
     """
     获取单个数据
     :param request:
     :param base_id:
     :param args:
     :param kwargs:
     :return:
     """
     model_data = self.model.objects.filter(id=base_id).first()
     if not model_data:
         return response_failed(code=self.code, message="数据不存在")
     return response_success(model_to_dict(model_data))
예제 #4
0
 def get(self, request, *args, **kwargs):
     """
     获取列表
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     model_lists = self.model.objects.all()
     result = []
     for r in model_lists:
         res = model_to_dict(r)
         result.append(res)
     return response_success(result)
예제 #5
0
 def delete(self, request, base_id, *args, **kwargs):
     """
     删除单个数据
     :param request:
     :param base_id:
     :param args:
     :param kwargs:
     :return:
     """
     model_data = self.model.objects.filter(id=base_id).first()
     if not model_data:
         return response_failed(message="删除的数据不存在")
     self.model.objects.filter(id=base_id).delete()
     return response_success()
예제 #6
0
    def post(self, request, *args, **kwargs):
        """
        创建数据
        :param request:
        :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()

        model_data = self.model.objects.create(**form.cleaned_data)
        if not model_data:
            return response_failed(code=self.code, message="创建数据失败")
        else:
            return response_success(model_to_dict(model_data))
예제 #7
0
def user_login(request, *args, **kwargs):
    """
    用户登录
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    # 用UserForm做字段的格式校验
    # form = UserForm(request.POST)
    # print("=========>", request.POST)
    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'])
    if not user:
        return response_failed(code=ErrorCode.auth, message="登录失败")
    else:
        login(request, user)  # 登录持久化,记录session
        return response_success()
예제 #8
0
    def put(self, request, base_id, *args, **kwargs):
        """
        修改单个数据
        :param request:
        :param base_id:
        :param args:
        :param kwargs:
        :return:
        """
        body = request.body
        # 将得到的body数据转换成字典
        data = json.loads(body, encoding='utf-8')
        form = self.form(data)
        if not form.is_valid():
            return response_failed()

        model_data = self.model.objects.filter(id=base_id).first()
        if not model_data:
            return response_failed(code=self.code, 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))
예제 #9
0
def user_logout(request, *args, **kwargs):
    logout(request)
    return response_success()