Projet

Général

Profil

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

Valentin Deniaud, 02 avril 2020 14:18

Télécharger (5,36 ko)

Voir les différences:

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(-)
src/authentic2/management/commands/clean-unused-accounts.py
89 89
                self.delete_user(user)
90 90

  
91 91
    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
92
        days_to_deletion = user.ou.clean_unused_accounts_deletion - user.ou.clean_unused_accounts_alert
94 93
        ctx = {
95 94
            'user': user,
96
            'threshold': alert_delay,
97
            'clean_threshold': days_to_deletion,
95
            'days_to_deletion': days_to_deletion,
98 96
        }
99 97
        try:
100 98
            self.send_mail('authentic2/unused_account_alert', user, ctx)
......
113 111
            send_templated_mail(user.email, prefix, ctx)
114 112

  
115 113
    def delete_user(self, user):
116
        ctx = {
117
            'user': user,
118
            'threshold': user.ou.clean_unused_accounts_deletion
119
        }
114
        ctx = {'user': user}
120 115
        try:
121 116
            self.send_mail('authentic2/unused_account_delete', user, ctx)
122 117
        except smtplib.SMTPException as e:
src/authentic2/settings.py
126 126
    'django.contrib.sessions',
127 127
    'django.contrib.messages',
128 128
    'django.contrib.admin',
129
    'django.contrib.humanize',
129 130
    'django_select2',
130 131
    'django_tables2',
131 132
    'authentic2.nonce',
src/authentic2/templates/authentic2/unused_account_alert_body.txt
1
{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }},
1
{% load i18n humanize %}
2
{% autoescape off %}
3
{% blocktrans with last_login_date=user.last_login|naturaltime %}
4
Hi {{ user }},
2 5

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

  
3
You have not logged since {{ threshold }} days so your account has been deleted.{% endblocktrans %}{% endautoescape %}
6
Since your last logging was {{ last_login_date }}, your account has been deleted.
7
{% endblocktrans %}
8
{% 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
-