'Enforce email restrictions (if applicable)'

def clean_email(self):

    email = self.cleaned_data['email']
    if (settings.REGISTRATION_EMAIL_PATTERNS_ALLOWED is not None):
        allowed_patterns = settings.REGISTRATION_EMAIL_PATTERNS_ALLOWED
        if (not any((re.match((pattern + '$'), email) for pattern in allowed_patterns))):
            if (not CourseEnrollmentAllowed.objects.filter(email=email).exists()):
                raise ValidationError(_('Unauthorized    email    address.'))
    if User.objects.filter(email__iexact=email).exists():
        raise ValidationError(_('It    looks    like    {email}    belongs    to    an    existing    account.    Try    again    with    a    different    email    address.').format(email=email))
    return email
