How to use clean_email method in Kiwi

Best Python code snippet using Kiwi_python

backend.py

Source:backend.py Github

copy

Full Screen

1from django.contrib.auth import get_user_model2from django.contrib.auth.backends import ModelBackend3from django.core.exceptions import ImproperlyConfigured4User = get_user_model()5def normalise_email(email):6 """7 The local part of an email address is case-sensitive, the domain part8 isn't. This function lowercases the host and should be used in all email9 handling.10 """11 clean_email = email.strip()12 if '@' in clean_email:13 local, host = clean_email.split('@')14 return local + '@' + host.lower()15 return clean_email16if hasattr(User, 'REQUIRED_FIELDS'):17 if not (User.USERNAME_FIELD == 'email' or 'email' in User.REQUIRED_FIELDS):18 raise ImproperlyConfigured(19 "EmailBackend: Your User model must have an email"20 " field with blank=False")21class EmailBackend(ModelBackend):22 """23 Custom auth backend that uses an email address and password24 For this to work, the User model must have an 'email' field25 """26 def _authenticate(self, request, email=None, password=None, *args, **kwargs):27 if email is None:28 if 'username' not in kwargs or kwargs['username'] is None:29 return None30 clean_email = normalise_email(kwargs['username'])31 else:32 clean_email = normalise_email(email)33 # Check if we're dealing with an email address34 if '@' not in clean_email:35 return None36 # Since Django doesn't enforce emails to be unique, we look for all37 # matching users and try to authenticate them all. Note that we38 # intentionally allow multiple users with the same email address39 # (has been a requirement in larger system deployments),40 # we just enforce that they don't share the same password.41 # We make a case-insensitive match when looking for emails.42 matching_users = User.objects.filter(email__iexact=clean_email)43 authenticated_users = [44 user for user in matching_users if (user.check_password(password) and self.user_can_authenticate(user))]45 if len(authenticated_users) == 1:46 # Happy path47 return authenticated_users[0]48 elif len(authenticated_users) > 1:49 # This is the problem scenario where we have multiple users with50 # the same email address AND password. We can't safely authenticate51 # either.52 raise User.MultipleObjectsReturned(53 "There are multiple users with the given email address and "54 "password")55 return None56 def authenticate(self, *args, **kwargs):...

Full Screen

Full Screen

forms.py

Source:forms.py Github

copy

Full Screen

...28class RegisterForm(UserCreationForm):29 class Meta:30 model = Account31 fields = ('email','username','password1','password2','currency')32 def clean_email(self):33 if self.is_valid():34 clean_email = self.cleaned_data['email']35 try:36 exist_email = Account.objects.get(email = clean_email)37 if exist_email:38 raise forms.ValidationError('{} has been registered by others'.format(clean_email))39 except:40 return clean_email41class UpdateDetailForm(forms.ModelForm):42 class Meta:43 model = Account44 fields = ('email','username')45 def clean_email(self):46 if self.is_valid():47 email = self.cleaned_data.get('email')48 try:49 email_exist = Account.objects.get(email = email).exclude(pk = self.instance.id)50 if email_exist:51 raise forms.ValidationError('Email of "{}" has been registered'.format(email))52 except:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kiwi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful