99from pythonpro .core .models import User
1010
1111
12- class UserEmailForm (ModelForm ):
12+ class NormalizeEmailMixin :
13+ def _normalize_email (self ):
14+ self .data = dict (self .data .items ())
15+ email = self .data .get ('email' )
16+ if email is not None :
17+ self .data ['email' ] = email .lower ()
18+
19+
20+ class UserEmailForm (ModelForm , NormalizeEmailMixin ):
1321 current_password = CharField (label = _ ("Password" ), strip = False , required = True )
1422
1523 class Meta :
@@ -19,6 +27,7 @@ class Meta:
1927 def __init__ (self , * args , ** kwargs ):
2028 self .user = kwargs .pop ('user' )
2129 super ().__init__ (* args , ** kwargs )
30+ self ._normalize_email ()
2231
2332 def clean (self ):
2433 cleaned_data = super ().clean ()
@@ -27,7 +36,7 @@ def clean(self):
2736 return cleaned_data
2837
2938
30- class UserSignupForm (UserCreationForm ):
39+ class UserSignupForm (UserCreationForm , NormalizeEmailMixin ):
3140 class Meta :
3241 model = User
3342 fields = ('first_name' , 'email' , 'source' )
@@ -44,6 +53,7 @@ def __init__(self, *args, **kwargs):
4453 self ._set_passwords (dct )
4554 args = (ChainMap (query_dict , dct ), * args [1 :])
4655 super ().__init__ (* args , ** kwargs )
56+ self ._normalize_email ()
4757
4858 def _set_passwords (self , data ):
4959 if 'password1' not in data and 'password2' not in data :
0 commit comments