##// END OF EJS Templates
Updated script to delete attachment files along with images
neko259 -
r1304:8a9a8cc0 default
parent child Browse files
Show More
@@ -1,30 +1,46 b''
1 1 import os
2 2
3 3 from django.core.management import BaseCommand
4 4 from django.db import transaction
5 from boards.models import Attachment
6 from boards.models.attachment import FILES_DIRECTORY
5 7
6 8 from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE
7 9 from neboard.settings import MEDIA_ROOT
8 10
9 11
10 12 __author__ = 'neko259'
11 13
12 14
13 15 class Command(BaseCommand):
14 help = 'Remove image files whose models were deleted'
16 help = 'Remove files whose models were deleted'
15 17
16 18 @transaction.atomic
17 19 def handle(self, *args, **options):
18 20 count = 0
19 21 thumb_prefix = '.{}x{}'.format(*IMAGE_THUMB_SIZE)
20 22
21 23 model_files = os.listdir(MEDIA_ROOT + IMAGES_DIRECTORY)
22 24 for file in model_files:
23 25 image_name = file if thumb_prefix not in file else file.replace(thumb_prefix, '')
24 found = PostImage.objects.filter(image=IMAGES_DIRECTORY + image_name).exists()
26 found = PostImage.objects.filter(
27 image=IMAGES_DIRECTORY + image_name).exists()
25 28
26 29 if not found:
27 30 print('Missing {}'.format(image_name))
28 31 os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file)
29 32 count += 1
30 print('Deleted {} image files.'.format(count)) No newline at end of file
33 print('Deleted {} image files.'.format(count))
34
35 count = 0
36 model_files = os.listdir(MEDIA_ROOT + FILES_DIRECTORY)
37 for file in model_files:
38 found = Attachment.objects.filter(file=FILES_DIRECTORY + file)\
39 .exists()
40
41 if not found:
42 print('Missing {}'.format(file))
43 os.remove(MEDIA_ROOT + FILES_DIRECTORY + file)
44 count += 1
45
46 print('Deleted {} attachment files.'.format(count))
General Comments 0
You need to be logged in to leave comments. Login now