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