forked from django-extensions/django-extensions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This command lists every file under MEDIA_ROOT that isn't referenced in the database in a FileField or subclass of FileField.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
django_extensions/management/commands/unreferenced_files.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from collections import defaultdict | ||
import os | ||
from django.conf import settings | ||
from django.core.management.base import NoArgsCommand | ||
from django.db import models | ||
from django.db.models.loading import cache | ||
|
||
class Command(NoArgsCommand): | ||
help = "Prints a list of all files in MEDIA_ROOT that are not referenced in the database." | ||
|
||
def handle_noargs(self, **options): | ||
|
||
if settings.MEDIA_ROOT == '': | ||
print "MEDIA_ROOT is not set, nothing to do" | ||
return | ||
|
||
# Get a list of all files under MEDIA_ROOT | ||
media = [] | ||
for root, dirs, files in os.walk(settings.MEDIA_ROOT): | ||
for f in files: | ||
media.append(os.path.abspath(os.path.join(root, f))) | ||
|
||
# Get list of all fields (value) for each model (key) | ||
# that is a FileField or subclass of a FileField | ||
model_dict = defaultdict(list) | ||
for app in cache.get_apps(): | ||
model_list = cache.get_models(app) | ||
for model in model_list: | ||
for field in model._meta.fields: | ||
if issubclass(field.__class__, models.FileField): | ||
model_dict[model].append(field) | ||
|
||
# Get a list of all files referenced in the database | ||
referenced = [] | ||
for model in model_dict.iterkeys(): | ||
all = model.objects.all().iterator() | ||
for object in all: | ||
for field in model_dict[model]: | ||
referenced.append(os.path.abspath(getattr(object, field.name).path)) | ||
|
||
# Print each file in MEDIA_ROOT that is not referenced in the database | ||
for m in media: | ||
if m not in referenced: | ||
print m |