Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented maintenance actions as management commands
- Loading branch information
1 parent
245c0d4
commit fd55bd5
Showing
8 changed files
with
162 additions
and
44 deletions.
There are no files selected for viewing
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
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,30 @@ | ||
Management Commands | ||
******************* | ||
|
||
You can use the following management commands to assist you with media file | ||
management. | ||
|
||
|
||
Orphaned files | ||
============== | ||
|
||
Use the following command to list all orphaned files, i.e. media files existing | ||
in storage that are not in the database:: | ||
|
||
manage.py mediaorphaned | ||
|
||
Use the following command to **delete** all orphaned files:: | ||
|
||
manage.py mediaorphaned --delete | ||
|
||
|
||
Media cache | ||
=========== | ||
|
||
Use the following command to list all media cache files, such as thumbnails:: | ||
|
||
manage.py mediacache | ||
|
||
Use the following command to **delete** all media cache files:: | ||
|
||
manage.py mediacache --delete |
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
Empty file.
Empty file.
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,26 @@ | ||
from media_tree.utils.maintenance import get_cache_files | ||
from media_tree.utils import get_media_storage | ||
from django.core.management.base import BaseCommand, CommandError | ||
from optparse import make_option | ||
|
||
class Command(BaseCommand): | ||
|
||
help = 'Lists (and optionally deletes) all media_tree cache files.' | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option('--delete', | ||
action='store_true', | ||
dest='delete', | ||
default=False, | ||
help='Delete all cache files'), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
cache_files = get_cache_files() | ||
storage = get_media_storage() | ||
for path in cache_files: | ||
if options['delete']: | ||
storage.delete(path) | ||
self.stdout.write("Deleted %s\n" % storage.path(path)) | ||
else: | ||
self.stdout.write("%s\n" % storage.path(path)) |
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,27 @@ | ||
from media_tree.utils.maintenance import get_orphaned_files | ||
from media_tree.utils import get_media_storage | ||
from django.core.management.base import BaseCommand, CommandError | ||
from optparse import make_option | ||
|
||
class Command(BaseCommand): | ||
|
||
help = 'Lists (and optionally deletes) all media_tree orphaned files, ' \ | ||
+ 'i.e. media files existing in storage that are not in the database.' | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option('--delete', | ||
action='store_true', | ||
dest='delete', | ||
default=False, | ||
help='Delete all orphaned files from storage'), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
orphaned_files = get_orphaned_files() | ||
storage = get_media_storage() | ||
for path in orphaned_files: | ||
if options['delete']: | ||
storage.delete(path) | ||
self.stdout.write("Deleted %s\n" % storage.path(path)) | ||
else: | ||
self.stdout.write("%s\n" % storage.path(path)) |
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,55 @@ | ||
from media_tree.utils import get_media_storage | ||
from media_tree.media_backends import get_media_backend | ||
from media_tree.models import FileNode | ||
from media_tree import settings as app_settings | ||
from unicodedata import normalize | ||
import os | ||
|
||
|
||
def get_cache_files(): | ||
storage = get_media_storage() | ||
cache_files = [] | ||
|
||
for cache_dir in get_media_backend().get_cache_paths(): | ||
if storage.exists(cache_dir): | ||
files_in_dir = [storage.path(os.path.join(cache_dir, filename)) \ | ||
for filename in storage.listdir(cache_dir)[1]] | ||
for file_path in files_in_dir: | ||
# need to normalize unicode path due to https://code.djangoproject.com/ticket/16315 | ||
file_path = normalize('NFC', file_path) | ||
storage_name = os.path.join(cache_dir, os.path.basename(file_path)) | ||
cache_files.append(storage_name) | ||
|
||
return cache_files | ||
|
||
|
||
def get_broken_media(): | ||
storage = get_media_storage() | ||
media_subdir = app_settings.MEDIA_TREE_UPLOAD_SUBDIR | ||
broken_nodes = [] | ||
orphaned_files = [] | ||
|
||
files_in_db = [] | ||
for node in FileNode.objects.filter(node_type=FileNode.FILE): | ||
path = node.file.path | ||
# need to normalize unicode path due to https://code.djangoproject.com/ticket/16315 | ||
path = normalize('NFC', path) | ||
files_in_db.append(path) | ||
if not storage.exists(node.file): | ||
broken_nodes.append(node) | ||
|
||
files_in_storage = [storage.path(os.path.join(media_subdir, filename)) \ | ||
for filename in storage.listdir(media_subdir)[1]] | ||
|
||
for file_path in files_in_storage: | ||
# need to normalize unicode path due to https://code.djangoproject.com/ticket/16315 | ||
file_path = normalize('NFC', file_path) | ||
if not file_path in files_in_db: | ||
storage_name = os.path.join(media_subdir, os.path.basename(file_path)) | ||
orphaned_files.append(storage_name) | ||
|
||
return [broken_nodes, orphaned_files] | ||
|
||
|
||
def get_orphaned_files(): | ||
return get_broken_media()[1] |