Projet

Général

Profil

0001-commands-human-duration-in-unused-account-email-4052.patch

Valentin Deniaud, 18 mars 2020 10:20

Télécharger (4,9 ko)

Voir les différences:

Subject: [PATCH] commands: human duration in unused account email (#40521)

 .../commands/clean-unused-accounts.py         | 10 ++++----
 .../authentic2/unused_account_alert_body.txt  |  2 +-
 .../authentic2/unused_account_delete_body.txt |  2 +-
 tests/test_commands.py                        | 24 +++++++++++++++++++
 4 files changed, 31 insertions(+), 7 deletions(-)
src/authentic2/management/commands/clean-unused-accounts.py
22 22

  
23 23
from datetime import timedelta
24 24
from django.contrib.auth import get_user_model
25
from django.contrib.humanize.templatetags.humanize import naturaltime
25 26
from django.core.management.base import BaseCommand, CommandError
26 27
from django.utils import timezone
27 28
from django.template.loader import render_to_string
......
89 90
                self.delete_user(user)
90 91

  
91 92
    def send_alert(self, user):
92
        alert_delay = user.ou.clean_unused_accounts_alert
93
        days_to_deletion = user.ou.clean_unused_accounts_deletion - alert_delay
93
        days_to_deletion = user.ou.clean_unused_accounts_deletion - user.ou.clean_unused_accounts_alert
94 94
        ctx = {
95 95
            'user': user,
96
            'threshold': alert_delay,
97
            'clean_threshold': days_to_deletion,
96
            'days_to_deletion': days_to_deletion,
97
            'last_login_date': naturaltime(user.last_login)
98 98
        }
99 99
        try:
100 100
            self.send_mail('authentic2/unused_account_alert', user, ctx)
......
115 115
    def delete_user(self, user):
116 116
        ctx = {
117 117
            'user': user,
118
            'threshold': user.ou.clean_unused_accounts_deletion
118
            'last_login_date': naturaltime(user.last_login)
119 119
        }
120 120
        try:
121 121
            self.send_mail('authentic2/unused_account_delete', user, ctx)
src/authentic2/templates/authentic2/unused_account_alert_body.txt
1 1
{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }},
2 2

  
3
You have not logged since {{ threshold }} days. In {{ clean_threshold }} days your account
3
Your last logging was {{ last_login_date }}. In {{ days_to_deletion }} days your account
4 4
will be deleted.{% endblocktrans %}{% endautoescape %}
src/authentic2/templates/authentic2/unused_account_delete_body.txt
1 1
{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }},
2 2

  
3
You have not logged since {{ threshold }} days so your account has been deleted.{% endblocktrans %}{% endautoescape %}
3
Since your last logging was {{ last_login_date }}, your account has been deleted.{% endblocktrans %}{% endautoescape %}
tests/test_commands.py
18 18
import importlib
19 19
import json
20 20

  
21
import pytest
22

  
21 23
from django.core import management
22 24
from django.utils import six
23 25
from django.utils.timezone import now
......
100 102
    assert len(mailoutbox) == 1
101 103

  
102 104

  
105
@pytest.mark.parametrize("deletion_delay,formatted",
106
                         [(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')])
107
def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, deletion_delay, formatted):
108
    simple_user.ou.clean_unused_accounts_alert = deletion_delay - 1
109
    simple_user.ou.clean_unused_accounts_deletion = deletion_delay
110
    simple_user.ou.save()
111
    simple_user.last_login = now() - datetime.timedelta(days=deletion_delay + 1)
112
    simple_user.save()
113

  
114
    # alert email
115
    management.call_command('clean-unused-accounts')
116
    mail = mailoutbox[0]
117
    assert formatted in mail.body
118

  
119
    # deletion email
120
    simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=2)
121
    simple_user.save()
122
    management.call_command('clean-unused-accounts')
123
    mail = mailoutbox[1]
124
    assert formatted in mail.body
125

  
126

  
103 127
def test_cleanupauthentic(db):
104 128
    management.call_command('cleanupauthentic')
105 129

  
106
-