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.
8 from django.core.management.base import BaseCommand
10 from optparse import make_option
13 def query_yes_no(question, default="yes"):
14 """Ask a yes/no question via raw_input() and return their answer.
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).
21 The "answer" return value is one of "yes" or "no".
23 valid = {"yes": True, "y": True, "ye": True,
24 "no": False, "n": False}
27 elif default == "yes":
32 raise ValueError("invalid default answer: '%s'" % default)
35 sys.stdout.write(question + prompt)
36 choice = raw_input().lower()
37 if default is not None and choice == '':
42 sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
45 class Command(BaseCommand):
46 help = 'Reindex pictures.'
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'),
54 def handle(self, *args, **opts):
55 from picture.models import Picture
56 from search.index import Index
62 if opts['picture_id']:
63 pictures += Picture.objects.filter(id=int(a)).all()
65 pictures += Picture.objects.filter(slug=a).all()
67 pictures = list(Picture.objects.order_by('slug'))
78 # we might not be able to rollback
82 retry = query_yes_no("Retry?")