##// END OF EJS Templates
Updated script to delete attachment files along with images
neko259 -
r1304:8a9a8cc0 default
parent child Browse files
Show More
@@ -2,6 +2,8 b' import os'
2
2
3 from django.core.management import BaseCommand
3 from django.core.management import BaseCommand
4 from django.db import transaction
4 from django.db import transaction
5 from boards.models import Attachment
6 from boards.models.attachment import FILES_DIRECTORY
5
7
6 from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE
8 from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE
7 from neboard.settings import MEDIA_ROOT
9 from neboard.settings import MEDIA_ROOT
@@ -11,7 +13,7 b' from neboard.settings import MEDIA_ROOT'
11
13
12
14
13 class Command(BaseCommand):
15 class Command(BaseCommand):
14 help = 'Remove image files whose models were deleted'
16 help = 'Remove files whose models were deleted'
15
17
16 @transaction.atomic
18 @transaction.atomic
17 def handle(self, *args, **options):
19 def handle(self, *args, **options):
@@ -21,10 +23,24 b' class Command(BaseCommand):'
21 model_files = os.listdir(MEDIA_ROOT + IMAGES_DIRECTORY)
23 model_files = os.listdir(MEDIA_ROOT + IMAGES_DIRECTORY)
22 for file in model_files:
24 for file in model_files:
23 image_name = file if thumb_prefix not in file else file.replace(thumb_prefix, '')
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 if not found:
29 if not found:
27 print('Missing {}'.format(image_name))
30 print('Missing {}'.format(image_name))
28 os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file)
31 os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file)
29 count += 1
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