# 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

        return user


class InvitationSignupForm(ModernSignupForm):
    """Modernized form with invitation code field."""

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

        new_fields = {'code': forms.CharField(label=_('Invitation code'))}
        new_fields.update(self.fields)
        self.fields = new_fields

    def clean_code(self):
        code = self.cleaned_data['code']

        if not InvitationCode.is_valid(code):
            raise forms.ValidationError(_('This invitation code is invalid.'))

        return code


class InvitationSignup(ModernSignup):
    """Modernized registration flow with additional invitation code field."""

    form = InvitationSignupForm

    def add_user(self, request, form):
        user = super(InvitationSignup, self).add_user(request, form)

# 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


        return user


class InvitationSignupForm(ModernSignupForm):
    """Modernized form with invitation code field."""

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

        new_fields = {'code': forms.CharField(label=_('Invitation code'))}
        new_fields.update(self.fields)
        self.fields = new_fields

    def clean_code(self):
        code = self.cleaned_data['code']

        if not InvitationCode.is_valid(code):
            raise forms.ValidationError(_('This invitation code is invalid.'))

        return code


class InvitationSignup(ModernSignup):
    """Modernized registration flow with additional invitation code field."""

    form = InvitationSignupForm

    def add_user(self, request, form):
        user = super(InvitationSignup, self).add_user(request, form)

        InvitationCode.accept(form.cleaned_data['code'], user)