##// END OF EJS Templates
Show link to posts in this tag for the tag image even if the image is used in...
Show link to posts in this tag for the tag image even if the image is used in other tag's posts

File last commit:

r1139:100ccd67 default
r1263:fe04a375 default
Show More
cleanimages.py
29 lines | 975 B | text/x-python | PythonLexer
neko259
Added management command to delete old images instead of using django_cleanup
r1139 import os
from django.core.management import BaseCommand
from django.db import transaction
from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE
from neboard.settings import MEDIA_ROOT
__author__ = 'neko259'
class Command(BaseCommand):
help = 'Remove image files whose models were deleted'
@transaction.atomic
def handle(self, *args, **options):
count = 0
thumb_prefix = '.{}x{}'.format(*IMAGE_THUMB_SIZE)
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()
if not found:
print('Missing {}'.format(image_name))
os.remove(MEDIA_ROOT + IMAGES_DIRECTORY + file)
count += 1
print('Deleted {} image files.'.format(count))