"""This module holds the ``MTPOAuth2Validator`` class"""

from django.contrib.auth import authenticate

from oauth2_provider.oauth2_validators import OAuth2Validator


class MTPOAuth2Validator(OAuth2Validator):
    """A ``OAuth2Validator`` who handle deleted users case"""

    def validate_user(self, username, password, client, request, *args, **kwargs):
        """Check username and password correspond to a valid and active User

        Also, check that the user is not "deactivated" (soft-deleted)

        For the parameters, see ``oauth2_provider.oauth2_validators.OAuth2Validator``

        """

        user = authenticate(username=username, password=password)

        if user is not None and user.is_active and not user.is_deleted:
            request.user = user
            return True
        return False
