2 from django.core.management.base import BaseCommand
4 from optparse import make_option
6 def query_yes_no(question, default="yes"):
7 """Ask a yes/no question via raw_input() and return their answer.
9 "question" is a string that is presented to the user.
10 "default" is the presumed answer if the user just hits <Enter>.
11 It must be "yes" (the default), "no" or None (meaning
12 an answer is required of the user).
14 The "answer" return value is one of "yes" or "no".
16 valid = {"yes":True, "y":True, "ye":True,
17 "no":False, "n":False}
20 elif default == "yes":
25 raise ValueError("invalid default answer: '%s'" % default)
28 sys.stdout.write(question + prompt)
29 choice = raw_input().lower()
30 if default is not None and choice == '':
35 sys.stdout.write("Please respond with 'yes' or 'no' "\
38 class Command(BaseCommand):
39 help = 'Reindex everything.'
42 option_list = BaseCommand.option_list + (
43 make_option('-n', '--book-id', action='store_true', dest='book_id', default=False,
44 help='book id instead of slugs'),
45 make_option('-t', '--just-tags', action='store_true', dest='just_tags', default=False,
46 help='just reindex tags'),
48 def handle(self, *args, **opts):
49 from catalogue.models import Book
53 if not opts['just_tags']:
58 books += Book.objects.filter(id=int(a)).all()
60 books += Book.objects.filter(slug=a).all()
62 books = list(Book.objects.all())
72 print "Error occured: %s" % e
74 # we might not be able to rollback
78 retry = query_yes_no("Retry?")
82 print 'Reindexing tags.'