From 6e0c952371ee2933cc6c4e42ef2264c96e231c41 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 | 11 +++------ src/authentic2/settings.py | 1 + .../authentic2/unused_account_alert_body.txt | 11 ++++++--- .../authentic2/unused_account_delete_body.txt | 9 +++++-- tests/test_commands.py | 24 +++++++++++++++++++ 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/authentic2/management/commands/clean-unused-accounts.py b/src/authentic2/management/commands/clean-unused-accounts.py index 4868c333..df7963da 100644 --- a/src/authentic2/management/commands/clean-unused-accounts.py +++ b/src/authentic2/management/commands/clean-unused-accounts.py @@ -89,12 +89,10 @@ class Command(BaseCommand): self.delete_user(user) def send_alert(self, user): - alert_delay = user.ou.clean_unused_accounts_alert - days_to_deletion = user.ou.clean_unused_accounts_deletion - alert_delay + days_to_deletion = user.ou.clean_unused_accounts_deletion - user.ou.clean_unused_accounts_alert ctx = { 'user': user, - 'threshold': alert_delay, - 'clean_threshold': days_to_deletion, + 'days_to_deletion': days_to_deletion, } try: self.send_mail('authentic2/unused_account_alert', user, ctx) @@ -113,10 +111,7 @@ class Command(BaseCommand): send_templated_mail(user.email, prefix, ctx) def delete_user(self, user): - ctx = { - 'user': user, - 'threshold': user.ou.clean_unused_accounts_deletion - } + ctx = {'user': user} try: self.send_mail('authentic2/unused_account_delete', user, ctx) except smtplib.SMTPException as e: diff --git a/src/authentic2/settings.py b/src/authentic2/settings.py index 44f59a1c..17900401 100644 --- a/src/authentic2/settings.py +++ b/src/authentic2/settings.py @@ -126,6 +126,7 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.admin', + 'django.contrib.humanize', 'django_select2', 'django_tables2', 'authentic2.nonce', diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt index b65fb6a4..c758783a 100644 --- a/src/authentic2/templates/authentic2/unused_account_alert_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_alert_body.txt @@ -1,4 +1,9 @@ -{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, +{% load i18n humanize %} +{% autoescape off %} +{% blocktrans with last_login_date=user.last_login|naturaltime %} +Hi {{ user }}, -You have not logged since {{ threshold }} days. In {{ clean_threshold }} days your account -will be deleted.{% endblocktrans %}{% endautoescape %} +Your last logging was {{ last_login_date }}. In {{ days_to_deletion }} 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..002456ae 100644 --- a/src/authentic2/templates/authentic2/unused_account_delete_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_delete_body.txt @@ -1,3 +1,8 @@ -{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, +{% load i18n humanize %} +{% autoescape off %} +{% blocktrans with last_login_date=user.last_login|naturaltime %} +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 fbcce590..b2806b40 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 @@ -128,6 +130,28 @@ def test_clean_unused_account_always_alert(db, simple_user, mailoutbox, freezer) assert len(mailoutbox) == 1 +@pytest.mark.parametrize("deletion_delay,formatted", + [(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')]) +def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, deletion_delay, formatted): + simple_user.ou.clean_unused_accounts_alert = deletion_delay - 1 + simple_user.ou.clean_unused_accounts_deletion = deletion_delay + simple_user.ou.save() + simple_user.last_login = now() - datetime.timedelta(days=deletion_delay + 1) + simple_user.save() + + # alert email + management.call_command('clean-unused-accounts') + mail = mailoutbox[0] + assert formatted in mail.body + + # deletion email + simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=2) + simple_user.save() + management.call_command('clean-unused-accounts') + mail = mailoutbox[1] + assert formatted in mail.body + + def test_cleanupauthentic(db): management.call_command('cleanupauthentic') -- 2.20.1