Show More
@@ -2,6 +2,8 b' 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 |
@@ -11,7 +13,7 b' from neboard.settings import MEDIA_ROOT' | |||
|
11 | 13 | |
|
12 | 14 | |
|
13 | 15 | class Command(BaseCommand): |
|
14 |
help = 'Remove |
|
|
16 | help = 'Remove files whose models were deleted' | |
|
15 | 17 | |
|
16 | 18 | @transaction.atomic |
|
17 | 19 | def handle(self, *args, **options): |
@@ -21,10 +23,24 b' class Command(BaseCommand):' | |||
|
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( |
|
|
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