feat: Add management command to clean old registrations

Implements a Django management command to automatically
remove outdated user registrations. Cleans 'started'
registrations older than 48 hours and 'denied' or
'approved' registrations older than 30 days, for
privacy reasons.
This commit is contained in:
Kumi 2024-11-16 21:31:18 +01:00
parent eb0a58170c
commit 2d3e039414
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 26 additions and 0 deletions

View file

@ -0,0 +1,26 @@
from django.core.management.base import BaseCommand
from ...models import UserRegistration
from datetime import timedelta, datetime
class Command(BaseCommand):
help = "Clean up old user registrations"
def handle(self, *args, **options):
# Remove all registrations that are still in the "started" state after 48 hours
UserRegistration.objects.filter(
status=UserRegistration.STATUS_STARTED,
timestamp__lt=datetime.now() - timedelta(hours=48),
).delete()
# Remove all registrations that are denied or approved after 30 days
UserRegistration.objects.filter(
status__in=[UserRegistration.STATUS_DENIED, UserRegistration.STATUS_APPROVED],
timestamp__lt=datetime.now() - timedelta(days=30),
).delete()
self.stdout.write(
self.style.SUCCESS("Successfully cleaned up old user registrations")
)