# Path: sitegate/signup_flows/classic.py
# class SimpleClassicWithEmailSignup(ClassicWithEmailSignup):
#     """Classic registration flow borrowed from Django built-in
#     with e-mail field, but without second password field.
# 
#     """
# 
#     form = SimpleClassicWithEmailSignupForm
# 
# class SimpleClassicWithEmailSignupForm(ClassicWithEmailSignupForm):
#     """Classic form with email field but without the second password field."""
# 
#     def __init__(self, *args, **kwargs):
#         super(SimpleClassicWithEmailSignupForm, self).__init__(*args, **kwargs)
#         del self.fields['password2']
# 
# Path: sitegate/models.py
# class InvitationCode(InheritedModel, ModelWithCode):
# 
#     creator = models.ForeignKey(
#         USER_MODEL, related_name='creators', verbose_name=_('Creator'), on_delete=models.CASCADE)
# 
#     acceptor = models.ForeignKey(
#         USER_MODEL, related_name='acceptors', verbose_name=_('Acceptor'), null=True, blank=True,
#         editable=False, on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = _('Invitation code')
#         verbose_name_plural = _('Invitation codes')
# 
#     class Fields:
#         code = _('Invitation code')
#         expired = {'help_text': _('Visitors won\'t be able to sign up with an expired code.')}
# 
#     @classmethod
#     def add(cls, creator: 'User') -> 'InvitationCode':
#         new_code = cls(creator=creator)
#         new_code.save(force_insert=True)
#         return new_code
# 
#     @classmethod
#     def accept(cls, code: str, acceptor: 'User'):
#         return cls.objects.filter(code=code).update(acceptor=acceptor, expired=True, time_accepted=timezone.now())
# 
# Path: sitegate/utils.py
# USER = get_user_model()


from django import forms
from django.forms import ModelForm
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _
from .classic import SimpleClassicWithEmailSignup, SimpleClassicWithEmailSignupForm
from ..models import InvitationCode
from ..utils import USER
    from django.contrib.auth.models import User  # noqa


def get_username_max_len() -> int:
    """Returns username maximum length as supported by Django."""

    fields = [field for field in USER._meta.fields if field.name == 'username']
    try:
        length = fields[0].max_length

    except IndexError:
        length = 30

    return length


USERNAME_MAX_LEN = get_username_max_len()


class ModernSignupForm(SimpleClassicWithEmailSignupForm):
    """Modernized form with unique e-mail field, without username and second password fields."""

    def __init__(self, *args, **kwargs):
        super(ModernSignupForm, self).__init__(*args, **kwargs)

        if 'username' in self.fields:
            del self.fields['username']

    def clean_email(self):
        email = super(ModernSignupForm, self).clean_email()


# Path: sitegate/signup_flows/classic.py
# class SimpleClassicWithEmailSignup(ClassicWithEmailSignup):
#     """Classic registration flow borrowed from Django built-in
#     with e-mail field, but without second password field.
# 
#     """
# 
#     form = SimpleClassicWithEmailSignupForm
# 
# class SimpleClassicWithEmailSignupForm(ClassicWithEmailSignupForm):
#     """Classic form with email field but without the second password field."""
# 
#     def __init__(self, *args, **kwargs):
#         super(SimpleClassicWithEmailSignupForm, self).__init__(*args, **kwargs)
#         del self.fields['password2']
# 
# Path: sitegate/models.py
# class InvitationCode(InheritedModel, ModelWithCode):
# 
#     creator = models.ForeignKey(
#         USER_MODEL, related_name='creators', verbose_name=_('Creator'), on_delete=models.CASCADE)
# 
#     acceptor = models.ForeignKey(
#         USER_MODEL, related_name='acceptors', verbose_name=_('Acceptor'), null=True, blank=True,
#         editable=False, on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = _('Invitation code')
#         verbose_name_plural = _('Invitation codes')
# 
#     class Fields:
#         code = _('Invitation code')
#         expired = {'help_text': _('Visitors won\'t be able to sign up with an expired code.')}
# 
#     @classmethod
#     def add(cls, creator: 'User') -> 'InvitationCode':
#         new_code = cls(creator=creator)
#         new_code.save(force_insert=True)
#         return new_code
# 
#     @classmethod
#     def accept(cls, code: str, acceptor: 'User'):
#         return cls.objects.filter(code=code).update(acceptor=acceptor, expired=True, time_accepted=timezone.now())
# 
# Path: sitegate/utils.py
# USER = get_user_model()


# Path: sitegate/signup_flows/modern.py
from django import forms
from django.forms import ModelForm
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _
from .classic import SimpleClassicWithEmailSignup, SimpleClassicWithEmailSignupForm
from ..models import InvitationCode
from ..utils import USER
    from django.contrib.auth.models import User  # noqa



def get_username_max_len() -> int:
    """Returns username maximum length as supported by Django."""

    fields = [field for field in USER._meta.fields if field.name == 'username']
    try:
        length = fields[0].max_length

    except IndexError:
        length = 30

    return length


USERNAME_MAX_LEN = get_username_max_len()


class ModernSignupForm(SimpleClassicWithEmailSignupForm):
    """Modernized form with unique e-mail field, without username and second password fields."""

    def __init__(self, *args, **kwargs):
        super(ModernSignupForm, self).__init__(*args, **kwargs)

        if 'username' in self.fields:
            del self.fields['username']

    def clean_email(self):
        email = super(ModernSignupForm, self).clean_email()


        if USER._default_manager.filter(email__iexact=email):