1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.core.management.base import BaseCommand
8 from optparse import make_option
11 def query_yes_no(question, default="yes"):
12 """Ask a yes/no question via raw_input() and return their answer.
14 "question" is a string that is presented to the user.
15 "default" is the presumed answer if the user just hits <Enter>.
16 It must be "yes" (the default), "no" or None (meaning
17 an answer is required of the user).
19 The "answer" return value is one of "yes" or "no".
21 valid = {"yes": True, "y": True, "ye": True,
22 "no": False, "n": False}
25 elif default == "yes":
30 raise ValueError("invalid default answer: '%s'" % default)
33 sys.stdout.write(question + prompt)
34 choice = raw_input().lower()
35 if default is not None and choice == '':
40 sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
43 class Command(BaseCommand):
44 help = 'Reindex everything.'
47 option_list = BaseCommand.option_list + (
48 make_option('-n', '--book-id', action='store_true', dest='book_id', default=False,
49 help='book id instead of slugs'),
50 make_option('-t', '--just-tags', action='store_true', dest='just_tags', default=False,
51 help='just reindex tags'),
54 def handle(self, *args, **opts):
55 from catalogue.models import Book
56 from search.index import Index
59 if not opts['just_tags']:
64 books += Book.objects.filter(id=int(a)).all()
66 books += Book.objects.filter(slug=a).all()
68 books = list(Book.objects.all())
78 print "Error occured: %s" % e
80 # we might not be able to rollback
84 retry = query_yes_no("Retry?")
88 print 'Reindexing tags.'