# HG changeset patch # User neko259 # Date 2015-08-24 13:17:42 # Node ID 8a9a8cc060255a30ceae64a0cd812d3a300e3d68 # Parent 3fd1c7d272a09c518d7bf239874c900bbbd5db42 Updated script to delete attachment files along with images diff --git a/boards/management/commands/cleanimages.py b/boards/management/commands/cleanfiles.py rename from boards/management/commands/cleanimages.py rename to boards/management/commands/cleanfiles.py --- a/boards/management/commands/cleanimages.py +++ b/boards/management/commands/cleanfiles.py @@ -2,6 +2,8 @@ import os from django.core.management import BaseCommand from django.db import transaction +from boards.models import Attachment +from boards.models.attachment import FILES_DIRECTORY from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE from neboard.settings import MEDIA_ROOT @@ -11,7 +13,7 @@ from neboard.settings import MEDIA_ROOT class Command(BaseCommand): - help = 'Remove image files whose models were deleted' + help = 'Remove files whose models were deleted' @transaction.atomic def handle(self, *args, **options): @@ -21,10 +23,24 @@ class Command(BaseCommand): model_files = os.listdir(MEDIA_ROOT + IMAGES_DIRECTORY) for file in model_files: image_name = file if thumb_prefix not in file else file.replace(thumb_prefix, '') - found = PostImage.objects.filter(image=IMAGES_DIRECTORY + image_name).exists() + found = PostImage.objects.filter( + image=IMAGES_DIRECTORY + image_name).exists() if not found: print('Missing {}'.format(image_name)) os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file) count += 1 - print('Deleted {} image files.'.format(count)) \ No newline at end of file + print('Deleted {} image files.'.format(count)) + + count = 0 + model_files = os.listdir(MEDIA_ROOT + FILES_DIRECTORY) + for file in model_files: + found = Attachment.objects.filter(file=FILES_DIRECTORY + file)\ + .exists() + + if not found: + print('Missing {}'.format(file)) + os.remove(MEDIA_ROOT + FILES_DIRECTORY + file) + count += 1 + + print('Deleted {} attachment files.'.format(count))