Пример #1
0
    def login(self, request):
        """User sign in."""
        print(request.data)
        # First clear everything before login
        logout(request)
        serializer = UserLoginSerializer(data=request.data)

        if not serializer.is_valid(raise_exception=False):
            data = {
                "info": "password or username are not valid",
                "failure": "validation failed",
            }

            return Response(data, status=status.HTTP_403_FORBIDDEN)

        user, token = serializer.save()

        data = {
            "info": "success validation",
            "success": "user validated",
            "user": UserModelSerializer(user).data,
            "access_token": token,
        }

        response = Response(data, status=status.HTTP_201_CREATED)

        # Use default django authentication
        login(request, user)

        return response
Пример #2
0
 def login(self, request):
     """User sign in."""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user, token = serializer.save()
     data = {'user': UserModelSerializer(user).data, 'access_token': token}
     return Response(data, status=status.HTTP_201_CREATED)
Пример #3
0
 def login(self, request):
     """Inicio de sesion de Usuarios."""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user, token = serializer.save()
     data = {'user': UserModelSerializer(user).data, 'access_token': token}
     return Response(data, status=status.HTTP_200_OK)
Пример #4
0
 def login(self, request):
     """Instructor Login"""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     token = serializer.save()
     data = {
         'access_token': token
     }
     return Response(data, status=status.HTTP_201_CREATED)
Пример #5
0
    def login(self, request):
        notification_token = request.query_params.get("notification_token")
        current_user_param = request.data["username"]
        current_user = User.objects.get(username=current_user_param)

        profile = current_user.profile

        profile.notification_token = notification_token
        profile.save()

        serealizer = UserLoginSerializer(data=request.data)
        serealizer.is_valid(raise_exception=True)
        user, token = serealizer.save()

        data = {"user": UserModelSerializer(user).data, "jwt": token}
        return Response(data, status=status.HTTP_201_CREATED)
Пример #6
0
    def post(self, request):

        copy_data = request.data.copy()
        copy_data['type'] = request.path.split("/")[-3]
        if (request.data.get('type') != None):
            copy_data['social'] = request.data['type']

        serializer = UserLoginSerializer(data=copy_data)
        serializer.is_valid(raise_exception=True)
        user, token = serializer.save()
        data = {
            'user':
            ClientAllSerializer(user).data
            if copy_data['type'] == 'client' else WorkerSerializer(user).data,
            'access_token':
            token
        }
        return Response(data)
    def test_userloginserializer_token_generation(self):
        """Checks User instance login using UserLoginSerializer

        Test if UserLoginSerializer deserialization is valid."""
        time_before_login = timezone.now()
        data = {'email': self.email, 'password': self.password}
        serializer = UserLoginSerializer(data=data)
        self.assertTrue(
            serializer.is_valid(),
            msg="""User deserialization should be valid since the credentials
            are correct.""")

        serializer_data = serializer.save()
        user_data = serializer_data.get('user')

        for key in ['email', 'first_name', 'last_name']:
            self.assertIn(
                key,
                user_data.keys(),
                msg="""UserLoginSerializer did not return {} information.""".
                format(key))

            value = user_data[key]
            expected_value = getattr(self.user, key)
            self.assertEqual(
                value,
                expected_value,
                msg="""UserLoginSerializer returned incorrect {} information.
                Expected: {}. Obtained: {}""".format(key, expected_value,
                                                     value))

        last_login = user_data.get('last_login')
        last_login = datetime.strptime(last_login, '%Y-%m-%dT%H:%M:%S%f%z')
        self.assertGreater(
            last_login,
            time_before_login,
            msg="""UserLoginSerializer returned incorrect last_login
            information. Expected: {}. Obtained: {}""".format(
                time_before_login, last_login))