Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Bump versions and use emailit instead of current solution for the emails

Adam Bogdał1b481e0c0069
diff --git a/saleor/communication/__init__.py b/saleor/communication/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/saleor/communication/mail.py b/saleor/communication/mail.py
deleted file mode 100644
index 6426f4b5a2..0000000000
--- a/saleor/communication/mail.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from django.conf import settings
-from django.core.mail.message import EmailMultiAlternatives
-from django.template import Context
-from django.template.loader import get_template
-from django.template.loader_tags import BlockNode
-
-BLOCKS = [SUBJECT, TEXT, HTML] = 'subject', 'text', 'html' # template blocks
-
-
-def send_email(address, template_name, context=None):
- """Renders template blocks and sends an email."""
- blocks = render_blocks(template_name=template_name, context=context or {})
- message = EmailMultiAlternatives(
- subject=blocks[SUBJECT],
- body=blocks[TEXT],
- from_email=settings.DEFAULT_FROM_EMAIL,
- to=[address])
- if HTML in blocks:
- message.attach_alternative(blocks[HTML], 'text/html')
- message.send()
-
-
-def render_blocks(template_name, context):
- """Renders BLOCKS from template. Block needs to be a top level node."""
- context = Context(context)
- template = get_template(template_name=template_name)
- return dict((node.name, node.render(context)) for node in template
- if isinstance(node, BlockNode) and node.name in BLOCKS)
diff --git a/saleor/communication/tests.py b/saleor/communication/tests.py
deleted file mode 100644
index 2914604114..0000000000
--- a/saleor/communication/tests.py
+++ /dev/null
@@ -1,73 +0,0 @@
-from unittest import TestCase
-
-from django.template.loader_tags import BlockNode
-from mock import Mock, patch, sentinel
-
-from .mail import SUBJECT, TEXT, HTML, send_email, render_blocks
-
-
-class SendEmailTestCase(TestCase):
-
- def setUp(self):
- patcher = patch('saleor.communication.mail.settings')
- self.settings_mock = patcher.start()
- patcher = patch('saleor.communication.mail.render_blocks')
- self.render_mock = patcher.start()
- patcher = patch('saleor.communication.mail.EmailMultiAlternatives')
- self.email_mock = patcher.start()
-
- self.settings_mock.DEFAULT_FROM_EMAIL = sentinel.from_email
-
- def test_sending_email_without_html(self):
- """Html content is not attached when html block is missing"""
- self.render_mock.return_value = {SUBJECT: sentinel.subject,
- TEXT: sentinel.text}
- send_email(address=sentinel.address,
- template_name=sentinel.template_name,
- context=sentinel.context)
- self.assert_email_constructed()
- self.email_mock().send.assert_called_once()
-
- def test_sending_email_with_html(self):
- """Html content is attached when html block present"""
- self.render_mock.return_value = {SUBJECT: sentinel.subject,
- TEXT: sentinel.text,
- HTML: sentinel.html}
- send_email(address=sentinel.address,
- template_name=sentinel.template_name,
- context=sentinel.context)
- self.assert_email_constructed()
- self.email_mock().attach_alternative.assert_called_once_with(
- sentinel.html, 'text/html')
- self.email_mock().send.assert_called_once()
-
- def assert_email_constructed(self):
- self.email_mock.assert_called_once_with(
- subject=sentinel.subject,
- body=sentinel.text,
- from_email=sentinel.from_email,
- to=[sentinel.address])
-
- def tearDown(self):
- patch.stopall()
-
-
-class RenderBlocksTestCase(TestCase):
-
- @patch('saleor.communication.mail.get_template')
- @patch('saleor.communication.mail.Context')
- def test_block_rendering(self, context_mock, get_template_mock):
- """Template blocks are rendered with proper context"""
- html_block = Mock(spec=BlockNode)
- html_block.name = HTML
- some_block = Mock(spec=BlockNode)
- some_block.name = 'some_block'
- non_block = Mock()
- get_template_mock.return_value = [html_block, some_block, non_block]
- blocks = render_blocks(template_name=sentinel.template_name,
- context=sentinel.context)
- context_mock.assert_called_once_with(sentinel.context)
- html_block.render.assert_called_once_with(context_mock())
- some_block.render.assert_not_called()
- non_block.render.assert_not_called()
- self.assertEquals(blocks, {HTML: html_block.render()})
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 8aa0800d10..8c92b180e5 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
from decimal import Decimal
from uuid import uuid4
+import emailit.api
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.validators import MinValueValidator, MaxValueValidator
@@ -18,7 +19,6 @@ from payments.models import BasePayment
from prices import Price
from satchless.item import ItemSet, ItemLine
-from ..communication.mail import send_email
from ..core.utils import build_absolute_uri
from ..product.models import Product
from ..userprofile.models import Address, User
@@ -121,7 +121,8 @@ class Order(models.Model, ItemSet):
payment_url = build_absolute_uri(
reverse('order:details', kwargs={'token': self.token}))
context = {'payment_url': payment_url}
- send_email(email, 'order/emails/confirm_email.txt', context)
+
+ emailit.api.send_mail([email], context, 'order/emails/confirm_email')
def get_last_payment_status(self):
last_payment = self.payments.last()
@@ -344,7 +345,8 @@ class Payment(BasePayment):
order_url = build_absolute_uri(
reverse('order:details', kwargs={'token': self.order.token}))
context = {'order_url': order_url}
- send_email(email, 'order/payment/emails/confirm_email.txt', context)
+ emailit.api.send_mail(
+ [email], context, 'order/payment/emails/confirm_email')
def get_purchased_items(self):
items = [PurchasedItem(
diff --git a/saleor/order/templates/order/emails/confirm_email.txt b/saleor/order/templates/order/emails/confirm_email.body.txt
similarity index 62%
rename from saleor/order/templates/order/emails/confirm_email.txt
rename to saleor/order/templates/order/emails/confirm_email.body.txt
index b198c1dd86..420073b470 100644
--- a/saleor/order/templates/order/emails/confirm_email.txt
+++ b/saleor/order/templates/order/emails/confirm_email.body.txt
@@ -1,8 +1,6 @@
{% load i18n %}
-{% block subject %}{% trans "Order details" %}{% endblock %}
-
-{% block text %}{% blocktrans %}Hi!
+{% block content %}{% blocktrans %}Hi!
Thank you for your order. To see your payment details please visit:
{{ payment_url }}
diff --git a/saleor/order/templates/order/emails/confirm_email.subject.txt b/saleor/order/templates/order/emails/confirm_email.subject.txt
new file mode 100644
index 0000000000..6239b9c4b5
--- /dev/null
+++ b/saleor/order/templates/order/emails/confirm_email.subject.txt
@@ -0,0 +1,2 @@
+{% load i18n %}
+{% trans "Order details" %}
\ No newline at end of file
diff --git a/saleor/order/templates/order/payment/emails/confirm_email.txt b/saleor/order/templates/order/payment/emails/confirm_email.body.txt
similarity index 61%
rename from saleor/order/templates/order/payment/emails/confirm_email.txt
rename to saleor/order/templates/order/payment/emails/confirm_email.body.txt
index 787c92cf8f..e502e22865 100644
--- a/saleor/order/templates/order/payment/emails/confirm_email.txt
+++ b/saleor/order/templates/order/payment/emails/confirm_email.body.txt
@@ -1,8 +1,6 @@
{% load i18n %}
-{% block subject %}{% trans "Payment details" %}{% endblock %}
-
-{% block text %}{% blocktrans %}Hi!
+{% block content %}{% blocktrans %}Hi!
Thank you for your payment. To see your payment details please visit:
{{ order_url }}
diff --git a/saleor/order/templates/order/payment/emails/confirm_email.subject.txt b/saleor/order/templates/order/payment/emails/confirm_email.subject.txt
new file mode 100644
index 0000000000..730246779a
--- /dev/null
+++ b/saleor/order/templates/order/payment/emails/confirm_email.subject.txt
@@ -0,0 +1,2 @@
+{% load i18n %}
+{% trans "Payment details" %}
\ No newline at end of file
diff --git a/saleor/registration/forms.py b/saleor/registration/forms.py
index af345e2d15..9a87b50df6 100644
--- a/saleor/registration/forms.py
+++ b/saleor/registration/forms.py
@@ -1,5 +1,6 @@
from __future__ import unicode_literals
+import emailit.api
from django import forms
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth.forms import AuthenticationForm, SetPasswordForm
@@ -12,7 +13,6 @@ from .models import (
EmailChangeRequest,
ExternalUserData)
from .utils import get_client_class_for_service
-from ..communication.mail import send_email
class LoginForm(AuthenticationForm):
@@ -48,7 +48,7 @@ class RequestEmailConfirmationForm(forms.Form):
email = forms.EmailField()
- template = 'registration/emails/confirm_email.txt'
+ template = 'registration/emails/confirm_email'
def __init__(self, local_host=None, data=None):
self.local_host = local_host
@@ -59,7 +59,7 @@ class RequestEmailConfirmationForm(forms.Form):
request = self.create_request_instance()
confirmation_url = self.local_host + request.get_confirmation_url()
context = {'confirmation_url': confirmation_url}
- send_email(email, self.template, context)
+ emailit.api.send_mail([email], context, self.template)
def create_request_instance(self):
email = self.cleaned_data['email']
@@ -70,7 +70,7 @@ class RequestEmailConfirmationForm(forms.Form):
class RequestEmailChangeForm(RequestEmailConfirmationForm):
- template = 'registration/emails/change_email.txt'
+ template = 'registration/emails/change_email'
def __init__(self, user=None, *args, **kwargs):
self.user = user
diff --git a/saleor/registration/templates/registration/emails/change_email.txt b/saleor/registration/templates/registration/emails/change_email.body.txt
similarity index 71%
rename from saleor/registration/templates/registration/emails/change_email.txt
rename to saleor/registration/templates/registration/emails/change_email.body.txt
index fc27738756..70659c9fad 100644
--- a/saleor/registration/templates/registration/emails/change_email.txt
+++ b/saleor/registration/templates/registration/emails/change_email.body.txt
@@ -1,8 +1,6 @@
{% load i18n %}
-{% block subject %}{% trans "Confirm your email address" %}{% endblock %}
-
-{% block text %}{% blocktrans %}Hi!
+{% block content %}{% blocktrans %}Hi!
You have requested email change in saleor project. To continue visit the link below.
diff --git a/saleor/registration/templates/registration/emails/change_email.subject.txt b/saleor/registration/templates/registration/emails/change_email.subject.txt
new file mode 100644
index 0000000000..3155262047
--- /dev/null
+++ b/saleor/registration/templates/registration/emails/change_email.subject.txt
@@ -0,0 +1,2 @@
+{% load i18n %}
+{% trans "Confirm your email address" %}
\ No newline at end of file
diff --git a/saleor/registration/templates/registration/emails/confirm_email.txt b/saleor/registration/templates/registration/emails/confirm_email.body.txt
similarity index 71%
rename from saleor/registration/templates/registration/emails/confirm_email.txt
rename to saleor/registration/templates/registration/emails/confirm_email.body.txt
index f3c175fcd2..927540faa9 100644
--- a/saleor/registration/templates/registration/emails/confirm_email.txt
+++ b/saleor/registration/templates/registration/emails/confirm_email.body.txt
@@ -1,8 +1,6 @@
{% load i18n %}
-{% block subject %}{% trans "Confirm your email address" %}{% endblock %}
-
-{% block text %}{% blocktrans %}Hi!
+{% block content %}{% blocktrans %}Hi!
You have requested login access to saleor project. To continue visit the link below.
diff --git a/saleor/registration/templates/registration/emails/confirm_email.subject.txt b/saleor/registration/templates/registration/emails/confirm_email.subject.txt
new file mode 100644
index 0000000000..3155262047
--- /dev/null
+++ b/saleor/registration/templates/registration/emails/confirm_email.subject.txt
@@ -0,0 +1,2 @@
+{% load i18n %}
+{% trans "Confirm your email address" %}
\ No newline at end of file
diff --git a/saleor/settings.py b/saleor/settings.py
index 749226167c..f2711e20f4 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -104,6 +104,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sitemaps',
+ 'django.contrib.sites',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.auth',
@@ -123,6 +124,7 @@ INSTALLED_APPS = [
'babeldjango',
'django_images',
'django_prices',
+ 'emailit',
'mptt',
'payments',
'selectable'
diff --git a/saleor/tests.py b/saleor/tests.py
index 981dc38c13..9286175753 100644
--- a/saleor/tests.py
+++ b/saleor/tests.py
@@ -8,7 +8,6 @@ if hasattr(django, 'setup'):
TEST_MODULES = [
'saleor.cart.tests',
'saleor.checkout.tests',
- 'saleor.communication.tests',
'saleor.core.tests',
#'saleor.delivery.tests',
'saleor.order.tests',
diff --git a/setup.py b/setup.py
index 62d55165af..843d831434 100755
--- a/setup.py
+++ b/setup.py
@@ -19,10 +19,11 @@ setup(
'BabelDjango>=0.2,<0.3a0',
'Django>=1.7',
'dj_database_url>=0.3.0',
- 'django-images>=0.4,<0.5a0',
+ 'django-emailit>=0.2.1',
+ 'django-images>=0.4.2,<0.5a0',
'django-model-utils>=2.0.0,<2.1a0',
'django-mptt>=0.7.1',
- 'django-offsite-storage>=0.0.3',
+ 'django-offsite-storage>=0.0.5',
'django-payments>=0.6.1,<0.7a0',
'django-prices>=0.3.3,<0.4a0',
'django-selectable==0.8.0',