Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Add block-disposable-email.com support
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbassett committed Nov 4, 2015
1 parent 3cc36b5 commit baa4683
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion disposable_email_checker/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.1'
__version__ = '1.2.1'
24 changes: 20 additions & 4 deletions disposable_email_checker/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from django.utils.encoding import force_text
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from .emails import email_domain_loader
from django.core.urlresolvers import get_callable
from bdea.client import BDEAClient


class DisposableEmailChecker():
Expand All @@ -27,21 +28,36 @@ def __init__(self, message=None, code=None, whitelist=None):
self.whitelist = whitelist

self.emails = self._load_emails()
self.BDEA_APIKEY = getattr(settings, 'BDEA_APIKEY', None)
self.BDEA_TIMEOUT = getattr(settings, 'BDEA_TIMEOUT', 5)

def __call__(self, value):
value = force_text(value)
user_part, domain_part = value.rsplit('@', 1)

if domain_part not in self.whitelist:
if self.BDEA_APIKEY: # Validate using block-disposable-email.com
client = BDEAClient(self.BDEA_APIKEY, timeout=self.BDEA_TIMEOUT)
response = client.get_domain_status(domain_part)

if response.status() and response.is_disposable():
raise ValidationError(self.message, code=self.code)

"""
This will run if we are not using BDEA, we're out of BDEA credits,
there was an error contacting BDEA's servers or we did not get a
hit on BDEA. Basically always check using local list as a backup
"""
for email_group in self.chunk(self.emails, 20):
regex = "(.*" + "$)|(.*".join(email_group) + "$)"
if re.match(regex, value):
raise ValidationError(self.message, code=self.code)

def _load_emails(self):
return getattr(
settings, "DISPOSABLE_EMAIL_DOMAINS_LOADER", email_domain_loader
)()
loader = getattr(
settings, 'DEC_LOADER', 'disposable_email_checker.emails.email_domain_loader'
)
return get_callable(loader)()

def chunk(self, l, n):
return (l[i:i+n] for i in range(0, len(l), n))
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ mock>=1.0.1
flake8>=2.1.0
tox>=1.7.0
six>=1.10.0
block-disposable-email>=1.0.1

# Additional test requirements go here
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
django>=1.8.0
wheel==0.24.0
wheel==0.26.0
six>=1.10.0
# Additional requirements go here
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
],
include_package_data=True,
install_requires=[
'six'
'six==1.10.0',
'block-disposable-email==1.0.1',
],
license="BSD",
zip_safe=False,
keywords='DisposableEmailChecker',
classifiers=[
'Development Status :: 3 - Alpha',
'Development Status :: 5 - Production/Stable',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
Expand Down

0 comments on commit baa4683

Please sign in to comment.