0001-commands-human-duration-in-unused-account-email-4052.patch
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 |
... | ... | |
118 | 119 |
b = n - datetime.timedelta(days=threshold - options['period']) |
119 | 120 |
for user in users.filter(last_login__lt=b, last_login__gte=a): |
120 | 121 |
logger.info('%s last login %d days ago, sending alert', user, threshold) |
121 |
self.send_alert(user, threshold, clean_threshold - threshold)
|
|
122 |
self.send_alert(user, clean_threshold - threshold) |
|
122 | 123 |
threshold = n - datetime.timedelta(days=clean_threshold) |
123 | 124 |
for user in users.filter(last_login__lt=threshold): |
124 | 125 |
d = n - user.last_login |
125 | 126 |
logger.info('%s last login %d days ago, deleting user', user, d.days) |
126 |
self.delete_user(user, clean_threshold)
|
|
127 |
self.delete_user(user) |
|
127 | 128 | |
128 |
def send_alert(self, user, threshold, clean_threshold):
|
|
129 |
def send_alert(self, user, clean_threshold): |
|
129 | 130 |
ctx = { |
130 | 131 |
'user': user, |
131 |
'threshold': threshold,
|
|
132 |
'clean_threshold': clean_threshold
|
|
132 |
'clean_threshold': clean_threshold,
|
|
133 |
'last_login_date': naturaltime(user.last_login)
|
|
133 | 134 |
} |
134 | 135 |
self.send_mail('authentic2/unused_account_alert', user, ctx) |
135 | 136 | |
... | ... | |
145 | 146 |
except Exception: |
146 | 147 |
logger.exception('email sending failure') |
147 | 148 | |
148 |
def delete_user(self, user, threshold):
|
|
149 |
def delete_user(self, user): |
|
149 | 150 |
ctx = { |
150 | 151 |
'user': user, |
151 |
'threshold': threshold
|
|
152 |
'last_login_date': naturaltime(user.last_login)
|
|
152 | 153 |
} |
153 | 154 |
self.send_mail('authentic2/unused_account_delete', user, ctx) |
154 | 155 |
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 |
... | ... | |
58 | 60 |
simple_user.last_login = now() - datetime.timedelta(days=180) |
59 | 61 |
simple_user.save() |
60 | 62 |
management.call_command('clean-unused-accounts', 730, '--alert-thresholds', '180') |
61 |
assert len(mailoutbox) == 1 |
|
63 |
mail = mailoutbox[0] |
|
64 |
assert u'6\xa0months' in mail.body |
|
65 | ||
66 | ||
67 |
@pytest.mark.parametrize("threshold,formatted", |
|
68 |
[(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')]) |
|
69 |
def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, threshold, formatted): |
|
70 |
simple_user.last_login = now() - datetime.timedelta(days=threshold + 1) |
|
71 |
simple_user.save() |
|
72 |
management.call_command('clean-unused-accounts', threshold) |
|
73 |
mail = mailoutbox[0] |
|
74 |
assert formatted in mail.body |
|
62 | 75 | |
63 | 76 | |
64 | 77 |
def test_cleanupauthentic(db): |
65 |
- |