from django.core.management.base import BaseCommand from django.utils import timezone from admin_app.models import Invitation from auth_app.models import User class Command(BaseCommand): help = 'Clean up duplicate and expired invitations' def add_arguments(self, parser): parser.add_argument( '--dry-run', action='store_true', help='Show what would be cleaned up without making changes', ) parser.add_argument( '--delete-expired', action='store_true', help='Delete expired invitations (instead of just marking as accepted)', ) def handle(self, *args, **options): dry_run = options['dry_run'] delete_expired = options['delete_expired'] self.stdout.write( self.style.SUCCESS('=== Invitation Cleanup Command ===') ) if dry_run: self.stdout.write( self.style.WARNING('DRY RUN MODE - No changes will be made') ) # Get all invitations all_invitations = Invitation.objects.all().order_by('email', '-created_at') self.stdout.write(f"Total invitations found: {all_invitations.count()}") # Track emails and their invitations email_groups = {} for invitation in all_invitations: email = invitation.email.lower() if email not in email_groups: email_groups[email] = [] email_groups[email].append(invitation) deleted_count = 0 updated_count = 0 for email, invitations in email_groups.items(): # Check if user already exists if User.objects.filter(email__iexact=email).exists(): self.stdout.write(f"šŸ”„ User exists for {email}, marking all invitations as accepted") for invitation in invitations: if not dry_run: invitation.is_accepted = True invitation.save() updated_count += 1 continue # If more than one invitation for same email if len(invitations) > 1: self.stdout.write(f"šŸ“§ Found {len(invitations)} invitations for {email}") # Keep the most recent non-expired invitation # Delete others invitations.sort(key=lambda x: x.created_at, reverse=True) keeper = None for invitation in invitations: if not invitation.is_expired() and not invitation.is_accepted: keeper = invitation break if not keeper: # All are expired/accepted, keep the most recent one keeper = invitations[0] self.stdout.write(f" šŸ“ All invitations expired/accepted, keeping most recent: {keeper.token}") else: self.stdout.write(f" āœ… Keeping active invitation: {keeper.token}") # Delete the others for invitation in invitations: if invitation != keeper: self.stdout.write(f" šŸ—‘ļø Deleting duplicate: {invitation.token} (created: {invitation.created_at})") if not dry_run: invitation.delete() deleted_count += 1 # Handle expired invitations if requested elif delete_expired and len(invitations) == 1: invitation = invitations[0] if invitation.is_expired() and not invitation.is_accepted: self.stdout.write(f"šŸ—‘ļø Deleting expired invitation: {email}") if not dry_run: invitation.delete() deleted_count += 1 self.stdout.write( self.style.SUCCESS(f'\\nšŸ“Š Cleanup Summary:') ) self.stdout.write(f" - Updated (marked as accepted): {updated_count}") self.stdout.write(f" - Deleted (duplicates/expired): {deleted_count}") if not dry_run: remaining_count = Invitation.objects.count() self.stdout.write(f" - Remaining invitations: {remaining_count}") if dry_run: self.stdout.write( self.style.WARNING('\\nNo changes were made. Run without --dry-run to apply changes.') )