Projet

Général

Profil

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

Valentin Deniaud, 16 mars 2020 16:26

Télécharger (5,07 ko)

Voir les différences:

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

 .../management/commands/clean-unused-accounts.py  | 15 ++++++++-------
 .../authentic2/unused_account_alert_body.txt      |  2 +-
 .../authentic2/unused_account_delete_body.txt     |  2 +-
 tests/test_commands.py                            | 12 ++++++++++++
 4 files changed, 22 insertions(+), 9 deletions(-)
src/authentic2/management/commands/clean-unused-accounts.py
20 20
import datetime
21 21

  
22 22
from django.contrib.auth import get_user_model
23
from django.contrib.humanize.templatetags.humanize import naturaltime
23 24
from django.core.management.base import BaseCommand, CommandError
24 25
from django.core.mail import send_mail
25 26
from django.utils.timezone import now
......
119 120
                b = n - datetime.timedelta(days=threshold - options['period'])
120 121
                for user in users.filter(last_login__lt=b, last_login__gte=a):
121 122
                    logger.info('%s last login %d days ago, sending alert', user, threshold)
122
                    self.send_alert(user, threshold, clean_threshold - threshold)
123
                    self.send_alert(user, clean_threshold - threshold)
123 124
        threshold = n - datetime.timedelta(days=clean_threshold)
124 125
        for user in users.filter(last_login__lt=threshold):
125 126
            d = n - user.last_login
126 127
            logger.info('%s last login %d days ago, deleting user', user, d.days)
127
            self.delete_user(user, clean_threshold)
128
            self.delete_user(user)
128 129

  
129
    def send_alert(self, user, threshold, clean_threshold):
130
    def send_alert(self, user, clean_threshold):
130 131
        ctx = {
131 132
            'user': user,
132
            'threshold': threshold,
133
            'clean_threshold': clean_threshold
133
            'clean_threshold': clean_threshold,
134
            'last_login_date': naturaltime(user.last_login)
134 135
        }
135 136
        self.send_mail('authentic2/unused_account_alert', user, ctx)
136 137

  
......
146 147
            except Exception:
147 148
                logger.exception('email sending failure')
148 149

  
149
    def delete_user(self, user, threshold):
150
    def delete_user(self, user):
150 151
        ctx = {
151 152
            'user': user,
152
            'threshold': threshold
153
            'last_login_date': naturaltime(user.last_login)
153 154
        }
154 155
        self.send_mail('authentic2/unused_account_delete', user, ctx)
155 156
        if not self.fake:
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 {{ 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
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
......
54 56
    assert DeletedUser.objects.get(user=simple_user)
55 57

  
56 58

  
59
@pytest.mark.parametrize("threshold,formatted",
60
                         [(730, '2\xa0years'), (500, '1\xa0year'), (65, '2\xa0months')])
61
def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, threshold, formatted):
62
    simple_user.last_login = now() - datetime.timedelta(days=threshold + 1)
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
-