Skip to content

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

Fix email template sent when customer is created from dashboard

Aleksander Spyrab1c1e79bcda4
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index c34e36999b..116055e4f6 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -10,7 +10,7 @@ from django.utils.translation import pgettext_lazy
from ...account.models import CustomerNote, User
from ...core.utils import get_paginator_items
-from ..emails import send_set_password_email
+from ..emails import send_set_password_customer_email
from ..views import staff_member_required
from .filters import UserFilter
from .forms import CustomerDeleteForm, CustomerForm, CustomerNoteForm
@@ -61,7 +61,7 @@ def customer_create(request):
form.save()
msg = pgettext_lazy(
'Dashboard message', 'Added customer %s') % customer
- send_set_password_email.delay(customer.pk)
+ send_set_password_customer_email.delay(customer.pk)
messages.success(request, msg)
return redirect('dashboard:customer-details', pk=customer.pk)
ctx = {'form': form, 'customer': customer}
diff --git a/saleor/dashboard/emails.py b/saleor/dashboard/emails.py
index 7788abc624..ae0ed8a68a 100644
--- a/saleor/dashboard/emails.py
+++ b/saleor/dashboard/emails.py
@@ -11,6 +11,25 @@ from ..core.emails import get_email_base_context
from ..core.utils import build_absolute_uri
+def _send_set_password_email(pk, template_name):
+ user = User.objects.get(pk=pk)
+ uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
+ token = default_token_generator.make_token(user)
+ password_set_url = build_absolute_uri(
+ reverse(
+ 'account:reset-password-confirm',
+ kwargs={
+ 'token': token,
+ 'uidb64': uid}))
+ ctx = get_email_base_context()
+ ctx['password_set_url'] = password_set_url
+ send_templated_mail(
+ template_name=template_name,
+ from_email=settings.DEFAULT_FROM_EMAIL,
+ recipient_list=[user.email],
+ context=ctx)
+
+
@shared_task
def send_set_password_email(staff_pk):
staff = User.objects.get(pk=staff_pk)
@@ -19,7 +38,9 @@ def send_set_password_email(staff_pk):
password_set_url = build_absolute_uri(
reverse(
'account:reset-password-confirm',
- kwargs={'token': token, 'uidb64': uid}))
+ kwargs={
+ 'token': token,
+ 'uidb64': uid}))
ctx = get_email_base_context()
ctx['password_set_url'] = password_set_url
send_templated_mail(
@@ -29,6 +50,11 @@ def send_set_password_email(staff_pk):
context=ctx)
+@shared_task
+def send_set_password_customer_email(pk):
+ _send_set_password_email(pk, 'dashboard/customer/set_password')
+
+
@shared_task
def send_promote_customer_to_staff_email(staff_pk):
staff = User.objects.get(pk=staff_pk)
diff --git a/templates/templated_email/dashboard/customer/set_password.email b/templates/templated_email/dashboard/customer/set_password.email
new file mode 100644
index 0000000000..8ec7121a1e
--- /dev/null
+++ b/templates/templated_email/dashboard/customer/set_password.email
@@ -0,0 +1,21 @@
+{% load i18n %}
+{% block subject %}{% blocktrans context "Set password for customer e-mail title" %}Hello from {{ site_name }}!{% endblocktrans %}{% endblock %}
+
+{% block plain %}
+{% blocktrans context "Set password for customer e-mail text" %}
+Hi!
+
+You're receiving this e-mail because you have to set password for your customer account at {{ domain }}.
+Click the link below to reset your password.
+{% endblocktrans %}
+
+{{ password_set_url }}
+
+{% blocktrans context "Set password for customer e-mail text" %}
+This is an automatically generated email, please do not reply.
+{% endblocktrans %}
+{% endblock %}
+
+{% block html %}
+{% include 'templated_email/compiled/set_customer_password.html' %}
+{% endblock %}
diff --git a/templates/templated_email/source/set_customer_password.mjml b/templates/templated_email/source/set_customer_password.mjml
new file mode 100644
index 0000000000..0f94d5df26
--- /dev/null
+++ b/templates/templated_email/source/set_customer_password.mjml
@@ -0,0 +1,30 @@
+<mjml>
+ <mj-head>
+ <mj-include path="./shared/styles.mjml" />
+ </mj-head>
+ <mj-body>
+ <mj-raw>
+ {% load i18n %}
+ </mj-raw>
+ <mj-include path="./shared/header.mjml" />
+ <mj-section>
+ <mj-column>
+ <mj-text font-size="16px">
+ {% trans "Hi!" context "Set password for customer e-mail text" %}
+ </mj-text>
+ <mj-text>
+ {% blocktrans trimmed context "Set password for customer e-mail text" %}
+ You're receiving this e-mail because you have to set password for your customer account at {{ domain }}.<br/>
+ Click the link below to set up your password.
+ {% endblocktrans %}
+ </mj-text>
+ <mj-text>
+ <a href="{{ password_set_url }}">
+ {{ password_set_url }}
+ </a>
+ </mj-text>
+ </mj-column>
+ </mj-section>
+ <mj-include path="./shared/footer.mjml" />
+ </mj-body>
+</mjml>
diff --git a/tests/dashboard/test_customer.py b/tests/dashboard/test_customer.py
index 0beb025619..b95c53c7f5 100644
--- a/tests/dashboard/test_customer.py
+++ b/tests/dashboard/test_customer.py
@@ -1,10 +1,18 @@
import json
+from django.contrib.auth.tokens import default_token_generator
+from django.core import mail
+from django.templatetags.static import static
from django.urls import reverse
+from django.utils.encoding import force_bytes
+from django.utils.http import urlsafe_base64_encode
+from templated_email import send_templated_mail
from saleor.account.models import CustomerNote, User
+from saleor.core.utils import build_absolute_uri
from saleor.dashboard.customer.forms import (
CustomerDeleteForm, CustomerForm, CustomerNoteForm)
+from saleor.settings import DEFAULT_FROM_EMAIL
def test_ajax_users_list(admin_client, admin_user, customer_user):
@@ -120,3 +128,58 @@ def test_form_delete_customer(
staff_user = User.objects.get(pk=staff_user.pk)
form = CustomerDeleteForm({}, instance=another_staff_user, user=staff_user)
assert form.is_valid()
+
+
+def test_add_customer_and_set_password(admin_client):
+ url = reverse('dashboard:customer-create')
+ data = {
+ 'first_name': 'John',
+ 'last_name': 'Doe',
+ 'email': '[email protected]',
+ 'is_active': True}
+ response = admin_client.post(url, data)
+ assert response.status_code == 302
+ new_user = User.objects.get(email=data['email'])
+ assert new_user.first_name == data['first_name']
+ assert new_user.last_name == data['last_name']
+ assert not new_user.password
+ uid = urlsafe_base64_encode(force_bytes(new_user.pk)).decode()
+ token = default_token_generator.make_token(new_user)
+ response = admin_client.get(
+ reverse(
+ 'account:reset-password-confirm',
+ kwargs={
+ 'uidb64': uid,
+ 'token': token}))
+ assert response.status_code == 302
+ post_data = {'new_password1': 'password', 'new_password2': 'password'}
+ response = admin_client.post(response['Location'], post_data)
+ assert response.status_code == 302
+ assert response['Location'] == reverse('account:reset-password-complete')
+ new_user = User.objects.get(email=data['email'])
+ assert new_user.has_usable_password()
+
+
+def test_send_set_password_customer_email(customer_user, site_settings):
+ site = site_settings.site
+ uid = urlsafe_base64_encode(force_bytes(customer_user.pk)).decode()
+ token = default_token_generator.make_token(customer_user)
+ logo_url = build_absolute_uri(static('images/logo-document.svg'))
+ password_set_url = build_absolute_uri(
+ reverse(
+ 'account:reset-password-confirm',
+ kwargs={
+ 'token': token,
+ 'uidb64': uid}))
+ ctx = {
+ 'logo_url': logo_url,
+ 'password_set_url': password_set_url,
+ 'site_name': site.name}
+ send_templated_mail(
+ template_name='dashboard/customer/set_password',
+ from_email=DEFAULT_FROM_EMAIL,
+ recipient_list=[customer_user.email],
+ context=ctx)
+ assert len(mail.outbox) == 1
+ sended_message = mail.outbox[0].body
+ assert password_set_url in sended_message