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
090ade50
Commit
090ade50
authored
Aug 20, 2012
by
chris
Browse files
Added a dryrun option to the cleanup command. Fixed minor bug in content
read caused by partially pre-read file object.
parent
a5dae1ca
Changes
3
Hide whitespace changes
Inline
Side-by-side
database_files/__init__.py
View file @
090ade50
VERSION
=
(
0
,
1
,
6
)
VERSION
=
(
0
,
1
,
7
)
__version__
=
'.'
.
join
(
map
(
str
,
VERSION
))
\ No newline at end of file
database_files/management/commands/database_files_cleanup.py
View file @
090ade50
import
os
from
optparse
import
make_option
from
django.conf
import
settings
from
django.core.files.storage
import
default_storage
...
...
@@ -11,11 +12,20 @@ class Command(BaseCommand):
args
=
''
help
=
'Deletes all files in the database that are not referenced by '
+
\
'any model fields.'
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--dryrun'
,
action
=
'store_true'
,
dest
=
'dryrun'
,
default
=
False
,
help
=
'If given, only displays the names of orphaned files '
+
\
'and does not delete them.'
),
)
def
handle
(
self
,
*
args
,
**
options
):
tmp_debug
=
settings
.
DEBUG
settings
.
DEBUG
=
False
names
=
set
()
dryrun
=
options
[
'dryrun'
]
try
:
for
model
in
get_models
():
for
field
in
model
.
_meta
.
fields
:
...
...
@@ -33,8 +43,14 @@ class Command(BaseCommand):
names
.
add
(
file
.
name
)
# Find all database files with names not in our list.
orphan_files
=
File
.
objects
.
exclude
(
name__in
=
names
)
total_bytes
=
0
for
f
in
orphan_files
:
print
'Deleting %s...'
%
(
f
.
name
,)
default_storage
.
delete
(
f
.
name
)
total_bytes
+=
f
.
size
if
dryrun
:
print
'File %s is orphaned.'
%
(
f
.
name
,)
else
:
print
'Deleting orphan file %s...'
%
(
f
.
name
,)
default_storage
.
delete
(
f
.
name
)
print
'%i total bytes in orphan files.'
%
total_bytes
finally
:
settings
.
DEBUG
=
tmp_debug
database_files/storage.py
View file @
090ade50
...
...
@@ -58,6 +58,7 @@ class DatabaseStorage(FileSystemStorage):
size
=
content
.
size
except
AttributeError
:
size
=
os
.
path
.
getsize
(
full_path
)
content
.
seek
(
0
)
content
=
content
.
read
()
f
=
models
.
File
.
objects
.
create
(
content
=
content
,
...
...
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