-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split media and static folders (#340)
* Split media and static folders, command to clean static folders in media * Fix flake error in cleanmediafolder.py
- Loading branch information
Showing
2 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from shutil import rmtree | ||
from django.conf import settings | ||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
|
||
class Command(BaseCommand): | ||
help = 'Run collectstatic and delete folders from media that exist in static' | ||
|
||
def handle(self, *args, **options): | ||
confirm = input("Do you want to run 'collectstatic' first? (yes/no): ") | ||
if confirm.lower() == 'yes': | ||
# Run collectstatic command | ||
call_command('collectstatic', interactive=False) | ||
|
||
# Get the paths of static and media folders | ||
static_root = settings.STATIC_ROOT | ||
media_root = settings.MEDIA_ROOT | ||
|
||
# Iterate over each directory in the static folder | ||
for static_dir in os.listdir(static_root): | ||
static_path = os.path.join(static_root, static_dir) | ||
|
||
# Check if the path is a directory and exists in the media folder | ||
if os.path.isdir(static_path) and os.path.exists(os.path.join(media_root, static_dir)): | ||
confirm = input(f"Are you sure you want to delete '{static_dir}' from the media folder? (yes/no): ") | ||
|
||
if confirm.lower() == 'yes': | ||
try: | ||
# Delete the corresponding folder in the media folder | ||
rmtree(os.path.join(media_root, static_dir)) | ||
self.stdout.write(self.style.SUCCESS(f'Deleted {static_dir} from media folder.')) | ||
except Exception as e: | ||
self.stderr.write(self.style.ERROR(f'Error deleting {static_dir}: {str(e)}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'Skipped deletion of {static_dir}.')) | ||
|
||
self.stdout.write(self.style.SUCCESS('The media folder has been cleaned.')) |