Skip to content

Commit

Permalink
update welcome email command to allow per stage templates
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Oct 8, 2024
1 parent 0ac4c82 commit 1252c73
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
23 changes: 18 additions & 5 deletions crowdsourcer/management/commands/send_welcome_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def add_arguments(self, parser):
parser.add_argument("--send_emails", action="store_true", help="Send emails")

parser.add_argument(
"--stage", action="store", help="Only send emails to people in this stage"
"--stage",
required=True,
action="store",
help="Use template for this stage and only send emails to people in this stage",
)

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

return None

def get_templates(self, config, user, stage="First Mark"):
if config.get(stage):
config = config[stage]

template = config["new_user_template"]
if user.password != "":
template = config["previous_user_template"]

return (template, config["subject_template"])

def handle(self, *args, **kwargs):
if not kwargs["send_emails"]:
self.stdout.write(
Expand Down Expand Up @@ -94,12 +107,12 @@ def handle(self, *args, **kwargs):
if user.email:
self.stdout.write(f"Sending email for to this email: {user.email}")
if kwargs["send_emails"]:
template = config["new_user_template"]
template, subject_template = self.get_templates(
config, user, kwargs["stage"]
)
if user.password == "":
user.set_password(get_random_string(length=20))
user.save()
else:
template = config["previous_user_template"]

form = PasswordResetForm({"email": user.email})
assert form.is_valid()
Expand All @@ -111,7 +124,7 @@ def handle(self, *args, **kwargs):
domain_override=config["server_name"],
use_https=True,
from_email=config["from_email"],
subject_template_name=config["subject_template"],
subject_template_name=subject_template,
email_template_name=template,
)
marker.send_welcome_email = False
Expand Down
64 changes: 63 additions & 1 deletion crowdsourcer/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ class SendWelcomeEmails(BaseCommandTestCase):
def test_required_args(self):
self.assertEquals(len(mail.outbox), 0)
with self.assertRaisesRegex(
CommandError, r"following arguments are required: --session"
CommandError, r"following arguments are required: --stage, --session"
):
self.call_command(
"send_welcome_emails",
Expand All @@ -810,19 +810,22 @@ def test_basic_run(self):
self.assertEquals(len(mail.outbox), 0)
self.call_command(
"send_welcome_emails",
stage="First Mark",
session="Default",
)
self.assertEquals(len(mail.outbox), 0)

self.call_command(
"send_welcome_emails",
stage="First Mark",
session="Default",
send_emails=True,
)
self.assertEquals(len(mail.outbox), 2)

self.call_command(
"send_welcome_emails",
stage="First Mark",
session="Default",
send_emails=True,
)
Expand All @@ -837,6 +840,7 @@ def test_only_sends_if_flag_set(self):

self.call_command(
"send_welcome_emails",
stage="First Mark",
session="Default",
send_emails=True,
)
Expand All @@ -848,6 +852,7 @@ def test_only_sends_if_flag_set(self):
def test_email_comtent(self):
self.call_command(
"send_welcome_emails",
stage="First Mark",
session="Default",
send_emails=True,
)
Expand Down Expand Up @@ -883,13 +888,15 @@ def test_limit_session(self):
self.assertEquals(len(mail.outbox), 0)
self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Second Session",
)
self.assertEquals(len(mail.outbox), 0)

self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Default",
)
Expand All @@ -903,6 +910,7 @@ def test_config_loading(self):
self.assertEquals(len(mail.outbox), 0)
self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Second Session",
)
Expand All @@ -913,18 +921,72 @@ def test_config_loading(self):
mail.outbox = []
self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Default",
)
self.assertEquals(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEquals(email.from_email, "Default From <[email protected]>")

@override_settings(
WELCOME_EMAIL={
"Default": {
"server_name": "example.org",
"from_email": "Default From <[email protected]>",
"subject_template": "registration/initial_password_email_subject.txt",
"new_user_template": "registration/initial_password_email.html",
"previous_user_template": "registration/repeat_password_email.html",
"Right of Reply": {
"subject_template": "registration/council_password_email_subject.txt",
"new_user_template": "registration/council_password_email.html",
"previous_user_template": "registration/council_repeat_password_email.html",
},
},
}
)
def test_template_loading(self):
marker = Marker.objects.get(user__email="[email protected]")
rt = ResponseType.objects.get(type="Right of Reply")
marker.response_type = rt
marker.save()

self.assertEquals(len(mail.outbox), 0)
self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Default",
)
self.assertEquals(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEquals(
email.subject,
"Registration link for CEUK Council Climate Scorecards Scoring System",
)
self.assertRegex(email.body, r"Thanks for volunteering")

mail.outbox = []
self.call_command(
"send_welcome_emails",
stage="Right of Reply",
send_emails=True,
session="Default",
)
self.assertEquals(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEquals(
email.subject,
"Registration link for CEUK Council Climate Scorecards Scoring System",
)
self.assertRegex(email.body, r"council’s contact to receive")

@override_settings(WELCOME_EMAIL={})
def test_error_if_no_config(self):
self.assertEquals(len(mail.outbox), 0)
_, err = self.call_command(
"send_welcome_emails",
stage="First Mark",
send_emails=True,
session="Default",
)
Expand Down
21 changes: 21 additions & 0 deletions templates/registration/council_repeat_password_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% autoescape off %}
Hi,

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.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
Your username is {{ user.get_username }}

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.

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.

Kind Regards,

The CEUK team.

{% endautoescape %}

0 comments on commit 1252c73

Please sign in to comment.