Skip to content

Commit cc7f8a9

Browse files
authored
Send a welcome mail on account creation: fixes #254
* Send a welcome mail on account creation * Add test
1 parent cf836da commit cc7f8a9

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

pytition/petition/forms.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .models import Signature, PetitionTemplate, Petition, Organization, PytitionUser, SlugModel
1010
from .widgets import SwitchField
11+
from .helpers import send_welcome_mail
1112

1213
import uuid
1314
import html
@@ -206,6 +207,8 @@ def clean(self):
206207
self.cleaned_data = cleaned_data
207208
return cleaned_data
208209

210+
def send_success_email(self):
211+
send_welcome_mail(self.cleaned_data)
209212

210213
class UpdateInfoForm(UserCreationForm):
211214
class Meta:

pytition/petition/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ def send_confirmation_email(request, signature):
8989
msg.attach_alternative(html_message, "text/html")
9090
msg.send(fail_silently=False)
9191

92+
# Send welcome mail on account creation
93+
def send_welcome_mail(user_infos):
94+
html_message = render_to_string("registration/confirmation_email.html", user_infos)
95+
message = strip_tags(html_message)
96+
with get_connection() as connection:
97+
msg = EmailMultiAlternatives(_("Account created !"),
98+
message, to=[user_infos["email"]], connection=connection,
99+
reply_to=[settings.DEFAULT_NOREPLY_MAIL])
100+
msg.attach_alternative(html_message, "text/html")
101+
msg.send(fail_silently=False)
102+
103+
92104

93105
# Generate a meta url for the HTML meta property
94106
def petition_detail_meta(request, petition_id):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% load i18n %}
2+
{% get_current_language as LANGUAGE_CODE %}
3+
<!DOCTYPE html>
4+
<html lang="{{ LANGUAGE_CODE }}">
5+
<head>
6+
<meta charset="UTF-8">
7+
</head>
8+
<body>
9+
10+
<h2>{% trans "Welcome" %} {{ firstname }}!</h2>
11+
12+
{% blocktrans %}
13+
You successfully created your account ! Your login is {{ username }}
14+
{% endblocktrans %}
15+
</body>
16+
</html>

pytition/petition/tests/tests_RegisterView.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.urls.exceptions import NoReverseMatch
44
from django.conf import settings
55
from django.contrib.auth import authenticate
6+
from django.core import mail
67

78
from petition.models import PytitionUser
89

@@ -171,9 +172,11 @@ def test_createPytitionUser(self):
171172
self.assertEqual(toto.user.email, data["email"])
172173
self.assertEqual(toto.user.first_name, data["first_name"])
173174
self.assertEqual(toto.user.last_name, data["last_name"])
175+
self.assertEqual(len(mail.outbox), 1)
176+
self.assertEqual(mail.outbox[0].subject, 'Account created !')
174177
# Let's try to authenticate, to check password is OK
175178
pu = authenticate(username=data['username'], password=data['password1'])
176179
self.assertNotEqual(pu, None)
177180
else:
178181
with self.assertRaises(NoReverseMatch):
179-
response = self.client.get(reverse("register"))
182+
response = self.client.get(reverse("register"))

pytition/petition/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from .forms import DeleteAccountForm, OrgCreationForm
3636
from .helpers import get_client_ip, get_session_user, petition_from_id
3737
from .helpers import check_petition_is_accessible
38-
from .helpers import send_confirmation_email, subscribe_to_newsletter
38+
from .helpers import send_confirmation_email, subscribe_to_newsletter, send_welcome_mail
3939
from .helpers import get_update_form, petition_detail_meta
4040

4141

@@ -1741,4 +1741,8 @@ def get_form_kwargs(self):
17411741
kwargs.update({
17421742
'request': self.request
17431743
})
1744-
return kwargs
1744+
return kwargs
1745+
1746+
def form_valid(self, form):
1747+
form.send_success_email()
1748+
return super().form_valid(form)

pytition/pytition/settings/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,5 @@
264264
#:| If set to True, regular users won't be able to create new organizations.
265265
#:| Only superusers will be allowed to
266266
RESTRICT_ORG_CREATION = False
267+
268+
DEFAULT_NOREPLY_MAIL = "[email protected]"

0 commit comments

Comments
 (0)