# Path: apps/core/models.py
# class EmailDomain(models.Model):
#     """
#     denotes an email domain
#     - domain:    the email domain
#     - is_banned: banned status
#     """
#     domain = models.CharField(max_length=100, unique=True)
#     is_banned = models.BooleanField(default=True)


import re
import requests
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth.models import User
from apps.core.models import EmailDomain

# give pretty name for social account
def get_social_name(type):
    return social_name_map.get(type, type)


# check given email is available or not
def validate_email(email, exclude=''):
    if email == exclude:
        return True

    m = re.match(r'^([^@]+)@([^@]+)$', email)
    if not m:
        return False

    domain = m.group(2).strip().lower()
    if domain == 'sso.sparcs.org':
        return False
    elif EmailDomain.objects.filter(domain=domain, is_banned=True).exists():
        return False
    return User.objects.filter(email=email).count() == 0


# validate reCAPTCHA
def validate_recaptcha(response):
    if not settings.RECAPTCHA_SECRET:
        return True

    data = {
        'secret': settings.RECAPTCHA_SECRET,

# Path: apps/core/models.py
# class EmailDomain(models.Model):
#     """
#     denotes an email domain
#     - domain:    the email domain
#     - is_banned: banned status
#     """
#     domain = models.CharField(max_length=100, unique=True)
#     is_banned = models.BooleanField(default=True)


# Path: apps/core/backends/util.py
import re
import requests
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth.models import User
from apps.core.models import EmailDomain


# give pretty name for social account
def get_social_name(type):
    return social_name_map.get(type, type)


# check given email is available or not
def validate_email(email, exclude=''):
    if email == exclude:
        return True

    m = re.match(r'^([^@]+)@([^@]+)$', email)
    if not m:
        return False

    domain = m.group(2).strip().lower()
    if domain == 'sso.sparcs.org':
        return False
    elif EmailDomain.objects.filter(domain=domain, is_banned=True).exists():
        return False
    return User.objects.filter(email=email).count() == 0


# validate reCAPTCHA
def validate_recaptcha(response):
    if not settings.RECAPTCHA_SECRET:
        return True

    data = {
        'secret': settings.RECAPTCHA_SECRET,

        'response': response,