##// END OF EJS Templates
Cosmetic fix for tags page
Cosmetic fix for tags page

File last commit:

r1590:0eb7ac3c default
r1746:fedc25df default
Show More
cleanfiles.py
36 lines | 1.0 KiB | text/x-python | PythonLexer
neko259
Updated script to delete attachment files along with images
r1304 import os
neko259
Store images as regular attachments instead of separate model
r1590 from boards.abstracts.constants import FILE_DIRECTORY
neko259
Updated script to delete attachment files along with images
r1304
from django.core.management import BaseCommand
from django.db import transaction
from boards.models import Attachment
from neboard.settings import MEDIA_ROOT
__author__ = 'neko259'
neko259
Store images as regular attachments instead of separate model
r1590 THUMB_SIZE = (200, 150)
neko259
Updated script to delete attachment files along with images
r1304
class Command(BaseCommand):
help = 'Remove files whose models were deleted'
@transaction.atomic
def handle(self, *args, **options):
neko259
Store images as regular attachments instead of separate model
r1590 thumb_prefix = '.{}x{}'.format(*THUMB_SIZE)
neko259
Updated script to delete attachment files along with images
r1304
count = 0
neko259
Store images as regular attachments instead of separate model
r1590 model_files = os.listdir(MEDIA_ROOT + FILE_DIRECTORY)
neko259
Updated script to delete attachment files along with images
r1304 for file in model_files:
neko259
Store images as regular attachments instead of separate model
r1590 model_filename = file if thumb_prefix not in file else file.replace(
thumb_prefix, '')
found = Attachment.objects.filter(file=FILE_DIRECTORY + model_filename)\
neko259
Updated script to delete attachment files along with images
r1304 .exists()
if not found:
print('Missing {}'.format(file))
neko259
Store images as regular attachments instead of separate model
r1590 os.remove(MEDIA_ROOT + FILE_DIRECTORY + file)
neko259
Updated script to delete attachment files along with images
r1304 count += 1
print('Deleted {} attachment files.'.format(count))