Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fdw-forks
django-database-files
Commits
763b0b9d
Commit
763b0b9d
authored
Mar 02, 2012
by
chrisspen
Browse files
Added option to load command to allow limiting file load by app model.
parent
3a3dbd2d
Changes
2
Hide whitespace changes
Inline
Side-by-side
database_files/__init__.py
View file @
763b0b9d
VERSION
=
(
0
,
1
,
2
)
VERSION
=
(
0
,
1
,
3
)
__version__
=
'.'
.
join
(
map
(
str
,
VERSION
))
__version__
=
'.'
.
join
(
map
(
str
,
VERSION
))
\ No newline at end of file
database_files/management/commands/database_files_load.py
View file @
763b0b9d
...
@@ -5,19 +5,35 @@ from django.core.files.storage import default_storage
...
@@ -5,19 +5,35 @@ from django.core.files.storage import default_storage
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.db.models
import
FileField
,
ImageField
,
get_models
from
django.db.models
import
FileField
,
ImageField
,
get_models
from
optparse
import
make_option
class
Command
(
BaseCommand
):
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 '
+
\
help
=
'Loads all files on the filesystem referenced by FileFields '
+
\
'or ImageFields into the database. This should only need to be '
+
\
'or ImageFields into the database. This should only need to be '
+
\
'done once, when initially migrating a legacy system.'
'done once, when initially migrating a legacy system.'
def
handle
(
self
,
*
args
,
**
options
):
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
tmp_debug
=
settings
.
DEBUG
settings
.
DEBUG
=
False
settings
.
DEBUG
=
False
try
:
try
:
broken
=
0
# Number of db records referencing missing files.
broken
=
0
# Number of db records referencing missing files.
for
model
in
get_models
():
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
)):
if
not
isinstance
(
field
,
(
FileField
,
ImageField
)):
continue
continue
print
model
.
__name__
,
field
.
name
print
model
.
__name__
,
field
.
name
...
@@ -31,14 +47,19 @@ class Command(BaseCommand):
...
@@ -31,14 +47,19 @@ class Command(BaseCommand):
continue
continue
if
not
file
.
name
:
if
not
file
.
name
:
continue
continue
if
show_files
:
print
"
\t
"
,
file
.
name
if
file
.
path
and
not
os
.
path
.
isfile
(
file
.
path
):
if
file
.
path
and
not
os
.
path
.
isfile
(
file
.
path
):
if
show_files
:
print
"Broken:"
,
file
.
name
broken
+=
1
broken
+=
1
continue
continue
file
.
read
()
file
.
read
()
row
.
save
()
row
.
save
()
except
IOError
:
except
IOError
:
broken
+=
1
broken
+=
1
print
'-'
*
80
if
show_files
:
print
'%i broken'
%
(
broken
,)
print
'-'
*
80
print
'%i broken'
%
(
broken
,)
finally
:
finally
:
settings
.
DEBUG
=
tmp_debug
settings
.
DEBUG
=
tmp_debug
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment