Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
change email backends, closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed May 2, 2016
1 parent 4468524 commit 5a8b4d5
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 12 deletions.
11 changes: 11 additions & 0 deletions apostello/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.conf import settings

from allauth.account.adapter import DefaultAccountAdapter


class ApostelloAccountAdapter(DefaultAccountAdapter):
def get_from_email(self):
"""Override to use SiteConfiguration, then fall back to settings."""
from site_config.models import SiteConfiguration
s = SiteConfiguration.get_solo()
return s.email_from or settings.EMAIL_FROM
21 changes: 21 additions & 0 deletions apostello/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.core.mail.backends.smtp import EmailBackend


class ApostelloEmailBackend(EmailBackend):
"""Email backend that reads settings from SiteConfiguration model."""

@staticmethod
def _db_or_setting(db_val, setting_val):
"""Default to value from settings if db value is None."""
if db_val:
return db_val
return setting_val

def __init__(self, *args, **kwargs):
super(ApostelloEmailBackend, self).__init__(*args, **kwargs)
from site_config.models import SiteConfiguration
s = SiteConfiguration.get_solo()
self.host = self._db_or_setting(s.email_host, self.host)
self.port = self._db_or_setting(s.email_port, self.port)
self.username = self._db_or_setting(s.email_username, self.username)
self.password = self._db_or_setting(s.email_password, self.password)
8 changes: 1 addition & 7 deletions apostello/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,8 @@ def send_async_mail(subject, body, to):
# settings.py instead
from site_config.models import SiteConfiguration
s = SiteConfiguration.get_solo()
conn = get_connection(
host=s.email_host or None,
port=s.email_port or None,
username=s.email_username or None,
password=s.email_password or None,
)
from_ = s.email_from or settings.EMAIL_FROM
send_mail(subject, body, from_, to, connection=conn)
send_mail(subject, body, from_, to)


def notify_office_mail(subject, body):
Expand Down
2 changes: 2 additions & 0 deletions settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
}

# email settings
EMAIL_BACKEND = 'apostello.mail.ApostelloEmailBackend'
EMAIL_USE_TLS = True
# these email settings can be overridden in the SiteConfiguration model
# this also allows for these settings to be left blank on initial setup and
Expand All @@ -147,6 +148,7 @@
EMAIL_PORT = int(os.environ.get('DJANGO_EMAIL_HOST_port', 587))

# social login settings
ACCOUNT_ADAPTER = 'apostello.account.ApostelloAccountAdapter'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
Expand Down
5 changes: 3 additions & 2 deletions settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def show_toolbar(request):
}

# overwrite static files (use white noise instead of runserver)
MIDDLEWARE_CLASSES = ['whitenoise.middleware.WhiteNoiseMiddleware',
] + MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = [
'whitenoise.middleware.WhiteNoiseMiddleware',
] + MIDDLEWARE_CLASSES
STATIC_ROOT = BASE_DIR + '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Expand Down
5 changes: 3 additions & 2 deletions settings/heroku.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# add whitenoise to handle static files
MIDDLEWARE_CLASSES = ['whitenoise.middleware.WhiteNoiseMiddleware',
] + MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = [
'whitenoise.middleware.WhiteNoiseMiddleware',
] + MIDDLEWARE_CLASSES
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# read db url:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_apostello_allauth_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from apostello.account import ApostelloAccountAdapter
from site_config.models import SiteConfiguration


@pytest.mark.django_db
def test_get_email():
"""Test pulling email from settings and SiteConfiguration."""
s = SiteConfiguration.get_solo()
a = ApostelloAccountAdapter()
# test from db
s.email_from = 'test2@apostello.ninja'
s.save()
assert a.get_from_email() == 'test2@apostello.ninja'
# test from settings
s.email_from = ''
s.save()
assert a.get_from_email() == 'test@apostello.ninja'
21 changes: 21 additions & 0 deletions tests/test_apostello_email_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from apostello.mail import ApostelloEmailBackend
from site_config.models import SiteConfiguration


@pytest.mark.django_db
def test_apostello_mail_backend():
"""Test email backend pulling from settings and db."""
# get from Siteconfiguration
s = SiteConfiguration.get_solo()
s.get_solo()
s.email_host = 'smtp.test2.apostello'
s.save()
mail_backend = ApostelloEmailBackend()
assert mail_backend.host == 'smtp.test2.apostello'
# get from settings
s.email_host = ''
s.save()
mail_backend = ApostelloEmailBackend()
assert mail_backend.host == 'smtp.test.apostello'
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ setenv =
TWILIO_FROM_NUM=+15005550006
COUNTRY_CODE=44
TWILIO_SENDING_COST=0.04
DJANGO_FROM_EMAIL=test@apostello.ninja
DJANGO_EMAIL_HOST=smtp.test.apostello
deps = -r{toxinidir}/requirements_test.txt
commands =
xvfb-run -a py.test {toxinidir} -s --nomigrations --cov="." --cov-report="html" {posargs}
xvfb-run -a py.test {toxinidir} -s --nomigrations --cov="." --cov-report="html" --cov-report="term-missing" {posargs}

0 comments on commit 5a8b4d5

Please sign in to comment.