forked from GithubBackups/healthchecks
pruneusers
command removes accounts older than 30 days that have never logged in.
This commit is contained in:
parent
521b089501
commit
bc56da1d88
@ -10,32 +10,20 @@ class Command(BaseCommand):
|
|||||||
help = """Prune old, inactive user accounts.
|
help = """Prune old, inactive user accounts.
|
||||||
|
|
||||||
Conditions for removing an user account:
|
Conditions for removing an user account:
|
||||||
- created 6 months ago and never logged in. Does not belong
|
- created 1 month ago and never logged in. Does not belong
|
||||||
to any team.
|
to any team.
|
||||||
Use case: visitor types in their email at the website but
|
Use case: visitor types in their email at the website but
|
||||||
never follows through with login.
|
never follows through with login.
|
||||||
|
|
||||||
- not logged in for 6 months, and has no checks. Does not
|
|
||||||
belong to any team.
|
|
||||||
Use case: user wants to remove their account. So they
|
|
||||||
remove all checks and leave the account at that.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
cutoff = timezone.now() - timedelta(days=180)
|
cutoff = timezone.now() - timedelta(days=30)
|
||||||
|
|
||||||
# Old accounts, never logged in, no team memberships
|
# Old accounts, never logged in, no team memberships
|
||||||
q = User.objects
|
q = User.objects.order_by("id")
|
||||||
q = q.annotate(n_teams=Count("memberships"))
|
q = q.annotate(n_teams=Count("memberships"))
|
||||||
q = q.filter(date_joined__lt=cutoff, last_login=None, n_teams=0)
|
q = q.filter(date_joined__lt=cutoff, last_login=None, n_teams=0)
|
||||||
n1, _ = q.delete()
|
|
||||||
|
|
||||||
# Not logged in for 1 month, 0 checks, no team memberships
|
n, summary = q.delete()
|
||||||
q = User.objects
|
return "Done! Pruned %d user accounts." % summary.get("auth.User", 0)
|
||||||
q = q.annotate(n_checks=Count("check"))
|
|
||||||
q = q.annotate(n_teams=Count("memberships"))
|
|
||||||
q = q.filter(last_login__lt=cutoff, n_checks=0, n_teams=0)
|
|
||||||
n2, _ = q.delete()
|
|
||||||
|
|
||||||
return "Done! Pruned %d user accounts." % (n1 + n2)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user