Skip to content

Commit

Permalink
Send email on commit (#355)
Browse files Browse the repository at this point in the history
* send email on commit

* adopt test class to on_commit, see: https://stackoverflow.com/q/42585224/

* missing setting fix
  • Loading branch information
adibo authored and joshblum committed Feb 23, 2019
1 parent 3ae06c7 commit ae105ff
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions registration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ def create_inactive_user(self, site, new_user=None, send_email=True,
registration_profile = self.create_profile(
new_user, **profile_info)

if send_email:
registration_profile.send_activation_email(site, request)
# send email only if desired and transaction succeeds
if send_email:
transaction.on_commit(
lambda: registration_profile.send_activation_email(
site, request)
)

return new_user

Expand Down
4 changes: 2 additions & 2 deletions registration/tests/default_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core import mail
from django.test import TestCase
from django.test import TransactionTestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.urls import reverse
Expand All @@ -16,7 +16,7 @@

@override_settings(ROOT_URLCONF='test_app.urls_default',
ACCOUNT_ACTIVATION_DAYS=7)
class DefaultBackendViewTests(TestCase):
class DefaultBackendViewTests(TransactionTestCase):
"""
Test the default registration backend.
Expand Down
4 changes: 2 additions & 2 deletions registration/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.core import mail
from django.core import management
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test import TransactionTestCase
from django.test import override_settings
from django.utils import six
from django.utils.timezone import now as datetime_now
Expand All @@ -27,7 +27,7 @@
REGISTRATION_DEFAULT_FROM_EMAIL='[email protected]',
REGISTRATION_EMAIL_HTML=True,
DEFAULT_FROM_EMAIL='[email protected]')
class RegistrationModelTests(TestCase):
class RegistrationModelTests(TransactionTestCase):
"""
Test the model and manager used in the default backend.
Expand Down
2 changes: 2 additions & 0 deletions test_app/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@
('admin1', '[email protected]'),
('admin2', '[email protected]'),
)

ACCOUNT_ACTIVATION_DAYS = 7

0 comments on commit ae105ff

Please sign in to comment.