Show More
@@ -1,19 +1,30 b'' | |||||
|
1 | import os | |||
|
2 | ||||
1 | from django.core.management import BaseCommand |
|
3 | from django.core.management import BaseCommand | |
2 | from django.db import transaction |
|
4 | from django.db import transaction | |
3 | from django.db.models import Count |
|
|||
4 |
|
5 | |||
5 | from boards.models import Tag |
|
6 | from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE | |
|
7 | from neboard.settings import MEDIA_ROOT | |||
6 |
|
8 | |||
7 |
|
9 | |||
8 | __author__ = 'neko259' |
|
10 | __author__ = 'neko259' | |
9 |
|
11 | |||
10 |
|
12 | |||
11 | class Command(BaseCommand): |
|
13 | class Command(BaseCommand): | |
12 |
help = 'Remove |
|
14 | help = 'Remove image files whose models were deleted' | |
13 |
|
15 | |||
14 | @transaction.atomic |
|
16 | @transaction.atomic | |
15 | def handle(self, *args, **options): |
|
17 | def handle(self, *args, **options): | |
16 | empty = Tag.objects.annotate(num_threads=Count('thread'))\ |
|
18 | count = 0 | |
17 | .filter(num_threads=0).order_by('-required', 'name') |
|
19 | thumb_prefix = '.{}x{}'.format(*IMAGE_THUMB_SIZE) | |
18 | print('Removing {} empty tags'.format(empty.count())) |
|
20 | ||
19 | empty.delete() |
|
21 | model_files = os.listdir(MEDIA_ROOT + IMAGES_DIRECTORY) | |
|
22 | for file in model_files: | |||
|
23 | 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() | |||
|
25 | ||||
|
26 | if not found: | |||
|
27 | print('Missing {}'.format(image_name)) | |||
|
28 | os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file) | |||
|
29 | count += 1 | |||
|
30 | print('Deleted {} image files.'.format(count)) No newline at end of file |
@@ -142,7 +142,6 b' INSTALLED_APPS = (' | |||||
142 | # Uncomment the next line to enable admin documentation: |
|
142 | # Uncomment the next line to enable admin documentation: | |
143 | # 'django.contrib.admindocs', |
|
143 | # 'django.contrib.admindocs', | |
144 | 'django.contrib.humanize', |
|
144 | 'django.contrib.humanize', | |
145 | 'django_cleanup', |
|
|||
146 |
|
145 | |||
147 | 'debug_toolbar', |
|
146 | 'debug_toolbar', | |
148 |
|
147 |
General Comments 0
You need to be logged in to leave comments.
Login now