From 2d3e039414afe4a4496a9c382fae25c008f442ab Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 16 Nov 2024 21:31:18 +0100 Subject: [PATCH] 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. --- .../registration/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/cleanup.py | 26 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/synapse_registration/registration/management/__init__.py create mode 100644 src/synapse_registration/registration/management/commands/__init__.py create mode 100644 src/synapse_registration/registration/management/commands/cleanup.py diff --git a/src/synapse_registration/registration/management/__init__.py b/src/synapse_registration/registration/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/synapse_registration/registration/management/commands/__init__.py b/src/synapse_registration/registration/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/synapse_registration/registration/management/commands/cleanup.py b/src/synapse_registration/registration/management/commands/cleanup.py new file mode 100644 index 0000000..a623a4f --- /dev/null +++ b/src/synapse_registration/registration/management/commands/cleanup.py @@ -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") + )