Commit 58a61be82f0efef2e3014fe4f41fe4176950341f
1 parent
a991d922
Exists in
master
and in
39 other branches
Adding script to erase inactive accounts
Showing
3 changed files
with
42 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,42 @@ |
| 1 | + | |
| 2 | + | |
| 3 | +from django.db.models import F | |
| 4 | +from django.utils import timezone | |
| 5 | +from django.utils.translation import ugettext as _ | |
| 6 | +from django.core.management.base import BaseCommand, CommandError | |
| 7 | + | |
| 8 | + | |
| 9 | +from ...models import User | |
| 10 | + | |
| 11 | + | |
| 12 | +class Command(BaseCommand): | |
| 13 | + """Delete user accounts that have never logged in. | |
| 14 | + | |
| 15 | + Delete from database user accounts that have never logged in | |
| 16 | + and are at least 24h older. | |
| 17 | + | |
| 18 | + """ | |
| 19 | + | |
| 20 | + help = __doc__ | |
| 21 | + | |
| 22 | + def handle(self, *args, **kwargs): | |
| 23 | + seconds = timezone.timedelta(seconds=1) | |
| 24 | + now = timezone.now() | |
| 25 | + one_day_ago = timezone.timedelta(days=1) | |
| 26 | + | |
| 27 | + # Query for users that have NEVER logged in | |
| 28 | + # | |
| 29 | + # By default django sets the last_login as auto_now and then | |
| 30 | + # last_login is pretty much the same than date_joined | |
| 31 | + # (instead of null as I expected). Because of that we query | |
| 32 | + # for users which last_login is between date_joined - N and | |
| 33 | + # date_joined + N, where N is a small constant in seconds. | |
| 34 | + users = User.objects.filter(last_login__gt=(F('date_joined') - seconds), | |
| 35 | + last_login__lt=(F('date_joined') + seconds), | |
| 36 | + date_joined__lt=now-one_day_ago) | |
| 37 | + count = 0 | |
| 38 | + for user in users: | |
| 39 | + count += 1 | |
| 40 | + user.delete() | |
| 41 | + | |
| 42 | + print _(u'%(count)s users deleted.') % {'count': count} | ... | ... |