1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.core.management.base import BaseCommand
10 def query_yes_no(question, default="yes"):
11 """Ask a yes/no question via raw_input() and return their answer.
13 "question" is a string that is presented to the user.
14 "default" is the presumed answer if the user just hits <Enter>.
15 It must be "yes" (the default), "no" or None (meaning
16 an answer is required of the user).
18 The "answer" return value is one of "yes" or "no".
20 valid = {"yes": True, "y": True, "ye": True,
21 "no": False, "n": False}
24 elif default == "yes":
29 raise ValueError("invalid default answer: '%s'" % default)
32 sys.stdout.write(question + prompt)
33 choice = raw_input().lower()
34 if default is not None and choice == '':
39 sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
42 class Command(BaseCommand):
43 help = 'Reindex everything.'
45 def add_arguments(self, parser):
47 '-n', '--book-id', action='store_true', dest='book_id',
48 default=False, help='book id instead of slugs')
50 '-t', '--just-tags', action='store_true', dest='just_tags',
51 default=False, help='just reindex tags')
53 '--start', dest='start_from', default=None,
54 help='start from this slug')
56 '--stop', dest='stop_after', default=None,
57 help='stop after this slug')
58 parser.add_argument('args', nargs='*', metavar='slug/id')
60 def handle(self, **opts):
61 from catalogue.models import Book
62 from search.index import Index
65 if not opts['just_tags']:
68 for a in opts['args']:
70 books += Book.objects.filter(id=int(a)).all()
72 books += Book.objects.filter(slug=a).all()
74 books = list(Book.objects.order_by('slug'))
75 start_from = opts.get('start_from')
76 stop_after = opts.get('stop_after')
78 start_from = start_from.replace('-', '')
80 stop_after = stop_after.replace('-', '')
84 slug = b.slug.replace('-', '')
85 if stop_after and slug > stop_after:
87 if not start_from or slug >= start_from:
95 # we might not be able to rollback
99 retry = query_yes_no("Retry?")
103 print('Reindexing tags.')