Skip to content

Commit 83e2182

Browse files
authored
Fix cleanupregistration, skip if user.is_active (#342)
* Fix cleanupregistration, skip if user.is_active * Fix W504 * changelog * fix
1 parent 8ad4200 commit 83e2182

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Version 2.5, TBD
66
----------------
77
* Feature: Add support for Django 2.1. -
88
`#337 <https://github.com/macropin/django-registration/pull/337>_`
9+
* Bugfix: Don't delete if user.is_active=True in cleanupregistration. -
10+
`#342 <https://github.com/macropin/django-registration/pull/342>_`
911

1012
Version 2.4, 11 April, 2018
1113
----------------

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/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

0 commit comments

Comments
 (0)