From 6d03c8428abe9df7634bbf810bbcb63709b11d45 Mon Sep 17 00:00:00 2001 From: Lova ANDRIARIMALALA <43842786+Xpirix@users.noreply.github.com> Date: Fri, 19 Jan 2024 10:11:31 +0300 Subject: [PATCH] Split media and static folders (#340) * Split media and static folders, command to clean static folders in media * Fix flake error in cleanmediafolder.py --- dockerize/docker-compose.yml | 20 +++++----- .../management/commands/cleanmediafolder.py | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 qgis-app/plugins/management/commands/cleanmediafolder.py diff --git a/dockerize/docker-compose.yml b/dockerize/docker-compose.yml index eb50ecfd..80578f9f 100644 --- a/dockerize/docker-compose.yml +++ b/dockerize/docker-compose.yml @@ -1,4 +1,7 @@ version: "3.8" +volumes: + django-statics-data: {} + django-media-data: {} services: db: container_name: qgis-plugins-db @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/qgis-app/plugins/management/commands/cleanmediafolder.py b/qgis-app/plugins/management/commands/cleanmediafolder.py new file mode 100644 index 00000000..da18bda9 --- /dev/null +++ b/qgis-app/plugins/management/commands/cleanmediafolder.py @@ -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.'))