From 256d2450d95a81ff58f021b2c5878c3c885e05c0 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 12 Mar 2020 16:56:17 +0100 Subject: [PATCH] commands: send html unused account alert with link (#40522) --- .../management/commands/clean-unused-accounts.py | 10 +++++----- .../authentic2/unused_account_alert_body.html | 6 ++++++ .../templates/authentic2/unused_account_alert_body.txt | 6 ++++-- tests/settings.py | 2 ++ tests/test_commands.py | 8 ++++++++ 5 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/authentic2/templates/authentic2/unused_account_alert_body.html diff --git a/src/authentic2/management/commands/clean-unused-accounts.py b/src/authentic2/management/commands/clean-unused-accounts.py index dd5d71f9..267a7c47 100644 --- a/src/authentic2/management/commands/clean-unused-accounts.py +++ b/src/authentic2/management/commands/clean-unused-accounts.py @@ -22,14 +22,15 @@ import datetime from django.contrib.auth import get_user_model from django.contrib.humanize.templatetags.humanize import apnumber from django.core.management.base import BaseCommand, CommandError -from django.core.mail import send_mail from django.template.defaultfilters import pluralize +from django.utils.six.moves.urllib import parse as urlparse from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string from authentic2 import app_settings from authentic2.models import DeletedUser +from authentic2.utils import send_templated_mail from django.conf import settings @@ -134,7 +135,8 @@ class Command(BaseCommand): def send_alert(self, user, threshold, clean_threshold): ctx = { 'user': user, - 'clean_threshold': clean_threshold + 'clean_threshold': clean_threshold, + 'login_url': urlparse.urljoin(settings.SITE_BASE_URL, settings.LOGIN_URL) } self.add_threshold_data(threshold, ctx) self.send_mail('authentic2/unused_account_alert', user, ctx) @@ -142,12 +144,10 @@ class Command(BaseCommand): def send_mail(self, prefix, user, ctx): if not user.email: logger.debug('%s has no email, no mail sent', user) - subject = render_to_string(prefix + '_subject.txt', ctx).strip() - body = render_to_string(prefix + '_body.txt', ctx) if not self.fake: try: logger.debug('sending mail to %s', user.email) - send_mail(subject, body, self.from_email, [user.email]) + send_templated_mail(user.email, prefix, ctx) except Exception: logger.exception('email sending failure') diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.html b/src/authentic2/templates/authentic2/unused_account_alert_body.html new file mode 100644 index 00000000..c275bace --- /dev/null +++ b/src/authentic2/templates/authentic2/unused_account_alert_body.html @@ -0,0 +1,6 @@ +{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user.get_full_name }}, + +You have not logged in for {{ threshold }} {{ threshold_unit }}. +In order to keep your account, you must log in before {{ clean_threshold }} days. +Otherwise, it will be deleted after this time. +{% endblocktrans %}{% endautoescape %} diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt index e992b358..5ba70421 100644 --- a/src/authentic2/templates/authentic2/unused_account_alert_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_alert_body.txt @@ -1,4 +1,6 @@ {% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, -You have not logged in for {{ threshold }} {{ threshold_unit }}. In {{ clean_threshold }} days your account -will be deleted.{% endblocktrans %}{% endautoescape %} +You have not logged in for {{ threshold }} {{ threshold_unit }}. +In order to keep your account, you must log in before {{ clean_threshold }} days. +Otherwise, it will be deleted after this time. +{% endblocktrans %}{% endautoescape %} diff --git a/tests/settings.py b/tests/settings.py index 3161bc75..d8b3376c 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -44,3 +44,5 @@ ALLOWED_HOSTS = ALLOWED_HOSTS + ['example.net', 'cache1.example.com', 'cache2.ex A2_AUTH_KERBEROS_ENABLED = False A2_VALIDATE_EMAIL_DOMAIN = False + +SITE_BASE_URL = 'http://localhost' diff --git a/tests/test_commands.py b/tests/test_commands.py index bdbe21af..b2d5794a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -96,6 +96,14 @@ def test_clean_unused_account_settings(simple_user, mailoutbox, settings): assert DeletedUser.objects.filter(user=simple_user).exists() +def test_clean_unused_account_login_url(simple_user, mailoutbox): + simple_user.last_login = now() - datetime.timedelta(days=1) + simple_user.save() + management.call_command('clean-unused-accounts', 2, '--alert-thresholds', '1') + mail = mailoutbox[0] + assert 'href=http://localhost/login' in mail.message().as_string() + + def test_cleanupauthentic(db): management.call_command('cleanupauthentic') -- 2.20.1