Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds opt-out option for reminder emails #1695

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/admin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MaCanteenUserAdmin(UserAdmin):
_("Emails automatiques"),
{
"fields": (
"opt_out_reminder_emails",
"email_no_canteen_first_reminder",
"email_no_canteen_second_reminder",
),
Expand Down
18 changes: 18 additions & 0 deletions data/migrations/0084_user_opt_out_reminder_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-26 16:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('data', '0083_diagnostic_diagnostic_type'),
]

operations = [
migrations.AddField(
model_name='user',
name='opt_out_reminder_emails',
field=models.BooleanField(default='False', verbose_name='Désactiver les emails de rappel'),
),
]
2 changes: 2 additions & 0 deletions data/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Sources(models.TextChoices):
email = models.EmailField(_("email address"), unique=True)
email_confirmed = models.BooleanField(default="False", verbose_name="adresse email confirmée")

opt_out_reminder_emails = models.BooleanField(default="False", verbose_name="Désactiver les emails de rappel")

law_awareness = ChoiceArrayField(
base_field=models.CharField(max_length=255, choices=LawAwareness.choices),
blank=True,
Expand Down
4 changes: 3 additions & 1 deletion macantine/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def no_canteen_first_reminder():
canteens=None,
date_joined__lte=threshold,
email_no_canteen_first_reminder__isnull=True,
opt_out_reminder_emails=False,
).all()
if not users:
logger.info("no_canteen_first_reminder: No users to notify.")
Expand Down Expand Up @@ -69,6 +70,7 @@ def no_canteen_second_reminder():
date_joined__lte=threshold,
email_no_canteen_first_reminder__lte=first_reminder_threshold,
email_no_canteen_second_reminder__isnull=True,
opt_out_reminder_emails=False,
).all()
if not users:
logger.info("no_canteen_second_reminder: No users to notify.")
Expand Down Expand Up @@ -109,7 +111,7 @@ def no_diagnostic_first_reminder():
logger.info(f"no_diagnostic_first_reminder: {len(canteens)} canteens to notify.")

for canteen in canteens:
for manager in canteen.managers.all():
for manager in canteen.managers.filter(opt_out_reminder_emails=False):

try:
parameters = {"PRENOM": manager.first_name, "NOM_CANTINE": canteen.name}
Expand Down
93 changes: 93 additions & 0 deletions macantine/tests/test_automatic_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,96 @@ def test_no_diagnostic_first_reminder(self, _):
# DB objects are updated
canteen_no_diagnostics.refresh_from_db()
self.assertIsNotNone(canteen_no_diagnostics.email_no_diagnostic_first_reminder)

@mock.patch("macantine.tasks._send_sib_template")
@override_settings(TEMPLATE_ID_NO_CANTEEN_FIRST=1)
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"})
def test_email_opt_out_first_reminder(self, _):
"""
The email should not be sent to users that have opted-out of the
reminder emails.
"""
today = timezone.now()

jean = UserFactory.create(
date_joined=(today - timedelta(weeks=1)),
email_no_canteen_first_reminder=None,
first_name="Jean",
last_name="Sérien",
email="[email protected]",
opt_out_reminder_emails=True,
)
tasks.no_canteen_first_reminder()

tasks._send_sib_template.assert_not_called()

self.assertIsNone(jean.email_no_canteen_first_reminder)

@mock.patch("macantine.tasks._send_sib_template")
@override_settings(TEMPLATE_ID_NO_CANTEEN_SECOND=2)
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"})
def test_opt_out_second_reminder(self, _):
"""
The email should not be sent to users that have opted-out of the
reminder emails.
"""
today = timezone.now()

marie = UserFactory.create(
date_joined=(today - timedelta(weeks=2)),
email_no_canteen_first_reminder=(today - timedelta(weeks=1)),
email_no_canteen_second_reminder=None,
first_name="Marie",
last_name="Olait",
email="[email protected]",
opt_out_reminder_emails=True,
)
tasks.no_canteen_second_reminder()

tasks._send_sib_template.assert_not_called()

marie.refresh_from_db()

self.assertIsNone(marie.email_no_canteen_second_reminder)

@mock.patch("macantine.tasks._send_sib_template")
@override_settings(TEMPLATE_ID_NO_DIAGNOSTIC_FIRST=1)
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"})
def test_opt_out_no_diagnostic_first_reminder(self, _):
"""
The email should not be sent to users that have opted-out of the
reminder emails.
"""
today = timezone.now()

# Only Anna should receive the email since Jean has opted out
jean = UserFactory.create(
first_name="Jean",
last_name="Sérien",
email="[email protected]",
opt_out_reminder_emails=True,
)
anna = UserFactory.create(
first_name="Anna",
last_name="Logue",
email="[email protected]",
)
canteen_no_diagnostics = CanteenFactory.create(
managers=[
anna,
jean,
]
)
Canteen.objects.filter(pk=canteen_no_diagnostics.id).update(creation_date=(today - timedelta(weeks=2)))

tasks.no_diagnostic_first_reminder()

# Email is only sent once to Anna
tasks._send_sib_template.assert_called
self.assertEqual(tasks._send_sib_template.call_count, 1)
call_args_list = tasks._send_sib_template.call_args_list
self.assertEqual(call_args_list[0][0][2], "[email protected]")

# DB objects are updated
canteen_no_diagnostics.refresh_from_db()
self.assertIsNotNone(canteen_no_diagnostics.email_no_diagnostic_first_reminder)