Beispiel #1
0
    def get(self, request, uid=None):
        print(uid)
        response = MyResponse()
        if not uid:
            user_queryset = self.get_queryset()
            if user_queryset.exists():
                response.data = self.get_serializer(user_queryset,
                                                    many=True).data
                return Response(response.to_dict())
            return Response(response.error_response("nothing"))

        else:
            user_obj = self.get_object()
            if user_obj:
                response.data = self.get_serializer(user_obj).data
                return Response(response.to_dict())
            return Response(response.error_response("no one"))
Beispiel #2
0
 def post(self, request):
     response = MyResponse()
     form = forms.LoginForm(request.data)
     if form.is_valid():
         username = form.cleaned_data.get("username")
         password = form.cleaned_data.get("password")
         image_code_id = form.cleaned_data.get("image_code_id")
         image_code = form.cleaned_data.get("image_code")
         try:
             user_obj = self.get_user(username)
             self.checke_captcha(image_code_id, image_code)
             self.check_password(user_obj, password)
         except MyException as e:
             return Response(response.error_response(e.msg))
         response.data = self.login_success(user_obj)
         return Response(response.to_dict())
     else:
         msg = form.errors[list(form.errors.keys())[0]][0]
         return Response(response.error_response(msg=msg))
Beispiel #3
0
    def post(self, request):
        form = forms.RegisterForm(request.data)
        response = MyResponse()
        if form.is_valid():
            clean_data = form.cleaned_data
            username = clean_data.get("username")
            password = clean_data.get("password")
            phone = clean_data.get("phone")
            email = clean_data.get("email")

            with transaction.atomic():
                user = User.objects.create(username=username,
                                           password=password,
                                           phone=phone,
                                           email=email)
            response.data = UserSerializer(user).data
            return Response(response.to_dict())

        else:
            msg = form.errors[list(form.errors.keys())[0]][0]
            return Response(response.error_response(msg=msg))
Beispiel #4
0
 def get(self, request):
     response = MyResponse()
     print("正在重试。。。")
     raise KeyError
     return Response(response.to_dict())