<s> from collections import OrderedDict <EOL> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . classic import SimpleClassicWithEmailSignup , SimpleClassicWithEmailSignupForm <EOL> from . . models import InvitationCode <EOL> from . . utils import USER <EOL> class ModernSignupForm ( SimpleClassicWithEmailSignupForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ModernSignupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if '<STR_LIT:username>' in self . fields : <EOL> del self . fields [ '<STR_LIT:username>' ] <EOL> def clean_email ( self ) : <EOL> email = super ( ModernSignupForm , self ) . clean_email ( ) <EOL> if USER . _default_manager . filter ( email__iexact = email ) : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return email <EOL> class ModernSignup ( SimpleClassicWithEmailSignup ) : <EOL> """<STR_LIT>""" <EOL> form = ModernSignupForm <EOL> def sign_in ( self , request , form , signup_result ) : <EOL> form_data = form . cleaned_data <EOL> return self . login_generic ( request , form_data [ '<STR_LIT:email>' ] , form_data [ '<STR_LIT>' ] ) <EOL> def add_user ( self , request , form ) : <EOL> user = super ( form . __class__ , form ) . save ( commit = False ) <EOL> if not hasattr ( user , '<STR_LIT>' ) or user . USERNAME_FIELD == '<STR_LIT:username>' : <EOL> user . username = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> user . email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> user . set_password ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> user . save ( ) <EOL> self . send_email ( request , user ) <EOL> return user <EOL> class InvitationSignupForm ( ModernSignupForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( InvitationSignupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> try : <EOL> self . fields . insert ( <NUM_LIT:0> , '<STR_LIT:code>' , forms . CharField ( label = _ ( '<STR_LIT>' ) ) ) <EOL> except AttributeError : <EOL> new_fields = OrderedDict ( ) <EOL> new_fields [ '<STR_LIT:code>' ] = forms . CharField ( label = _ ( '<STR_LIT>' ) ) <EOL> for k , v in self . fields . items ( ) : <EOL> new_fields [ k ] = v <EOL> self . fields = new_fields <EOL> def clean_code ( self ) : <EOL> code = self . cleaned_data [ '<STR_LIT:code>' ] <EOL> if not InvitationCode . is_valid ( code ) : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return code <EOL> class InvitationSignup ( ModernSignup ) : <EOL> """<STR_LIT>""" <EOL> form = InvitationSignupForm <EOL> def add_user ( self , request , form ) : <EOL> user = super ( InvitationSignup , self ) . add_user ( request , form ) <EOL> InvitationCode . accept ( form . cleaned_data [ '<STR_LIT:code>' ] , user ) <EOL> return user </s>
