mirror of
https://github.com/healthchecks/healthchecks.git
synced 2025-04-11 15:51:19 +00:00
pruneemails and pruneusers commands for database cleanup
This commit is contained in:
parent
6c3a99e977
commit
bbc87fe9bf
3 changed files with 51 additions and 1 deletions
hc
37
hc/accounts/management/commands/pruneusers.py
Normal file
37
hc/accounts/management/commands/pruneusers.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
from datetime import timedelta
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Count
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = """Prune old, inactive user accounts.
|
||||
|
||||
Conditions for removing an user account:
|
||||
- created 1+ month ago and never logged in.
|
||||
Use case: visitor types in their email at the website but
|
||||
never follows through with login.
|
||||
|
||||
- not logged in for 1 month, and has no checks
|
||||
Use case: user wants to remove their account. So they
|
||||
remove all checks and leave the account at that.
|
||||
|
||||
"""
|
||||
|
||||
def handle(self, *args, **options):
|
||||
cutoff = timezone.now() - timedelta(days=31)
|
||||
|
||||
# Old accounts, never logged in
|
||||
q = User.objects
|
||||
q = q.filter(date_joined__lt=cutoff, last_login=None)
|
||||
n1, _ = q.delete()
|
||||
|
||||
# Not logged in for 1 month, 0 checks
|
||||
q = User.objects
|
||||
q = q.annotate(n_checks=Count("check"))
|
||||
q = q.filter(last_login__lt=cutoff, n_checks=0)
|
||||
n2, _ = q.delete()
|
||||
|
||||
return "Done! Pruned %d user accounts." % (n1 + n2)
|
14
hc/api/management/commands/pruneemails.py
Normal file
14
hc/api/management/commands/pruneemails.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from datetime import timedelta
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
from djmail.models import Message
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Prune stored email messages older than 7 days'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
cutoff = timezone.now() - timedelta(days=7)
|
||||
n, _ = Message.objects.filter(sent_at__lt=cutoff).delete()
|
||||
return "Done! Pruned %d email messages." % n
|
|
@ -1,5 +1,4 @@
|
|||
from datetime import timedelta as td
|
||||
import json
|
||||
|
||||
from django.db.models import F
|
||||
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
||||
|
|
Loading…
Add table
Reference in a new issue