""" Management command to cleanup old notifications. Deletes notifications older than 30 days (configurable). Run via cron or celery beat daily. Usage: python manage.py cleanup_notifications python manage.py cleanup_notifications --days=30 python manage.py cleanup_notifications --dry-run """ from django.core.management.base import BaseCommand from django.utils import timezone from datetime import timedelta from apps.notifications.models import UserNotification class Command(BaseCommand): help = "Cleanup old notifications (default: 30 days)" def add_arguments(self, parser): parser.add_argument("--days", type=int, default=30, help="Number of days to keep notifications (default: 30)") parser.add_argument( "--dry-run", action="store_true", help="Show what would be deleted without actually deleting" ) def handle(self, *args, **options): days = options["days"] dry_run = options["dry_run"] cutoff_date = timezone.now() - timedelta(days=days) # Get notifications to delete (both read and unread, older than cutoff) notifications_to_delete = UserNotification.objects.filter(created_at__lt=cutoff_date) count = notifications_to_delete.count() if dry_run: self.stdout.write( self.style.WARNING(f"[DRY RUN] Would delete {count} notifications older than {days} days") ) # Show sample of what would be deleted sample = notifications_to_delete[:5] for n in sample: self.stdout.write(f" - {n.title} ({n.created_at})") if count > 5: self.stdout.write(f" ... and {count - 5} more") else: if count > 0: notifications_to_delete.delete() self.stdout.write(self.style.SUCCESS(f"Deleted {count} notifications older than {days} days")) else: self.stdout.write(self.style.SUCCESS("No notifications to delete"))