Skip to content

Commit

Permalink
[#N/A] Demo command.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmckissock committed Aug 5, 2024
1 parent 8170c44 commit 9376599
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
54 changes: 54 additions & 0 deletions audioapp/management/commands/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from audioapp.models import AudioFile
from django.core.files import File
import os
import shutil



class Command(BaseCommand):
help = "Seed the database with initial data"

def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS("Deleting old data..."))

# Delete all existing users and audio files
User.objects.all().delete()

audio_files_dir = os.path.join('media/audio_files')
if os.path.exists(audio_files_dir):
shutil.rmtree(audio_files_dir)
os.makedirs(audio_files_dir) # Recreate the directory
self.stdout.write(self.style.SUCCESS("All files in audio_files/ directory removed."))
self.stdout.write(self.style.SUCCESS("Seeding data..."))

base_path ='audioapp/demo_audios/'
# Create users
users = []
for user_folder in os.listdir(base_path):
item_path = os.path.join(base_path, user_folder)
user_name = os.path.basename(item_path)
user = User.objects.create_user(
username=user_name, password="password"
)
users.append(user)

#create sounds
for user in users:
user_path = os.path.join(base_path, user.username)
print(user_path)
for root, dirs, files in os.walk(user_path):
for file in files:
audio_path = os.path.join(user_path, file)
title = file[:-4]
description = "This is a description for a sound by", user.username
audio_file = AudioFile(
user=user,
title=title,
description=description,
)
with open(audio_path, "rb") as f:
audio_file.file.save(file, File(f), save=True)

self.stdout.write(self.style.SUCCESS("Database seeded successfully!"))

0 comments on commit 9376599

Please sign in to comment.