public
Last active

django-full-email

  • Download Gist
README.rst
reStructuredText

Full e-mail address validation for Django

Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, joe@hacker.com is accepted.

On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:

Joe Hacker <joe@hacker.com>

This package adds support for validating full e-mail addresses.

Database model example

from django import models
from full_email.models import FullEmailField

class MyModel(models.Model):
    email = FullEmailField()

Forms example

from django import forms
from full_email.formfields import FullEmailField

class MyForm(forms.Form):
    email = FullEmailField(label='E-mail address')
full_email/__init__.py
Python

          
full_email/formfields.py
Python
1 2 3 4 5 6 7
from django import forms
 
from .validators import validate_full_email
 
 
class FullEmailField(forms.EmailField):
default_validators = [validate_full_email]
full_email/models.py
Python
1 2 3 4 5 6 7 8 9 10 11 12
from django.db import models
from . import formfields
from .validators import validate_full_email
 
 
class FullEmailField(models.EmailField):
default_validators = [validate_full_email]
 
def formfield(self, **kwargs):
defaults = {'form_class': formfields.FullEmailField}
defaults.update(kwargs)
return super(FullEmailField, self).formfield(**defaults)
full_email/tests.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# -*- encoding: utf-8 -*-
 
from unittest import TestCase
 
from django.core.validators import email_re, EmailValidator, ValidationError
from . import validators
from nose.tools import raises
 
 
class ValidateFullEmail_Tests(TestCase):
def test_normal_valid_email(self):
"""A normal valid e-mail address validates correctly"""
validators.validate_full_email(u'john@doe.com')
 
def test_fullname(self):
"""A valid e-mail address with a full name validates correctly"""
validators.validate_full_email(u'John Doe <john@doe.com>')
 
def test_quoted_fullname(self):
"""A valid e-mail address with a quoted full name validates correctly"""
validators.validate_full_email(u'"Doe, John" <john@doe.com>')
 
def test_unicode_fullname(self):
"""A valid e-mail address with a Unicode name validates correctly"""
validators.validate_full_email(u'"Übergeek, Jörg" <joerg@ubergeek.com>')
 
@raises(ValidationError)
def test_incomplete_domain(self):
"""An incomplete domain in an e-mail address fails validation"""
validators.validate_full_email(u'joerg@ubergeek')
 
@raises(ValidationError)
def test_fullname_and_incomplete_domain(self):
"""An incomplete domain in a full e-mail address fails validation"""
validators.validate_full_email(u'Jörg <joerg@ubergeek>')
full_email/validators.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
from django.core.validators import email_re, EmailValidator, ValidationError
from django.utils.translation import ugettext_lazy as _
 
 
class FullEmailValidator(EmailValidator):
def __call__(self, value):
try:
super(FullEmailValidator, self).__call__(value)
except ValidationError, e:
# Trivial case failed. Try for possible Full Name <email@address>
if value and u'<' in value and value.endswith(u'>'):
parts = value.rsplit(u'<', 1)
fullname_part = parts[0].rstrip()
email_part = parts[1][:-1]
super(EmailValidator, self).__call__(email_part)
else:
raise
 
 
validate_full_email = FullEmailValidator(email_re,
_(u'Enter a valid e-mail address.'),
'invalid')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.