Skip to content

Commit dfc652a

Browse files
authored
Fix cleanupregistration, skip if user.is_active (#344)
* Fix cleanupregistration, skip if user.is_active * requirements fix * Drop python 3.3
1 parent 69a479f commit dfc652a

10 files changed

+53
-32
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: python
22
sudo: false
33
python:
44
- 2.7
5-
- 3.3
65
- 3.4
76
- 3.5
87
- 3.6

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ django-registration-redux changelog
44

55
Version 1.11, TBD
66
----------------
7+
* Bugfix: Don't delete if user.is_active=True in cleanupregistration. -
8+
`#342 <https://github.com/macropin/django-registration/pull/342>_`
79

810
Version 1.10, 09 February, 2018
911
----------------

docs/upgrade.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Django version requirement
1414

1515
As of |version|, |project| requires Django 1.8 or newer;
1616
older Django releases may work, but are officially unsupported. Additionally,
17-
|project| officially supports Python 2.7, 3.3, 3.4, 3.5, and 3.6.
17+
|project| officially supports Python 2.7, 3.4, 3.5, and 3.6.
1818

1919

2020
Backwards-incompatible changes

registration/models.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ def send_email(addresses_to, ctx_dict, subject_template, body_template,
5757
"""
5858
Function that sends an email
5959
"""
60-
subject = (
61-
getattr(settings, 'REGISTRATION_EMAIL_SUBJECT_PREFIX', '') +
62-
render_to_string(
63-
subject_template, ctx_dict)
64-
)
60+
61+
prefix = getattr(settings, 'REGISTRATION_EMAIL_SUBJECT_PREFIX', '')
62+
subject = prefix + render_to_string(subject_template, ctx_dict)
6563
# Email subject *must not* contain newlines
6664
subject = ''.join(subject.splitlines())
6765
from_email = get_from_email(ctx_dict.get('site'))
@@ -269,9 +267,13 @@ def delete_expired_users(self):
269267
be deleted.
270268
271269
"""
272-
for profile in self.all():
270+
profiles = self.filter(
271+
models.Q(user__is_active=False) | models.Q(user=None),
272+
activated=False,
273+
)
274+
for profile in profiles:
273275
try:
274-
if profile.activation_key_expired() and not profile.activated:
276+
if profile.activation_key_expired():
275277
user = profile.user
276278
logger.warning('Deleting expired Registration profile {} and user {}.'.format(profile, user))
277279
profile.delete()
@@ -350,10 +352,10 @@ def activation_key_expired(self):
350352
method returns ``True``.
351353
352354
"""
353-
expiration_date = datetime.timedelta(
355+
max_expiry_days = datetime.timedelta(
354356
days=settings.ACCOUNT_ACTIVATION_DAYS)
355-
return (self.activated or
356-
(self.user.date_joined + expiration_date <= datetime_now()))
357+
expiration_date = self.user.date_joined + max_expiry_days
358+
return self.activated or expiration_date <= datetime_now()
357359

358360
def send_activation_email(self, site, request=None):
359361
"""
@@ -418,9 +420,11 @@ def send_activation_email(self, site, request=None):
418420
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
419421
'site': site,
420422
}
421-
subject = (getattr(settings, 'REGISTRATION_EMAIL_SUBJECT_PREFIX', '') +
422-
render_to_string(
423-
activation_email_subject, ctx_dict, request=request))
423+
prefix = getattr(settings, 'REGISTRATION_EMAIL_SUBJECT_PREFIX', '')
424+
subject = prefix + render_to_string(
425+
activation_email_subject, ctx_dict, request=request
426+
)
427+
424428
# Email subject *must not* contain newlines
425429
subject = ''.join(subject.splitlines())
426430
from_email = get_from_email(site)

registration/tests/forms_custom_user.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
try:
1010
from importlib import reload # Python 3.4+
11-
except ImportError:
12-
try:
13-
from imp import reload # Python 3.3
14-
except Exception:
15-
pass # Python 2 reload()
11+
except Exception:
12+
pass # Python 2 reload()
1613

1714

1815
@override_settings(AUTH_USER_MODEL='test_app.CustomUser')

registration/tests/models.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ def test_user_creation(self):
169169
self.assertEqual(new_user.email, '[email protected]')
170170
self.failUnless(new_user.check_password('swordfish'))
171171
self.failIf(new_user.is_active)
172-
self.failIf(new_user.date_joined <=
173-
datetime_now() - timedelta(
174-
settings.ACCOUNT_ACTIVATION_DAYS)
175-
)
172+
173+
expiration_date = datetime_now() - timedelta(
174+
settings.ACCOUNT_ACTIVATION_DAYS
175+
)
176+
self.failIf(new_user.date_joined <= expiration_date)
176177

177178
def test_user_creation_email(self):
178179
"""
@@ -204,10 +205,9 @@ def test_user_creation_old_date_joined(self):
204205
self.assertEqual(new_user.email, '[email protected]')
205206
self.failUnless(new_user.check_password('swordfish'))
206207
self.failIf(new_user.is_active)
207-
self.failIf(new_user.date_joined <=
208-
datetime_now() - timedelta(
209-
settings.ACCOUNT_ACTIVATION_DAYS)
210-
)
208+
209+
expiry_date = datetime_now() - timedelta(settings.ACCOUNT_ACTIVATION_DAYS)
210+
self.failIf(new_user.date_joined <= expiry_date)
211211

212212
def test_unexpired_account_old_date_joined(self):
213213
"""
@@ -481,6 +481,27 @@ def test_expired_user_deletion_missing_user(self):
481481
self.assertRaises(UserModel().DoesNotExist,
482482
UserModel().objects.get, username='bob')
483483

484+
def test_manually_registered_account(self):
485+
"""
486+
Test if a user failed to go through the registration flow but was
487+
manually marked ``is_active`` in the DB. Although the profile is
488+
expired and not active, we should never delete active users.
489+
"""
490+
active_user = (self.registration_profile.objects
491+
.create_inactive_user(
492+
site=Site.objects.get_current(),
493+
username='bob',
494+
password='secret',
495+
496+
active_user.date_joined -= datetime.timedelta(
497+
days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
498+
active_user.is_active = True
499+
active_user.save()
500+
501+
self.registration_profile.objects.delete_expired_users()
502+
self.assertEqual(self.registration_profile.objects.count(), 1)
503+
self.assertEqual(UserModel().objects.get(username='bob'), active_user)
504+
484505
def test_management_command(self):
485506
"""
486507
The ``cleanupregistration`` management command properly

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ Sphinx
33
twine
44
pytest>3.2.1
55
Django>1.8
6-
docutils==0.12

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def run_tests(self):
4848
'Programming Language :: Python',
4949
'Programming Language :: Python :: 2.7',
5050
'Programming Language :: Python :: 3',
51-
'Programming Language :: Python :: 3.3',
5251
'Programming Language :: Python :: 3.4',
5352
'Programming Language :: Python :: 3.5',
5453
'Programming Language :: Python :: 3.6',

test-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ coveralls
33
isort<4.3.0
44
tox-travis
55
invoke==0.12.2
6+
docutils

tox.ini

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[tox]
77
envlist =
8-
{py27,py33,py34,py35}-django18,
8+
{py27,py34,py35}-django18,
99
{py27,py34,py35}-{django19,django110},
1010
{py27,py34,py35,py36}-django111,
1111

@@ -22,7 +22,6 @@ deps =
2222
[travis]
2323
python =
2424
2.7: py27
25-
3.3: py33
2625
3.4: py34
2726
3.5: py35
2827
3.6: py36

0 commit comments

Comments
 (0)