# Path: sitegate/signin_flows/base.py
# class SigninFlow(FlowsBase):
#     """Base class for sign in flows."""
# 
#     flow_type: str = 'signin'
#     enabled: bool = SIGNIN_ENABLED
#     disabled_text: str = SIGNIN_DISABLED_TEXT
# 
#     def get_requested_form(self, request: HttpRequest) -> ModelForm:
#         form = super().get_requested_form(request)
#         form.remotes = get_registered_remotes().values()
#         return form
# 
#     def handle_form_valid(self, request: HttpRequest, form: ModelForm) -> Optional[HttpResponse]:
#         login(request, form.get_user())
# 
#         redirect_to = self.flow_args.get('redirect_to', self.default_redirect_to)
# 
#         if redirect_to:  # TODO Handle lambda variant with user as arg.
#             return redirect(redirect_to)
# 
# Path: sitegate/utils.py
# USER = get_user_model()


from django import forms
from django.utils.translation import gettext_lazy as _
from .base import SigninFlow
from .classic import ClassicSigninForm
from ..utils import USER



class ModernSigninForm(ClassicSigninForm):

    def __init__(self, request=None, *args, **kwargs):
        super(ModernSigninForm, self).__init__(request, *args, **kwargs)
        self.fields['username'].label = '%s / %s' % (_('Username'), _('Email'))

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password and '@' in username:
            # Let's first try an e-mail auth.
            result = USER._default_manager.filter(email__iexact=username)

            if len(result) == 1:
                self.cleaned_data['username'] = result[0].username

            elif len(result) > 1:
                raise forms.ValidationError(
                    _('There is more than one user with this e-mail. Please use your username to log in.'))

        return super(ModernSigninForm, self).clean()



# Path: sitegate/signin_flows/base.py
# class SigninFlow(FlowsBase):
#     """Base class for sign in flows."""
# 
#     flow_type: str = 'signin'
#     enabled: bool = SIGNIN_ENABLED
#     disabled_text: str = SIGNIN_DISABLED_TEXT
# 
#     def get_requested_form(self, request: HttpRequest) -> ModelForm:
#         form = super().get_requested_form(request)
#         form.remotes = get_registered_remotes().values()
#         return form
# 
#     def handle_form_valid(self, request: HttpRequest, form: ModelForm) -> Optional[HttpResponse]:
#         login(request, form.get_user())
# 
#         redirect_to = self.flow_args.get('redirect_to', self.default_redirect_to)
# 
#         if redirect_to:  # TODO Handle lambda variant with user as arg.
#             return redirect(redirect_to)
# 
# Path: sitegate/utils.py
# USER = get_user_model()


# Path: sitegate/signin_flows/modern.py
from django import forms
from django.utils.translation import gettext_lazy as _
from .base import SigninFlow
from .classic import ClassicSigninForm
from ..utils import USER




class ModernSigninForm(ClassicSigninForm):

    def __init__(self, request=None, *args, **kwargs):
        super(ModernSigninForm, self).__init__(request, *args, **kwargs)
        self.fields['username'].label = '%s / %s' % (_('Username'), _('Email'))

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password and '@' in username:
            # Let's first try an e-mail auth.
            result = USER._default_manager.filter(email__iexact=username)

            if len(result) == 1:
                self.cleaned_data['username'] = result[0].username

            elif len(result) > 1:
                raise forms.ValidationError(
                    _('There is more than one user with this e-mail. Please use your username to log in.'))

        return super(ModernSigninForm, self).clean()



class ModernSignin(SigninFlow):