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
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' "\
42 class Command(BaseCommand):
43 help = 'Reindex everything.'
46 option_list = BaseCommand.option_list + (
47 make_option('-n', '--book-id', action='store_true', dest='book_id', default=False,
48 help='book id instead of slugs'),
49 make_option('-t', '--just-tags', action='store_true', dest='just_tags', default=False,
50 help='just reindex tags'),
52 def handle(self, *args, **opts):
53 from catalogue.models import Book
54 from search.index import Index
57 if not opts['just_tags']:
62 books += Book.objects.filter(id=int(a)).all()
64 books += Book.objects.filter(slug=a).all()
66 books = list(Book.objects.all())
76 print "Error occured: %s" % e
78 # we might not be able to rollback
82 retry = query_yes_no("Retry?")
86 print 'Reindexing tags.'