Homogenize email-validation into one utility. Resolve #2143
This commit is contained in:
parent
8f85bc1873
commit
c8b056d06d
2 changed files with 15 additions and 60 deletions
|
|
@ -29,6 +29,8 @@ from django.conf import settings
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
|
from django.core.validators import validate_email as django_validate_email
|
||||||
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
from evennia.utils import logger
|
from evennia.utils import logger
|
||||||
|
|
||||||
_MULTIMATCH_TEMPLATE = settings.SEARCH_MULTIMATCH_TEMPLATE
|
_MULTIMATCH_TEMPLATE = settings.SEARCH_MULTIMATCH_TEMPLATE
|
||||||
|
|
@ -906,69 +908,25 @@ def to_str(text, session=None):
|
||||||
|
|
||||||
def validate_email_address(emailaddress):
|
def validate_email_address(emailaddress):
|
||||||
"""
|
"""
|
||||||
Checks if an email address is syntactically correct.
|
Checks if an email address is syntactically correct. Makes use
|
||||||
|
of the django email-validator for consistency.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
emailaddress (str): Email address to validate.
|
emailaddress (str): Email address to validate.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
is_valid (bool): If this is a valid email or not.
|
bool: If this is a valid email or not.
|
||||||
|
|
||||||
Notes.
|
|
||||||
(This snippet was adapted from
|
|
||||||
http://commandline.org.uk/python/email-syntax-check.)
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
emailaddress = r"%s" % emailaddress
|
|
||||||
|
|
||||||
domains = (
|
|
||||||
"aero",
|
|
||||||
"asia",
|
|
||||||
"biz",
|
|
||||||
"cat",
|
|
||||||
"com",
|
|
||||||
"coop",
|
|
||||||
"edu",
|
|
||||||
"gov",
|
|
||||||
"info",
|
|
||||||
"int",
|
|
||||||
"jobs",
|
|
||||||
"mil",
|
|
||||||
"mobi",
|
|
||||||
"museum",
|
|
||||||
"name",
|
|
||||||
"net",
|
|
||||||
"org",
|
|
||||||
"pro",
|
|
||||||
"tel",
|
|
||||||
"travel",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Email address must be more than 7 characters in total.
|
|
||||||
if len(emailaddress) < 7:
|
|
||||||
return False # Address too short.
|
|
||||||
|
|
||||||
# Split up email address into parts.
|
|
||||||
try:
|
try:
|
||||||
localpart, domainname = emailaddress.rsplit("@", 1)
|
django_validate_email(str(emailaddress))
|
||||||
host, toplevel = domainname.rsplit(".", 1)
|
except DjangoValidationError:
|
||||||
except ValueError:
|
return False
|
||||||
return False # Address does not have enough parts.
|
except Exception:
|
||||||
|
logger.log_trace()
|
||||||
# Check for Country code or Generic Domain.
|
return False
|
||||||
if len(toplevel) != 2 and toplevel not in domains:
|
|
||||||
return False # Not a domain name.
|
|
||||||
|
|
||||||
for i in "-_.%+.":
|
|
||||||
localpart = localpart.replace(i, "")
|
|
||||||
for i in "-_.":
|
|
||||||
host = host.replace(i, "")
|
|
||||||
|
|
||||||
if localpart.isalnum() and host.isalnum():
|
|
||||||
return True # Email address is fine.
|
|
||||||
else:
|
else:
|
||||||
return False # Email address has funny characters.
|
return True
|
||||||
|
|
||||||
|
|
||||||
def inherits_from(obj, parent):
|
def inherits_from(obj, parent):
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,8 @@ They can employ more paramters at your leisure.
|
||||||
import re as _re
|
import re as _re
|
||||||
import pytz as _pytz
|
import pytz as _pytz
|
||||||
import datetime as _dt
|
import datetime as _dt
|
||||||
from django.core.exceptions import ValidationError as _error
|
|
||||||
from django.core.validators import validate_email as _val_email
|
|
||||||
from evennia.utils.ansi import strip_ansi
|
from evennia.utils.ansi import strip_ansi
|
||||||
from evennia.utils.utils import string_partial_matching as _partial
|
from evennia.utils.utils import string_partial_matching as _partial, validate_email_address
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
_TZ_DICT = {str(tz): _pytz.timezone(tz) for tz in _pytz.common_timezones}
|
_TZ_DICT = {str(tz): _pytz.timezone(tz) for tz in _pytz.common_timezones}
|
||||||
|
|
@ -210,9 +208,8 @@ def timezone(entry, option_key="Timezone", **kwargs):
|
||||||
def email(entry, option_key="Email Address", **kwargs):
|
def email(entry, option_key="Email Address", **kwargs):
|
||||||
if not entry:
|
if not entry:
|
||||||
raise ValueError("Email address field empty!")
|
raise ValueError("Email address field empty!")
|
||||||
try:
|
valid = validate_email_address(entry)
|
||||||
_val_email(str(entry)) # offloading the hard work to Django!
|
if not valid:
|
||||||
except _error:
|
|
||||||
raise ValueError(f"That isn't a valid {option_key}!")
|
raise ValueError(f"That isn't a valid {option_key}!")
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue