##// END OF EJS Templates
Merged with default
Merged with default

File last commit:

r1304:8a9a8cc0 default
r1320:c450e81f merge decentral
Show More
cleanfiles.py
46 lines | 1.5 KiB | text/x-python | PythonLexer
import os
from django.core.management import BaseCommand
from django.db import transaction
from boards.models import Attachment
from boards.models.attachment import FILES_DIRECTORY
from boards.models.image import IMAGES_DIRECTORY, PostImage, IMAGE_THUMB_SIZE
from neboard.settings import MEDIA_ROOT
__author__ = 'neko259'
class Command(BaseCommand):
help = 'Remove 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))
count = 0
model_files = os.listdir(MEDIA_ROOT + FILES_DIRECTORY)
for file in model_files:
found = Attachment.objects.filter(file=FILES_DIRECTORY + file)\
.exists()
if not found:
print('Missing {}'.format(file))
os.remove(MEDIA_ROOT + FILES_DIRECTORY + file)
count += 1
print('Deleted {} attachment files.'.format(count))