1
|
|
|
from optparse import make_option |
2
|
|
|
from django.conf import settings |
3
|
|
|
from django.core.management.base import BaseCommand |
4
|
|
|
from django.apps import apps |
5
|
|
|
from ...api import invalidate |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Command(BaseCommand): |
9
|
|
|
help = 'Invalidates the cache keys set by django-cachalot.' |
10
|
|
|
args = '[app_label[.modelname] [...]]' |
11
|
|
|
option_list = BaseCommand.option_list + ( |
12
|
|
|
make_option('-c', '--cache', action='store', dest='cache_alias', |
13
|
|
|
type='choice', choices=list(settings.CACHES.keys()), |
14
|
|
|
help='Cache alias from the CACHES setting.'), |
15
|
|
|
make_option('-d', '--db', action='store', dest='db_alias', |
16
|
|
|
type='choice', choices=list(settings.DATABASES.keys()), |
17
|
|
|
help='Database alias from the DATABASES setting.'), |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
def handle(self, *args, **options): |
21
|
|
|
cache_alias = options['cache_alias'] |
22
|
|
|
db_alias = options['db_alias'] |
23
|
|
|
verbosity = int(options['verbosity']) |
24
|
|
|
|
25
|
|
|
models = [] |
26
|
|
|
for arg in args: |
27
|
|
|
try: |
28
|
|
|
models.extend(apps.get_app_config(arg).get_models()) |
29
|
|
|
except LookupError: |
30
|
|
|
app_label = '.'.join(arg.split('.')[:-1]) |
31
|
|
|
model_name = arg.split('.')[-1] |
32
|
|
|
models.append(apps.get_model(app_label, model_name)) |
33
|
|
|
|
34
|
|
|
cache_str = '' if cache_alias is None else "on cache '%s'" % cache_alias |
35
|
|
|
db_str = '' if db_alias is None else "for database '%s'" % db_alias |
36
|
|
|
keys_str = 'keys for %s models' % len(models) if args else 'all keys' |
37
|
|
|
|
38
|
|
|
if verbosity > 0: |
39
|
|
|
self.stdout.write(' '.join(filter(bool, ['Invalidating', keys_str, |
40
|
|
|
cache_str, db_str])) |
41
|
|
|
+ '...') |
42
|
|
|
|
43
|
|
|
invalidate(*models, cache_alias=cache_alias, db_alias=db_alias) |
44
|
|
|
if verbosity > 0: |
45
|
|
|
self.stdout.write('Cache keys successfully invalidated.') |
46
|
|
|
|