Django 1.7, working version.
[wolnelektury.git] / apps / search / management / commands / reindex.py
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.
4 #
5 import sys
6 from django.core.management.base import BaseCommand
7
8 from optparse import make_option
9
10 def query_yes_no(question, default="yes"):
11     """Ask a yes/no question via raw_input() and return their answer.
12
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).
17
18     The "answer" return value is one of "yes" or "no".
19     """
20     valid = {"yes":True,   "y":True,  "ye":True,
21              "no":False,     "n":False}
22     if default == None:
23         prompt = " [y/n] "
24     elif default == "yes":
25         prompt = " [Y/n] "
26     elif default == "no":
27         prompt = " [y/N] "
28     else:
29         raise ValueError("invalid default answer: '%s'" % default)
30
31     while True:
32         sys.stdout.write(question + prompt)
33         choice = raw_input().lower()
34         if default is not None and choice == '':
35             return valid[default]
36         elif choice in valid:
37             return valid[choice]
38         else:
39             sys.stdout.write("Please respond with 'yes' or 'no' "\
40                              "(or 'y' or 'n').\n")
41
42 class Command(BaseCommand):
43     help = 'Reindex everything.'
44     args = ''
45     
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'),
51     )
52     def handle(self, *args, **opts):
53         from catalogue.models import Book
54         from search.index import Index
55         idx = Index()
56         
57         if not opts['just_tags']:
58             if args:
59                 books = []
60                 for a in args:
61                     if opts['book_id']:
62                         books += Book.objects.filter(id=int(a)).all()
63                     else:
64                         books += Book.objects.filter(slug=a).all()
65             else:
66                 books = list(Book.objects.all())
67
68             while books:
69                 try:
70                     b = books[0]
71                     print b.title
72                     idx.index_book(b)
73                     idx.index.commit()
74                     books.pop(0)
75                 except Exception, e:
76                     print "Error occured: %s" % e
77                     try:
78                         # we might not be able to rollback
79                         idx.index.rollback()
80                     except:
81                         pass
82                     retry = query_yes_no("Retry?")
83                     if not retry:
84                         break
85
86         print 'Reindexing tags.'
87         idx.index_tags()
88         idx.index.commit()