Commit 763b0b9d authored by chrisspen's avatar chrisspen
Browse files

Added option to load command to allow limiting file load by app model.

parent 3a3dbd2d
VERSION = (0, 1, 2)
VERSION = (0, 1, 3)
__version__ = '.'.join(map(str, VERSION))
\ No newline at end of file
......@@ -5,19 +5,35 @@ from django.core.files.storage import default_storage
from django.core.management.base import BaseCommand, CommandError
from django.db.models import FileField, ImageField, get_models
from optparse import make_option
class Command(BaseCommand):
args = ''
option_list = BaseCommand.option_list + (
make_option('-m', '--models',
dest='models', default='',
help='A list of models to search for file fields. Default is all.'),
)
help = 'Loads all files on the filesystem referenced by FileFields ' + \
'or ImageFields into the database. This should only need to be ' + \
'done once, when initially migrating a legacy system.'
def handle(self, *args, **options):
show_files = int(options.get('verbosity', 1)) >= 2
all_models = [
_.lower().strip()
for _ in options.get('models', '').split()
if _.strip()
]
tmp_debug = settings.DEBUG
settings.DEBUG = False
try:
broken = 0 # Number of db records referencing missing files.
for model in get_models():
for field in model._meta.fields:
key = "%s.%s" % (model._meta.app_label,model._meta.module_name)
key = key.lower()
if all_models and key not in all_models:
continue
for field in model._meta.fields:
if not isinstance(field, (FileField, ImageField)):
continue
print model.__name__, field.name
......@@ -31,14 +47,19 @@ class Command(BaseCommand):
continue
if not file.name:
continue
if show_files:
print "\t",file.name
if file.path and not os.path.isfile(file.path):
if show_files:
print "Broken:",file.name
broken += 1
continue
file.read()
row.save()
except IOError:
broken += 1
print '-'*80
print '%i broken' % (broken,)
if show_files:
print '-'*80
print '%i broken' % (broken,)
finally:
settings.DEBUG = tmp_debug
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