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

Commit

Permalink
Add a default error message.
Browse files Browse the repository at this point in the history
When using multiple email field declarations it can be convienent to set
the error message once that you wish to use.
  • Loading branch information
sjdines committed Mar 21, 2017
1 parent 60f055f commit 1c041ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ optionally you can configure the BDE API timeout in seconds (default 5)
BDEA_TIMEOUT = 2
```

A default error message can be set globally for the validation checking:

```python
BDEA_MESSAGE = '<blocked email message>'
```

Adding to your models
---------------------

Expand Down Expand Up @@ -86,4 +92,4 @@ try:
validate_disposable_email(email)
except ValidationError:
pass
```
```
2 changes: 2 additions & 0 deletions disposable_email_checker/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class DisposableEmailChecker():
def __init__(self, message=None, code=None, whitelist=None):
if message is not None:
self.message = message
elif hasattr(settings, 'BDEA_MESSAGE'):
self.message = getattr(settings, 'BDEA_MESSAGE')
if code is not None:
self.code = code
if whitelist is not None:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.utils import six
from disposable_email_checker import validators
from disposable_email_checker.emails import email_domain_loader

Expand All @@ -18,3 +19,17 @@ def setUp(self):
def test_validator(self):
self.assertRaises(ValidationError, validators.validate_disposable_email, self.disposable_email)
validators.validate_disposable_email(self.not_a_disposable_email)

def test_validator_messages(self):
with self.assertRaisesMessage(ValidationError, 'Blocked email provider.'):
validators.validate_disposable_email(self.disposable_email)

with self.settings(BDEA_MESSAGE='Test message'):
# As the `BDEA_MESSAGE` gets calculated at run time we need to reload the module that
# defines the message.
six.moves.reload_module(validators)
with self.assertRaisesMessage(ValidationError, 'Test message'):
validators.validate_disposable_email(self.disposable_email)

# We need to reload the module again to set the default functionality back to normal.
six.moves.reload_module(validators)

0 comments on commit 1c041ed

Please sign in to comment.