Skip to content

Commit 1252c73

Browse files
committed
update welcome email command to allow per stage templates
1 parent 0ac4c82 commit 1252c73

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

crowdsourcer/management/commands/send_welcome_emails.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def add_arguments(self, parser):
2222
parser.add_argument("--send_emails", action="store_true", help="Send emails")
2323

2424
parser.add_argument(
25-
"--stage", action="store", help="Only send emails to people in this stage"
25+
"--stage",
26+
required=True,
27+
action="store",
28+
help="Use template for this stage and only send emails to people in this stage",
2629
)
2730

2831
parser.add_argument(
@@ -42,6 +45,16 @@ def get_config(self, session):
4245

4346
return None
4447

48+
def get_templates(self, config, user, stage="First Mark"):
49+
if config.get(stage):
50+
config = config[stage]
51+
52+
template = config["new_user_template"]
53+
if user.password != "":
54+
template = config["previous_user_template"]
55+
56+
return (template, config["subject_template"])
57+
4558
def handle(self, *args, **kwargs):
4659
if not kwargs["send_emails"]:
4760
self.stdout.write(
@@ -94,12 +107,12 @@ def handle(self, *args, **kwargs):
94107
if user.email:
95108
self.stdout.write(f"Sending email for to this email: {user.email}")
96109
if kwargs["send_emails"]:
97-
template = config["new_user_template"]
110+
template, subject_template = self.get_templates(
111+
config, user, kwargs["stage"]
112+
)
98113
if user.password == "":
99114
user.set_password(get_random_string(length=20))
100115
user.save()
101-
else:
102-
template = config["previous_user_template"]
103116

104117
form = PasswordResetForm({"email": user.email})
105118
assert form.is_valid()
@@ -111,7 +124,7 @@ def handle(self, *args, **kwargs):
111124
domain_override=config["server_name"],
112125
use_https=True,
113126
from_email=config["from_email"],
114-
subject_template_name=config["subject_template"],
127+
subject_template_name=subject_template,
115128
email_template_name=template,
116129
)
117130
marker.send_welcome_email = False

crowdsourcer/tests/test_commands.py

+63-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ class SendWelcomeEmails(BaseCommandTestCase):
799799
def test_required_args(self):
800800
self.assertEquals(len(mail.outbox), 0)
801801
with self.assertRaisesRegex(
802-
CommandError, r"following arguments are required: --session"
802+
CommandError, r"following arguments are required: --stage, --session"
803803
):
804804
self.call_command(
805805
"send_welcome_emails",
@@ -810,19 +810,22 @@ def test_basic_run(self):
810810
self.assertEquals(len(mail.outbox), 0)
811811
self.call_command(
812812
"send_welcome_emails",
813+
stage="First Mark",
813814
session="Default",
814815
)
815816
self.assertEquals(len(mail.outbox), 0)
816817

817818
self.call_command(
818819
"send_welcome_emails",
820+
stage="First Mark",
819821
session="Default",
820822
send_emails=True,
821823
)
822824
self.assertEquals(len(mail.outbox), 2)
823825

824826
self.call_command(
825827
"send_welcome_emails",
828+
stage="First Mark",
826829
session="Default",
827830
send_emails=True,
828831
)
@@ -837,6 +840,7 @@ def test_only_sends_if_flag_set(self):
837840

838841
self.call_command(
839842
"send_welcome_emails",
843+
stage="First Mark",
840844
session="Default",
841845
send_emails=True,
842846
)
@@ -848,6 +852,7 @@ def test_only_sends_if_flag_set(self):
848852
def test_email_comtent(self):
849853
self.call_command(
850854
"send_welcome_emails",
855+
stage="First Mark",
851856
session="Default",
852857
send_emails=True,
853858
)
@@ -883,13 +888,15 @@ def test_limit_session(self):
883888
self.assertEquals(len(mail.outbox), 0)
884889
self.call_command(
885890
"send_welcome_emails",
891+
stage="First Mark",
886892
send_emails=True,
887893
session="Second Session",
888894
)
889895
self.assertEquals(len(mail.outbox), 0)
890896

891897
self.call_command(
892898
"send_welcome_emails",
899+
stage="First Mark",
893900
send_emails=True,
894901
session="Default",
895902
)
@@ -903,6 +910,7 @@ def test_config_loading(self):
903910
self.assertEquals(len(mail.outbox), 0)
904911
self.call_command(
905912
"send_welcome_emails",
913+
stage="First Mark",
906914
send_emails=True,
907915
session="Second Session",
908916
)
@@ -913,18 +921,72 @@ def test_config_loading(self):
913921
mail.outbox = []
914922
self.call_command(
915923
"send_welcome_emails",
924+
stage="First Mark",
916925
send_emails=True,
917926
session="Default",
918927
)
919928
self.assertEquals(len(mail.outbox), 1)
920929
email = mail.outbox[0]
921930
self.assertEquals(email.from_email, "Default From <default@example.org>")
922931

932+
@override_settings(
933+
WELCOME_EMAIL={
934+
"Default": {
935+
"server_name": "example.org",
936+
"from_email": "Default From <default@example.org>",
937+
"subject_template": "registration/initial_password_email_subject.txt",
938+
"new_user_template": "registration/initial_password_email.html",
939+
"previous_user_template": "registration/repeat_password_email.html",
940+
"Right of Reply": {
941+
"subject_template": "registration/council_password_email_subject.txt",
942+
"new_user_template": "registration/council_password_email.html",
943+
"previous_user_template": "registration/council_repeat_password_email.html",
944+
},
945+
},
946+
}
947+
)
948+
def test_template_loading(self):
949+
marker = Marker.objects.get(user__email="new_marker@example.org")
950+
rt = ResponseType.objects.get(type="Right of Reply")
951+
marker.response_type = rt
952+
marker.save()
953+
954+
self.assertEquals(len(mail.outbox), 0)
955+
self.call_command(
956+
"send_welcome_emails",
957+
stage="First Mark",
958+
send_emails=True,
959+
session="Default",
960+
)
961+
self.assertEquals(len(mail.outbox), 1)
962+
email = mail.outbox[0]
963+
self.assertEquals(
964+
email.subject,
965+
"Registration link for CEUK Council Climate Scorecards Scoring System",
966+
)
967+
self.assertRegex(email.body, r"Thanks for volunteering")
968+
969+
mail.outbox = []
970+
self.call_command(
971+
"send_welcome_emails",
972+
stage="Right of Reply",
973+
send_emails=True,
974+
session="Default",
975+
)
976+
self.assertEquals(len(mail.outbox), 1)
977+
email = mail.outbox[0]
978+
self.assertEquals(
979+
email.subject,
980+
"Registration link for CEUK Council Climate Scorecards Scoring System",
981+
)
982+
self.assertRegex(email.body, r"council’s contact to receive")
983+
923984
@override_settings(WELCOME_EMAIL={})
924985
def test_error_if_no_config(self):
925986
self.assertEquals(len(mail.outbox), 0)
926987
_, err = self.call_command(
927988
"send_welcome_emails",
989+
stage="First Mark",
928990
send_emails=True,
929991
session="Default",
930992
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% autoescape off %}
2+
Hi,
3+
4+
You're receiving this email because you are your council’s contact to receive the Right of Reply for Climate Emergency UK’s Council Climate Scorecards. This email allows you to log into the online data collection system to submit the Right of Reply and respond to your council’s first mark in the Council Climate Scorecards.
5+
6+
Please go to the following page and choose a new password:
7+
{% block reset_link %}
8+
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
9+
{% endblock %}
10+
Your username is {{ user.get_username }}
11+
12+
Please Note: Multiple people will be able to login to GRACE as long as they have this username and password. Please only share this with relevant staff within your council. We will be unable to set up multiple accounts for your Council.
13+
14+
You will also receive a second email containing further information, a guide to the Right of Reply process and a recording of the Right of Reply briefing. This email will be sent to every officer contact for your council on our email list.
15+
16+
Kind Regards,
17+
18+
The CEUK team.
19+
20+
{% endautoescape %}
21+

0 commit comments

Comments
 (0)