@@ -0,0 +1,1580 @@
+from pathlib import Path%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.conf import settings%0A%0Afrom geotrek.common.models import Attachment%0Afrom easy_thumbnails.models import Thumbnail%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Remove files for deleted attachments%22%0A%0A    def handle(self, *args, **options):%0A        paperclip_dir = Path(settings.MEDIA_ROOT) / 'paperclip'%0A        attachments = set(Attachment.objects.values_list('attachment_file', flat=True))%0A        thumbnails = set(Thumbnail.objects.values_list('name', flat=True))%0A        if options%5B'verbosity'%5D %3E= 1:%0A            self.stdout.write(u%22Attachments: %7B%7D / Thumbnails: %7B%7D%22.format(len(attachments), len(thumbnails)))%0A        total = 0%0A        deleted = 0%0A        for path in paperclip_dir.glob('**/*'):%0A            if not path.is_file():%0A                continue%0A            total += 1%0A            relative = str(path.relative_to(settings.MEDIA_ROOT))%0A            if relative in attachments:%0A                if options%5B'verbosity'%5D %3E= 2:%0A                    self.stdout.write(u%22%7B%7D... Found%22.format(relative))%0A                continue%0A            if relative in thumbnails:%0A                if options%5B'verbosity'%5D %3E= 2:%0A                    self.stdout.write(u%22%7B%7D... Thumbnail%22.format(relative))%0A                continue%0A            deleted += 1%0A            path.unlink()%0A            if options%5B'verbosity'%5D %3E= 1:%0A                self.stdout.write(u%22%7B%7D... DELETED%22.format(relative))%0A        if options%5B'verbosity'%5D %3E= 1:%0A            self.stdout.write(u%22Files: %7B%7D / Deleted: %7B%7D%22.format(total, deleted))%0A

Add a command to clean attachments