我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用django.core.validators.EmailValidator()。
def validate_mailto(self, parts, verify_exists=False): """ Validates a mailto URL, by using Django's EmailValidator. `verify_exists` does nothing at this time. :param parts: :param verify_exists: :return: """ validator = EmailValidator() try: validator(parts['path']) except ValidationError: return False else: return True
def is_email(string): from django.core.exceptions import ValidationError from django.core.validators import EmailValidator validator = EmailValidator() try: validator(string) except ValidationError: return False return True
def login(email: str, password: str) -> tuple: allowed_domains = AD_ALLOWED_DOMAINS + [AD_REALM] validate_email = EmailValidator(whitelist=allowed_domains) if '@' in email: try: validate_email(email) except ValidationError as e: raise ADLoginError({'error_message': str(e)}) username, domain = email.split('@') else: # If no @ used then try to use AD_DEFAULT_DOMAIN username, domain = email, AD_DEFAULT_DOMAIN email = '{username}@{domain}'.format( username=username, domain=domain ) if domain not in allowed_domains: raise ADLoginError({'error_message': 'Invalid domain in e-mail'}) try: checkPassword(username, password, "", AD_REALM) except BasicAuthError as e: errmsg, _junk = e.args if errmsg == "Cannot contact any KDC for requested realm": errmsg = "Error connecting to KERBEROS server" raise KerberosLoginError({"error_message": errmsg}) elif errmsg == "Decrypt integrity check failed": errmsg = "KERBEROS account or password incorrect" raise KerberosLoginError({"error_message": errmsg}) else: raise KerberosLoginError({"error_message": errmsg}) except Exception as e: raise ADLoginError({'error_message': str(e)}) # Lookup email and username from AD (ldap_email, ldap_fullname) = None, None try: ldap_email, ldap_fullname = do_ldap_search(username, password) except Exception as e: pass # Return a fullname property if available from LDAP query. if ldap_fullname: username = ldap_fullname if ldap_email: email = ldap_email return (email, username)