You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

61 lines
3.0 KiB

from django.core.management.base import BaseCommand
from django.db import transaction
from apps.podcast.models import PodcastCategory, PodcastCollection, PodcastPlaylist, PlaylistItem
class Command(BaseCommand):
help = 'Delete all data from PodcastCategory, PodcastCollection, and PodcastPlaylist (keeps Podcast model data)'
def add_arguments(self, parser):
parser.add_argument(
'--confirm',
action='store_true',
help='Confirm deletion without prompting'
)
def handle(self, *args, **options):
confirm = options.get('confirm', False)
# Count current data
category_count = PodcastCategory.objects.count()
collection_count = PodcastCollection.objects.count()
playlist_count = PodcastPlaylist.objects.count()
playlist_item_count = PlaylistItem.objects.count()
self.stdout.write(self.style.WARNING('\n=== Current Data Count ==='))
self.stdout.write(f'PodcastCategory: {category_count}')
self.stdout.write(f'PodcastCollection: {collection_count}')
self.stdout.write(f'PodcastPlaylist: {playlist_count}')
self.stdout.write(f'PlaylistItem: {playlist_item_count}')
self.stdout.write(self.style.WARNING('\n=== Podcast Data Will NOT Be Deleted ===\n'))
if not confirm:
user_input = input('Are you sure you want to delete this data? Type "yes" to confirm: ')
if user_input.lower() != 'yes':
self.stdout.write(self.style.ERROR('Operation cancelled.'))
return
try:
with transaction.atomic():
# Delete in order to respect foreign key constraints
# 1. Delete PlaylistItem first (references PodcastPlaylist)
deleted_playlist_items = PlaylistItem.objects.all().delete()
self.stdout.write(self.style.SUCCESS(f'✓ Deleted {deleted_playlist_items[0]} PlaylistItems'))
# 2. Delete PodcastPlaylist (may reference PodcastCategory and PodcastCollection through M2M)
deleted_playlists = PodcastPlaylist.objects.all().delete()
self.stdout.write(self.style.SUCCESS(f'✓ Deleted {deleted_playlists[0]} PodcastPlaylists'))
# 3. Delete PodcastCollection
deleted_collections = PodcastCollection.objects.all().delete()
self.stdout.write(self.style.SUCCESS(f'✓ Deleted {deleted_collections[0]} PodcastCollections'))
# 4. Delete PodcastCategory
deleted_categories = PodcastCategory.objects.all().delete()
self.stdout.write(self.style.SUCCESS(f'✓ Deleted {deleted_categories[0]} PodcastCategories'))
self.stdout.write(self.style.SUCCESS('\n✓ All data deleted successfully!'))
except Exception as e:
self.stdout.write(self.style.ERROR(f'\n✗ Error during deletion: {str(e)}'))
raise