cleanfiles.py
36 lines
| 1.0 KiB
| text/x-python
|
PythonLexer
|
r1304 | import os | ||
|
r1590 | from boards.abstracts.constants import FILE_DIRECTORY | ||
|
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' | ||||
|
r1590 | THUMB_SIZE = (200, 150) | ||
|
r1304 | |||
class Command(BaseCommand): | ||||
help = 'Remove files whose models were deleted' | ||||
@transaction.atomic | ||||
def handle(self, *args, **options): | ||||
|
r1590 | thumb_prefix = '.{}x{}'.format(*THUMB_SIZE) | ||
|
r1304 | |||
count = 0 | ||||
|
r1590 | model_files = os.listdir(MEDIA_ROOT + FILE_DIRECTORY) | ||
|
r1304 | for file in model_files: | ||
|
r1590 | model_filename = file if thumb_prefix not in file else file.replace( | ||
thumb_prefix, '') | ||||
found = Attachment.objects.filter(file=FILE_DIRECTORY + model_filename)\ | ||||
|
r1304 | .exists() | ||
if not found: | ||||
print('Missing {}'.format(file)) | ||||
|
r1590 | os.remove(MEDIA_ROOT + FILE_DIRECTORY + file) | ||
|
r1304 | count += 1 | ||
print('Deleted {} attachment files.'.format(count)) | ||||