Projet

Général

Profil

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

Valentin Deniaud, 12 mars 2020 12:34

Télécharger (5,06 ko)

Voir les différences:

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

 .../commands/clean-unused-accounts.py         | 21 ++++++++++++++++---
 .../authentic2/unused_account_alert_body.txt  |  2 +-
 .../authentic2/unused_account_delete_body.txt |  2 +-
 tests/test_commands.py                        | 12 +++++++++++
 4 files changed, 32 insertions(+), 5 deletions(-)
src/authentic2/management/commands/clean-unused-accounts.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from __future__ import print_function
17
from __future__ import print_function, division
18 18

  
19 19
import logging
20 20
import datetime
21 21

  
22 22
from django.contrib.auth import get_user_model
23
from django.contrib.humanize.templatetags.humanize import apnumber
23 24
from django.core.management.base import BaseCommand, CommandError
24 25
from django.core.mail import send_mail
26
from django.template.defaultfilters import pluralize
25 27
from django.utils.timezone import now
28
from django.utils.translation import ugettext_lazy as _
26 29
from django.template.loader import render_to_string
27 30

  
28 31
from authentic2.models import DeletedUser
......
129 132
    def send_alert(self, user, threshold, clean_threshold):
130 133
        ctx = {
131 134
            'user': user,
132
            'threshold': threshold,
133 135
            'clean_threshold': clean_threshold
134 136
        }
137
        self.add_threshold_data(threshold, ctx)
135 138
        self.send_mail('authentic2/unused_account_alert', user, ctx)
136 139

  
137 140
    def send_mail(self, prefix, user, ctx):
......
149 152
    def delete_user(self, user, threshold):
150 153
        ctx = {
151 154
            'user': user,
152
            'threshold': threshold
153 155
        }
156
        self.add_threshold_data(threshold, ctx)
154 157
        self.send_mail('authentic2/unused_account_delete', user, ctx)
155 158
        if not self.fake:
156 159
            DeletedUser.objects.delete_user(user)
160

  
161
    @staticmethod
162
    def add_threshold_data(threshold, ctx):
163
        if threshold // 365 > 0:
164
            threshold = round(threshold / 365)
165
            threshold_unit = _('year')
166
        else:
167
            threshold = round(threshold / 30)
168
            threshold_unit = _('month')
169
        threshold_unit += pluralize(threshold)
170
        ctx['threshold'] = apnumber(threshold)
171
        ctx['threshold_unit'] = threshold_unit
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
You have not logged in for {{ threshold }} {{ threshold_unit }}. In {{ clean_threshold }} 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
You have not logged in for {{ threshold }} {{ threshold_unit }} so 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
......
54 56
    assert DeletedUser.objects.get(user=simple_user)
55 57

  
56 58

  
59
@pytest.mark.parametrize("threshold,formatted",
60
                         [('730', 'two years'), (500, 'one year'), (65, 'two months')])
61
def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, threshold, formatted):
62
    simple_user.last_login = now() - datetime.timedelta(days=1000)
63
    simple_user.save()
64
    management.call_command('clean-unused-accounts', threshold)
65
    mail = mailoutbox[0]
66
    assert formatted in mail.body
67

  
68

  
57 69
def test_cleanupauthentic(db):
58 70
    management.call_command('cleanupauthentic')
59 71

  
60
-