Projet

Général

Profil

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

Valentin Deniaud, 02 avril 2020 12:09

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
......
128 130
    assert len(mailoutbox) == 1
129 131

  
130 132

  
133
@pytest.mark.parametrize("deletion_delay,formatted",
134
                         [(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')])
135
def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, deletion_delay, formatted):
136
    simple_user.ou.clean_unused_accounts_alert = deletion_delay - 1
137
    simple_user.ou.clean_unused_accounts_deletion = deletion_delay
138
    simple_user.ou.save()
139
    simple_user.last_login = now() - datetime.timedelta(days=deletion_delay + 1)
140
    simple_user.save()
141

  
142
    # alert email
143
    management.call_command('clean-unused-accounts')
144
    mail = mailoutbox[0]
145
    assert formatted in mail.body
146

  
147
    # deletion email
148
    simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=2)
149
    simple_user.save()
150
    management.call_command('clean-unused-accounts')
151
    mail = mailoutbox[1]
152
    assert formatted in mail.body
153

  
154

  
131 155
def test_cleanupauthentic(db):
132 156
    management.call_command('cleanupauthentic')
133 157

  
134
-