From 46e2aeb5cec0e87da09455a20badf04eff20f8c6 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 12 Mar 2020 12:22:57 +0100 Subject: [PATCH] commands: human duration in unused account email (#40521) --- .../commands/clean-unused-accounts.py | 21 ++++++++++++++++--- .../authentic2/unused_account_alert_body.txt | 2 +- .../authentic2/unused_account_delete_body.txt | 2 +- tests/test_commands.py | 12 +++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/authentic2/management/commands/clean-unused-accounts.py b/src/authentic2/management/commands/clean-unused-accounts.py index 720e58ec..50969a4a 100644 --- a/src/authentic2/management/commands/clean-unused-accounts.py +++ b/src/authentic2/management/commands/clean-unused-accounts.py @@ -14,15 +14,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import print_function +from __future__ import print_function, division import logging 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.timezone import now +from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string from authentic2.models import DeletedUser @@ -129,9 +132,9 @@ class Command(BaseCommand): def send_alert(self, user, threshold, clean_threshold): ctx = { 'user': user, - 'threshold': threshold, 'clean_threshold': clean_threshold } + self.add_threshold_data(threshold, ctx) self.send_mail('authentic2/unused_account_alert', user, ctx) def send_mail(self, prefix, user, ctx): @@ -149,8 +152,20 @@ class Command(BaseCommand): def delete_user(self, user, threshold): ctx = { 'user': user, - 'threshold': threshold } + self.add_threshold_data(threshold, ctx) self.send_mail('authentic2/unused_account_delete', user, ctx) if not self.fake: DeletedUser.objects.delete_user(user) + + @staticmethod + def add_threshold_data(threshold, ctx): + if threshold // 365 > 0: + threshold = round(threshold / 365) + threshold_unit = _('year') + else: + threshold = round(threshold / 30) + threshold_unit = _('month') + threshold_unit += pluralize(threshold) + ctx['threshold'] = apnumber(threshold) + ctx['threshold_unit'] = threshold_unit diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt index b65fb6a4..e992b358 100644 --- a/src/authentic2/templates/authentic2/unused_account_alert_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_alert_body.txt @@ -1,4 +1,4 @@ {% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, -You have not logged since {{ threshold }} days. In {{ clean_threshold }} days your account +You have not logged in for {{ threshold }} {{ threshold_unit }}. In {{ clean_threshold }} days your account will be deleted.{% endblocktrans %}{% endautoescape %} diff --git a/src/authentic2/templates/authentic2/unused_account_delete_body.txt b/src/authentic2/templates/authentic2/unused_account_delete_body.txt index abb74c84..dc0baa8e 100644 --- a/src/authentic2/templates/authentic2/unused_account_delete_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_delete_body.txt @@ -1,3 +1,3 @@ {% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, -You have not logged since {{ threshold }} days so your account has been deleted.{% endblocktrans %}{% endautoescape %} +You have not logged in for {{ threshold }} {{ threshold_unit }} so your account has been deleted.{% endblocktrans %}{% endautoescape %} diff --git a/tests/test_commands.py b/tests/test_commands.py index 1f9d290d..272a1441 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -18,6 +18,8 @@ import datetime import importlib import json +import pytest + from django.core import management from django.utils import six from django.utils.timezone import now @@ -54,6 +56,16 @@ def test_clean_unused_account(simple_user): assert DeletedUser.objects.get(user=simple_user) +@pytest.mark.parametrize("threshold,formatted", + [('730', 'two years'), (500, 'one year'), (65, 'two months')]) +def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, threshold, formatted): + simple_user.last_login = now() - datetime.timedelta(days=1000) + simple_user.save() + management.call_command('clean-unused-accounts', threshold) + mail = mailoutbox[0] + assert formatted in mail.body + + def test_cleanupauthentic(db): management.call_command('cleanupauthentic') -- 2.20.1