add pictures in search
[wolnelektury.git] / src / search / management / commands / reindex_pictures.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 import traceback
7
8 from django.core.management.base import BaseCommand
9
10 from optparse import make_option
11
12
13 def query_yes_no(question, default="yes"):
14     """Ask a yes/no question via raw_input() and return their answer.
15
16     "question" is a string that is presented to the user.
17     "default" is the presumed answer if the user just hits <Enter>.
18         It must be "yes" (the default), "no" or None (meaning
19         an answer is required of the user).
20
21     The "answer" return value is one of "yes" or "no".
22     """
23     valid = {"yes": True, "y": True, "ye": True,
24              "no": False, "n": False}
25     if default is None:
26         prompt = " [y/n] "
27     elif default == "yes":
28         prompt = " [Y/n] "
29     elif default == "no":
30         prompt = " [y/N] "
31     else:
32         raise ValueError("invalid default answer: '%s'" % default)
33
34     while True:
35         sys.stdout.write(question + prompt)
36         choice = raw_input().lower()
37         if default is not None and choice == '':
38             return valid[default]
39         elif choice in valid:
40             return valid[choice]
41         else:
42             sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
43
44
45 class Command(BaseCommand):
46     help = 'Reindex pictures.'
47     args = ''
48
49     option_list = BaseCommand.option_list + (
50         make_option('-n', '--picture-id', action='store_true', dest='picture_id', default=False,
51                     help='picture id instead of slugs'),
52     )
53
54     def handle(self, *args, **opts):
55         from picture.models import Picture
56         from search.index import Index
57         idx = Index()
58
59         if args:
60             pictures = []
61             for a in args:
62                 if opts['picture_id']:
63                     pictures += Picture.objects.filter(id=int(a)).all()
64                 else:
65                     pictures += Picture.objects.filter(slug=a).all()
66         else:
67             pictures = list(Picture.objects.order_by('slug'))
68         while pictures:
69             try:
70                 p = pictures[0]
71                 print p.slug
72                 idx.index_picture(p)
73                 idx.index.commit()
74                 pictures.pop(0)
75             except:
76                 traceback.print_exc()
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