Last active
February 6, 2017 14:44
-
-
Save filipeximenes/e537949f90b880f4d233ce33378d4d3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mock | |
from django.test import TestCase | |
from django.contrib.auth.models import User | |
class EmailTests(TestCase): | |
def setUp(self): | |
self.user = User.objects.create( | |
username='theusername', email='[email protected]', | |
first_name='Filipe', last_name='Ximenes', | |
password='1') | |
self.client.login(email=user.email, password='1') | |
@mock.patch('myapp.views.send_templated_mail') | |
def test_email_context_variables(self, send_templated_mail): | |
# assume '/complete-signup/' is the URL to our view | |
self.client.post('/complete-signup/') | |
kargs = send_templated_mail.call_args[1] | |
context = kwargs['context'] | |
self.assertEqual(context['username'], self.user.username) | |
self.assertEqual(context['full_name'], self.user.get_full_name()) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
# Get a copy of the default TEMPLATES value | |
RAISE_EXCEPTION_TEMPLATES = copy.deepcopy(settings.TEMPLATES) | |
# this is where the magic happens | |
class InvalidVarException(str): | |
def __new__(cls, *args, **kwargs): | |
return super().__new__(cls, '%s') | |
def __mod__(self, missing): | |
try: | |
missing_str = str(missing) | |
except: | |
missing_str = 'Failed to create string representation' | |
raise Exception('Unknown template variable %r %s' % (missing, missing_str)) | |
def __contains__(self, search): | |
return True | |
# set the value of string_if_invalid and template debug to True | |
RAISE_EXCEPTION_TEMPLATES[0]['OPTIONS']['string_if_invalid'] = InvalidVarException() | |
RAISE_EXCEPTION_TEMPLATES[0]['OPTIONS']['debug'] = True | |
from django.test import override_settings | |
@override_settings(TEMPLATES=RAISE_EXCEPTION_TEMPLATES) | |
def test_passes_all_email_variables(self): | |
self.client.post('/complete-signup/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
from templated_email import send_templated_mail | |
def complete_signup(request): | |
request.user.is_active = True | |
request.user.save() | |
send_templated_mail( | |
template_name='welcome', | |
from_email='[email protected]', | |
recipient_list=[request.user.email], | |
context={ | |
'username': request.user.username, | |
'full_name': request.user.get_full_name(), | |
}, | |
) | |
return HttpResponse('Signup Completed!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# templates/templated_emails/welcome.email #} | |
{% block subject %}Welcome {{ full_name }}!{% endblock %} | |
{% block plain %} | |
Hi {{ full_name }}! | |
You have successfuly completed the signup proccess. | |
Access our site using your username: {{ username }} | |
{% endblock %} | |
{% block html %} | |
<h1>Hi {{full_name}}!</h1> | |
<p>You have successfuly completed the signup proccess</p> | |
<p>Access our site using your username: {{ username }}</p> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment