From 58a61be82f0efef2e3014fe4f41fe4176950341f Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Wed, 9 Oct 2013 18:28:57 -0300 Subject: [PATCH] Adding script to erase inactive accounts --- src/accounts/management/__init__.py | 0 src/accounts/management/commands/__init__.py | 0 src/accounts/management/commands/delete_invalid.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 0 deletions(-) create mode 100644 src/accounts/management/__init__.py create mode 100644 src/accounts/management/commands/__init__.py create mode 100644 src/accounts/management/commands/delete_invalid.py diff --git a/src/accounts/management/__init__.py b/src/accounts/management/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/accounts/management/__init__.py diff --git a/src/accounts/management/commands/__init__.py b/src/accounts/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/accounts/management/commands/__init__.py diff --git a/src/accounts/management/commands/delete_invalid.py b/src/accounts/management/commands/delete_invalid.py new file mode 100644 index 0000000..0335e98 --- /dev/null +++ b/src/accounts/management/commands/delete_invalid.py @@ -0,0 +1,42 @@ + + +from django.db.models import F +from django.utils import timezone +from django.utils.translation import ugettext as _ +from django.core.management.base import BaseCommand, CommandError + + +from ...models import User + + +class Command(BaseCommand): + """Delete user accounts that have never logged in. + + Delete from database user accounts that have never logged in + and are at least 24h older. + + """ + + help = __doc__ + + def handle(self, *args, **kwargs): + seconds = timezone.timedelta(seconds=1) + now = timezone.now() + one_day_ago = timezone.timedelta(days=1) + + # Query for users that have NEVER logged in + # + # By default django sets the last_login as auto_now and then + # last_login is pretty much the same than date_joined + # (instead of null as I expected). Because of that we query + # for users which last_login is between date_joined - N and + # date_joined + N, where N is a small constant in seconds. + users = User.objects.filter(last_login__gt=(F('date_joined') - seconds), + last_login__lt=(F('date_joined') + seconds), + date_joined__lt=now-one_day_ago) + count = 0 + for user in users: + count += 1 + user.delete() + + print _(u'%(count)s users deleted.') % {'count': count} -- libgit2 0.21.2