Esempio n. 1
0
 def post(self, request, *args, **kwargs):
     # 前台账号用usr,密码用pwd
     user_ser = serializers.UserSerializer(data=request.data)
     try:
         user_ser.is_valid(raise_exception=True)
     except Exception as e:
         return APIResponse(data_status=1, data_msg='用户名或密码错误')
     return APIResponse(token=user_ser.token,
                        results=serializers.UserSerializer(
                            user_ser.user).data)
Esempio n. 2
0
    def post(self, request):
        # TODO change validation to use marshmallow for example
        email = request.data.get("email", None)
        password = request.data.get("password", None)
        print(email)
        user = models.TCTestUser.register(email, password)
        user.save()

        return Response(serializers.UserSerializer(user).data)
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)

        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        user = serializer.save()
        serializer_user = user_serializers.UserSerializer(user)
        tokens = serializer.data['tokens']
        data = {"refresh": tokens["refresh"], "access": tokens["access"], "id": serializer_user.data["id"]}
        return Response(data, status=status.HTTP_201_CREATED)
Esempio n. 4
0
 def post(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=self.request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     return Response({
         'user':
         serializers.UserSerializer(
             user, context=self.get_serializer_context()).data,
         'token':
         AuthToken.objects.create(user)[1]
     })
Esempio n. 5
0
 def post(self, request, *args, **kwargs):
     username = request.POST.get('username')
     password = request.POST.get('password')
     user = models.User.objects.filter(username=username,
                                       password=password).first()
     if user is None:
         return APIResponse(data_status=1, data_msg='用户名或密码不存在')
     user_ser = serializers.UserSerializer(user)
     # 签发Token
     payload = jwt_payload_handler(user)
     token = jwt_encode_handler(payload)
     return APIResponse(data_msg='登录成功', results=user_ser.data, token=token)
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        data_ser = serializers.UserSerializer(data=request.data)
        data_ser.is_valid(raise_exception=True)
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')
        mobile = request.POST.get('mobile')
        params = dict()
        params.setdefault('username', username)
        params.setdefault('password', password)
        params.setdefault('email', email)
        params.setdefault('mobile', mobile)
        params.setdefault('last_login', None)
        params.setdefault('is_superuser', 0)
        params.setdefault('first_name', '')
        params.setdefault('last_name', '')
        params.setdefault('is_staff', 1)
        params.setdefault('is_active', 1)
        params.setdefault('date_joined', datetime.now())

        user = models.User.objects.create(**params)
        user_ser = serializers.UserSerializer(user)
        return APIResponse(data_msg='注册成功!', results=user_ser.data)
Esempio n. 7
0
def create_amin_user(request):
    data = request.data.copy()
    data['is_superuser'] = data.get('is_superuser', True)
    data['is_staff'] = data.get('is_staff', True)
    serializer = serializers.UserSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        serializer.validated_data.pop('password')
        return Response(serializer.data, status=HTTP_201_CREATED)
    else:
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

    return Response({'error': 'Uer is not authorized to create new user!'},
                    status=HTTP_400_BAD_REQUEST)
Esempio n. 8
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data

        token = RefreshToken.for_user(user)

        return Response({
            'user':
            serializers.UserSerializer(
                user, context=self.get_serializer_context()).data,
            'token':
            str(token.access_token)
        })