def valid_email(email, user): """ Make sure that this email address does not already exist in the database """ if not email: raise ValidationError('Email is blank', code='blank') email = normalize_email(email) # If this the submitted email is the user's current email, everything is good since the current email was # already validated. This is will often be the case when a user edits his profile information. try: if user.email == email: return email # This is going to fail for AnonymousUser objects (aka every time during sign up. That's fine). except AttributeError: pass # consider case-sensitivity. It is valid for a domain provider to allow case insensitive emails. if User.user_exists(email): raise ValidationError('That email address is already registered', code="email_exists") # Store the normalized email in the database return email
def clean_email(self): email = self.cleaned_data.get('email') email = normalize_email(email) if not User.objects.filter(email=email): raise ValidationError('Email does not exist') return email