From 3f19eb298eb3516270e9153d38aea7877bd58571 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) --- .../management/commands/clean-unused-accounts.py | 15 ++++++++------- .../authentic2/unused_account_alert_body.txt | 2 +- .../authentic2/unused_account_delete_body.txt | 2 +- tests/test_commands.py | 15 ++++++++++++++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/authentic2/management/commands/clean-unused-accounts.py b/src/authentic2/management/commands/clean-unused-accounts.py index a0bcdf51..8227e80c 100644 --- a/src/authentic2/management/commands/clean-unused-accounts.py +++ b/src/authentic2/management/commands/clean-unused-accounts.py @@ -20,6 +20,7 @@ import logging import datetime from django.contrib.auth import get_user_model +from django.contrib.humanize.templatetags.humanize import naturaltime from django.core.management.base import BaseCommand, CommandError from django.core.mail import send_mail from django.utils.timezone import now @@ -118,18 +119,18 @@ class Command(BaseCommand): b = n - datetime.timedelta(days=threshold - options['period']) for user in users.filter(last_login__lt=b, last_login__gte=a): logger.info('%s last login %d days ago, sending alert', user, threshold) - self.send_alert(user, threshold, clean_threshold - threshold) + self.send_alert(user, clean_threshold - threshold) threshold = n - datetime.timedelta(days=clean_threshold) for user in users.filter(last_login__lt=threshold): d = n - user.last_login logger.info('%s last login %d days ago, deleting user', user, d.days) - self.delete_user(user, clean_threshold) + self.delete_user(user) - def send_alert(self, user, threshold, clean_threshold): + def send_alert(self, user, clean_threshold): ctx = { 'user': user, - 'threshold': threshold, - 'clean_threshold': clean_threshold + 'clean_threshold': clean_threshold, + 'last_login_date': naturaltime(user.last_login) } self.send_mail('authentic2/unused_account_alert', user, ctx) @@ -145,10 +146,10 @@ class Command(BaseCommand): except Exception: logger.exception('email sending failure') - def delete_user(self, user, threshold): + def delete_user(self, user): ctx = { 'user': user, - 'threshold': threshold + 'last_login_date': naturaltime(user.last_login) } self.send_mail('authentic2/unused_account_delete', user, ctx) if not self.fake: diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt index b65fb6a4..70197463 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 +Your last logging was {{ last_login_date }}. 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..e327cb5e 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 %} +Since your last logging was {{ last_login_date }}, your account has been deleted.{% endblocktrans %}{% endautoescape %} diff --git a/tests/test_commands.py b/tests/test_commands.py index 7cb0f763..d8ae1378 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 @@ -58,7 +60,18 @@ def test_clean_unused_account_alert_email(simple_user, mailoutbox, settings): simple_user.last_login = now() - datetime.timedelta(days=180) simple_user.save() management.call_command('clean-unused-accounts', 730, '--alert-thresholds', '180') - assert len(mailoutbox) == 1 + mail = mailoutbox[0] + assert u'6\xa0months' in mail.body + + +@pytest.mark.parametrize("threshold,formatted", + [(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')]) +def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, threshold, formatted): + simple_user.last_login = now() - datetime.timedelta(days=threshold + 1) + simple_user.save() + management.call_command('clean-unused-accounts', threshold) + mail = mailoutbox[0] + assert formatted in mail.body def test_cleanupauthentic(db): -- 2.20.1