Skip to content

Commit

Permalink
Modernizes the manage.py command.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Jul 21, 2016
1 parent 43b1fa3 commit 72596ac
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions cachalot/management/commands/invalidate_cachalot.py
@@ -1,39 +1,42 @@
from optparse import make_option
from django.conf import settings
from django.core.management.base import BaseCommand
from django.apps import apps

from ...api import invalidate


class Command(BaseCommand):
help = 'Invalidates the cache keys set by django-cachalot.'
args = '[app_label[.modelname] [...]]'
option_list = BaseCommand.option_list + (
make_option('-c', '--cache', action='store', dest='cache_alias',
type='choice', choices=list(settings.CACHES.keys()),
help='Cache alias from the CACHES setting.'),
make_option('-d', '--db', action='store', dest='db_alias',
type='choice', choices=list(settings.DATABASES.keys()),
help='Database alias from the DATABASES setting.'),
)

def add_arguments(self, parser):
parser.add_argument('app_label[.model_name]', nargs='*')
parser.add_argument(
'-c', '--cache', action='store', dest='cache_alias',
choices=list(settings.CACHES.keys()),
help='Cache alias from the CACHES setting.')
parser.add_argument(
'-d', '--db', action='store', dest='db_alias',
choices=list(settings.DATABASES.keys()),
help='Database alias from the DATABASES setting.')

def handle(self, *args, **options):
cache_alias = options['cache_alias']
db_alias = options['db_alias']
verbosity = int(options['verbosity'])
labels = options['app_label[.model_name]']

models = []
for arg in args:
for label in labels:
try:
models.extend(apps.get_app_config(arg).get_models())
models.extend(apps.get_app_config(label).get_models())
except LookupError:
app_label = '.'.join(arg.split('.')[:-1])
model_name = arg.split('.')[-1]
app_label = '.'.join(label.split('.')[:-1])
model_name = label.split('.')[-1]
models.append(apps.get_model(app_label, model_name))

cache_str = '' if cache_alias is None else "on cache '%s'" % cache_alias
db_str = '' if db_alias is None else "for database '%s'" % db_alias
keys_str = 'keys for %s models' % len(models) if args else 'all keys'
keys_str = 'keys for %s models' % len(models) if labels else 'all keys'

if verbosity > 0:
self.stdout.write(' '.join(filter(bool, ['Invalidating', keys_str,
Expand Down

0 comments on commit 72596ac

Please sign in to comment.