Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase thumbnail management command on BaseCommand #686

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 24 additions & 53 deletions sorl/thumbnail/management/commands/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import sys

from argparse import RawTextHelpFormatter
from django.core.management.base import LabelCommand, CommandError
from django.core.management.base import BaseCommand

from sorl.thumbnail import default
from sorl.thumbnail.images import delete_all_thumbnails
Expand All @@ -10,87 +7,61 @@
VALID_LABELS = ['cleanup', 'clear', 'clear_delete_referenced', 'clear_delete_all']


class Command(LabelCommand):
help = (
"Handles thumbnails and key-value store. Usage:\n"
"\n"
" manage.py thumbnail <label>\n"
"\n"
"Valid <label>s are:\n"
"\n"
" {}\n"
"\n"
"Documentation: https://sorl-thumbnail.readthedocs.io/en/latest/management.html"
).format(", ".join(VALID_LABELS))
missing_args_message = "Enter a valid label: {}".format(
class Command(BaseCommand):
help = "Handles thumbnails and key-value store"
missing_args_message = "Enter a valid operation: {}".format(
", ".join(VALID_LABELS)
)

def add_arguments(self, parser):
parser.add_argument('args', choices=VALID_LABELS, nargs=1)

def handle(self, *labels, **options):
verbosity = int(options.get('verbosity'))

# Django 1.4 compatibility fix
stdout = options.get('stdout', None)
stdout = stdout if stdout else sys.stdout

stderr = options.get('stderr', None)
stderr = stderr if stderr else sys.stderr

if not labels:
print(self.print_help('thumbnail', ''), file=stderr)
sys.exit(1)

if len(labels) != 1:
raise CommandError('`%s` is not a valid argument' % labels)

label = labels[0]

if label not in VALID_LABELS:
raise CommandError('`%s` unknown action' % label)

if label == 'cleanup':
if verbosity >= 1:
print("Cleanup thumbnails", end=' ... ', file=stdout)
self.stdout.write("Cleanup thumbnails", ending=' ... ')

default.kvstore.cleanup()

if verbosity >= 1:
print("[Done]", file=stdout)
self.stdout.write("[Done]")

return

if label == 'clear_delete_referenced':
if verbosity >= 1:
print("Delete all thumbnail files referenced in "
"Key Value Store", end=' ... ', file=stdout)
self.stdout.write(
"Delete all thumbnail files referenced in Key Value Store",
ending=' ... '
)

default.kvstore.delete_all_thumbnail_files()

if verbosity >= 1:
print('[Done]', file=stdout)
self.stdout.write('[Done]')

if verbosity >= 1:
print("Clear the Key Value Store", end=' ... ', file=stdout)
self.stdout.write("Clear the Key Value Store", ending=' ... ')

default.kvstore.clear()

if verbosity >= 1:
print('[Done]', file=stdout)
self.stdout.write('[Done]')

if label == 'clear_delete_all':
if verbosity >= 1:
print("Delete all thumbnail files in THUMBNAIL_PREFIX", end=' ... ', file=stdout)
self.stdout.write("Delete all thumbnail files in THUMBNAIL_PREFIX", ending=' ... ')

delete_all_thumbnails()

if verbosity >= 1:
print('[Done]', file=stdout)

def create_parser(self, *args, **kwargs):
"""
Necessary for multi-line help-text.
Source: https://stackoverflow.com/a/35470682/405682
"""
parser = super(Command, self).create_parser(*args, **kwargs)
parser.formatter_class = RawTextHelpFormatter
return parser
self.stdout.write('[Done]')

def create_parser(self, prog_name, subcommand, **kwargs):
kwargs['epilog'] = (
"Documentation: https://sorl-thumbnail.readthedocs.io/en/latest/management.html"
)
return super().create_parser(prog_name, subcommand, **kwargs)