def post(self, request):
     serializer = RegisterSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         instance = serializer.instance
         return Response(data={"user_id": instance.id, "status": instance.is_active}, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
    def test_given_inactive_user_login_should_fail(self):
        user = RegisterSerializer().create(self.user)
        user.is_active = False
        user.save()

        with self.assertRaisesRegex(ValidationError, "Incorrect credentials"):
            LoginSerializer().validate(self.user)
Пример #3
0
 def post(self, request, format=None, *args, **kwargs):
     serializer = RegisterSerializer(data=request.data)
     if serializer.is_valid():
         profile = serializer.save()
     else:
         return Response(
             serializer.errors, status=status.HTTP_400_BAD_REQUEST)
     self.send_mail(request, profile)  # should run asynchronously
     response = serializer.data
     response.update({
         'msg': 'Please confirm your email address to complete registration'
     })
     return Response(response, status=status.HTTP_201_CREATED)
    def post(request):
        # Comprovem que l'usuari no sigui manager
        if not request.data["is_manager"]:
            return Response("A manager can not be client",
                            status=status.HTTP_400_BAD_REQUEST)

        # Creem l'usuari
        user = RegisterSerializer(data=request.data)
        user.is_valid(raise_exception=True)
        user.save()

        # Creem el manager amb l'id i l'username de l'usuari
        manager = {
            "_id": user.data["id"],
            "username": user.data["username"],
            "nif": request.data.get("nif", None),
            "telf": request.data.get("telf", None)
        }

        serializer = ManagersSerializer(data=manager)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(data=serializer.data, status=status.HTTP_201_CREATED)
    def post(request):
        # Comprovem que l'usuari no sigui manager
        if request.data["is_manager"]:
            return Response("A client can not be manager",
                            status=status.HTTP_400_BAD_REQUEST)

        # Creem l'usuari
        user = RegisterSerializer(data=request.data)
        user.is_valid(raise_exception=True)
        user.save()

        # Creem el client amb l'id i l'username de l'usuari
        client = {
            "_id": user.data["id"],
            "username": user.data["username"],
            "age": request.data.get('age', None),
            "country": request.data.get('country', None)
        }

        serializer = ClientSerializer(data=client)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(data=serializer.data, status=status.HTTP_201_CREATED)
Пример #6
0
    def post(self, request, *args, **kwargs):
        try:
            data = JSONParser().parse(request)
            serializer = RegisterSerializer(data=data)

            if serializer.is_valid():
                user = serializer.save()
                token, _ = Token.objects.get_or_create(user=user)
            else:
                return Response(
                    {
                        'status': 'error',
                        'message': serializer.errors
                    },
                    status=400)
            return Response({
                'status': 'success',
                'token': token.key
            },
                            status=201)
        except Exception as e:
            return Response({'status': 'error', 'message': str(e)}, status=400)
Пример #7
0
 def post(self, request):
     serialize = RegisterSerializer(data=request.data)
     serialize.is_valid(raise_exception=True)
     user = serialize.save()
     instance, token = AuthToken.objects.create(user)
     return Response({"user": UserSerializer(user).data, "token": token})
Пример #8
0
 def get_or_add_users(self, users):
     for email in users:
         if not User.objects.filter(email=email).exists():
             register_serializer = RegisterSerializer(data={'email': email})
             register_serializer.is_valid(raise_exception=True)
             register_serializer.save()