Skip to content

Commit

Permalink
Split media and static folders (#340)
Browse files Browse the repository at this point in the history
* Split media and static folders, command to clean static folders in media

* Fix flake error in cleanmediafolder.py
  • Loading branch information
Xpirix authored Jan 19, 2024
1 parent 01467b2 commit 6d03c84
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
20 changes: 11 additions & 9 deletions dockerize/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version: "3.8"
volumes:
django-statics-data: {}
django-media-data: {}
services:
db:
container_name: qgis-plugins-db
Expand Down Expand Up @@ -28,8 +31,8 @@ services:
- RABBITMQ_HOST=rabbitmq
volumes:
- ../qgis-app:/home/web/django_project
- ./static:/home/web/static:rw
- ./static:/home/web/media:rw
- django-statics-data:/home/web/static:rw
- django-media-data:/home/web/media:rw
links:
- db:db
- rabbitmq:rabbitmq
Expand All @@ -54,8 +57,8 @@ services:
- RABBITMQ_HOST=rabbitmq
volumes:
- ../qgis-app:/home/web/django_project
- ./static:/home/web/static:rw
- ./static:/home/web/media:rw
- django-statics-data:/home/web/static:rw
- django-media-data:/home/web/media:rw
links:
- db:db
- rabbitmq:rabbitmq
Expand Down Expand Up @@ -95,8 +98,8 @@ services:
- RABBITMQ_HOST=rabbitmq
volumes:
- ../qgis-app:/home/web/django_project
- ./static:/home/web/static:rw
- ./static:/home/web/media:rw
- django-statics-data:/home/web/static:rw
- django-media-data:/home/web/media:rw
links:
- db:db
- rabbitmq:rabbitmq
Expand All @@ -108,9 +111,8 @@ services:
hostname: nginx
volumes:
- ./sites-enabled:/etc/nginx/conf.d:ro
# I dont use volumes_from as I want to use the ro modifier
- ./static:/home/web/static:ro
- ./static:/home/web/media:ro
- django-statics-data:/home/web/static:ro
- django-media-data:/home/web/media:ro
- ./logs:/var/log/nginx
links:
- web:uwsgi
Expand Down
38 changes: 38 additions & 0 deletions qgis-app/plugins/management/commands/cleanmediafolder.py
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.'))

0 comments on commit 6d03c84

Please sign in to comment.