Commit ae90cca8 authored by chris's avatar chris
Browse files

Improved performance of cleanup command.

parent a3f570c8
VERSION = (0, 1, 12)
VERSION = (0, 1, 13)
__version__ = '.'.join(map(str, VERSION))
\ No newline at end of file
......@@ -28,13 +28,20 @@ class Command(BaseCommand):
dryrun = options['dryrun']
try:
for model in get_models():
for field in model._meta.fields:
print 'Checking model %s...' % (model,)
for field in model._meta.fields:
if not isinstance(field, (FileField, ImageField)):
continue
# Ignore records with null or empty string values.
q = {'%s__isnull'%field.name:False}
xq = {field.name:''}
for row in model.objects.filter(**q).exclude(**xq):
subq = model.objects.filter(**q).exclude(**xq)
subq_total = subq.count()
subq_i = 0
for row in subq.iterator():
subq_i += 1
if subq_i == 1 or not subq_i % 100:
print '%i of %i' % (subq_i, subq_total)
file = getattr(row, field.name)
if file is None:
continue
......@@ -42,9 +49,16 @@ class Command(BaseCommand):
continue
names.add(file.name)
# Find all database files with names not in our list.
orphan_files = File.objects.exclude(name__in=names)
print 'Finding orphaned files...'
orphan_files = File.objects.exclude(name__in=names).only('name', 'size')
total_bytes = 0
for f in orphan_files:
orphan_total = orphan_files.count()
orphan_i = 0
print 'Deleting %i orphaned files...' % (orphan_total,)
for f in orphan_files.iterator():
orphan_i += 1
if orphan_i == 1 or not orphan_i % 100:
print '%i of %i' % (orphan_i, orphan_total)
total_bytes += f.size
if dryrun:
print 'File %s is orphaned.' % (f.name,)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment