0001-commands-send-html-unused-account-alert-with-link-40.patch
src/authentic2/management/commands/clean-unused-accounts.py | ||
---|---|---|
22 | 22 |
from django.contrib.auth import get_user_model |
23 | 23 |
from django.contrib.humanize.templatetags.humanize import apnumber |
24 | 24 |
from django.core.management.base import BaseCommand, CommandError |
25 |
from django.core.mail import send_mail |
|
26 | 25 |
from django.template.defaultfilters import pluralize |
26 |
from django.utils.six.moves.urllib import parse as urlparse |
|
27 | 27 |
from django.utils.timezone import now |
28 | 28 |
from django.utils.translation import ugettext_lazy as _ |
29 | 29 |
from django.template.loader import render_to_string |
30 | 30 | |
31 | 31 |
from authentic2 import app_settings |
32 | 32 |
from authentic2.models import DeletedUser |
33 |
from authentic2.utils import send_templated_mail |
|
33 | 34 | |
34 | 35 |
from django.conf import settings |
35 | 36 | |
... | ... | |
134 | 135 |
def send_alert(self, user, threshold, clean_threshold): |
135 | 136 |
ctx = { |
136 | 137 |
'user': user, |
137 |
'clean_threshold': clean_threshold |
|
138 |
'clean_threshold': clean_threshold, |
|
139 |
'login_url': urlparse.urljoin(settings.SITE_BASE_URL, settings.LOGIN_URL) |
|
138 | 140 |
} |
139 | 141 |
self.add_threshold_data(threshold, ctx) |
140 | 142 |
self.send_mail('authentic2/unused_account_alert', user, ctx) |
... | ... | |
142 | 144 |
def send_mail(self, prefix, user, ctx): |
143 | 145 |
if not user.email: |
144 | 146 |
logger.debug('%s has no email, no mail sent', user) |
145 |
subject = render_to_string(prefix + '_subject.txt', ctx).strip() |
|
146 |
body = render_to_string(prefix + '_body.txt', ctx) |
|
147 | 147 |
if not self.fake: |
148 | 148 |
try: |
149 | 149 |
logger.debug('sending mail to %s', user.email) |
150 |
send_mail(subject, body, self.from_email, [user.email])
|
|
150 |
send_templated_mail(user.email, prefix, ctx)
|
|
151 | 151 |
except Exception: |
152 | 152 |
logger.exception('email sending failure') |
153 | 153 |
src/authentic2/templates/authentic2/unused_account_alert_body.html | ||
---|---|---|
1 |
{% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user.get_full_name }}, |
|
2 | ||
3 |
You have not logged in for {{ threshold }} {{ threshold_unit }}. |
|
4 |
In order to keep your account, you must <a href={{ login_url}}>log in</a> before {{ clean_threshold }} days. |
|
5 |
Otherwise, it will be deleted after this time. |
|
6 |
{% endblocktrans %}{% endautoescape %} |
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 in for {{ threshold }} {{ threshold_unit }}. In {{ clean_threshold }} days your account |
|
4 |
will be deleted.{% endblocktrans %}{% endautoescape %} |
|
3 |
You have not logged in for {{ threshold }} {{ threshold_unit }}. |
|
4 |
In order to keep your account, you must log in before {{ clean_threshold }} days. |
|
5 |
Otherwise, it will be deleted after this time. |
|
6 |
{% endblocktrans %}{% endautoescape %} |
tests/settings.py | ||
---|---|---|
44 | 44 |
A2_AUTH_KERBEROS_ENABLED = False |
45 | 45 | |
46 | 46 |
A2_VALIDATE_EMAIL_DOMAIN = False |
47 | ||
48 |
SITE_BASE_URL = 'http://localhost' |
tests/test_commands.py | ||
---|---|---|
96 | 96 |
assert DeletedUser.objects.filter(user=simple_user).exists() |
97 | 97 | |
98 | 98 | |
99 |
def test_clean_unused_account_login_url(simple_user, mailoutbox): |
|
100 |
simple_user.last_login = now() - datetime.timedelta(days=1) |
|
101 |
simple_user.save() |
|
102 |
management.call_command('clean-unused-accounts', 2, '--alert-thresholds', '1') |
|
103 |
mail = mailoutbox[0] |
|
104 |
assert 'href=http://localhost/login' in mail.message().as_string() |
|
105 | ||
106 | ||
99 | 107 |
def test_cleanupauthentic(db): |
100 | 108 |
management.call_command('cleanupauthentic') |
101 | 109 | |
102 |
- |