X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/357027375ff8867f42ca34bcbfb5a78b5b185fc3..d32d20846a6b20dc6059dfde22515071e48f9ec0:/src/search/management/commands/reindex.py diff --git a/src/search/management/commands/reindex.py b/src/search/management/commands/reindex.py old mode 100755 new mode 100644 index 4941354ce..c2fe78e94 --- a/src/search/management/commands/reindex.py +++ b/src/search/management/commands/reindex.py @@ -1,11 +1,11 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # import sys +import traceback + from django.core.management.base import BaseCommand -from optparse import make_option def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. @@ -17,9 +17,9 @@ def query_yes_no(question, default="yes"): The "answer" return value is one of "yes" or "no". """ - valid = {"yes":True, "y":True, "ye":True, - "no":False, "n":False} - if default == None: + valid = {"yes": True, "y": True, "ye": True, + "no": False, "n": False} + if default is None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " @@ -36,44 +36,61 @@ def query_yes_no(question, default="yes"): elif choice in valid: return valid[choice] else: - sys.stdout.write("Please respond with 'yes' or 'no' "\ - "(or 'y' or 'n').\n") + sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n") + class Command(BaseCommand): help = 'Reindex everything.' - args = '' - - option_list = BaseCommand.option_list + ( - make_option('-n', '--book-id', action='store_true', dest='book_id', default=False, - help='book id instead of slugs'), - make_option('-t', '--just-tags', action='store_true', dest='just_tags', default=False, - help='just reindex tags'), - ) - def handle(self, *args, **opts): + + def add_arguments(self, parser): + parser.add_argument( + '-n', '--book-id', action='store_true', dest='book_id', + default=False, help='book id instead of slugs') + parser.add_argument( + '-t', '--just-tags', action='store_true', dest='just_tags', + default=False, help='just reindex tags') + parser.add_argument( + '--start', dest='start_from', default=None, + help='start from this slug') + parser.add_argument( + '--stop', dest='stop_after', default=None, + help='stop after this slug') + parser.add_argument('args', nargs='*', metavar='slug/id') + + def handle(self, **opts): from catalogue.models import Book from search.index import Index idx = Index() if not opts['just_tags']: - if args: + if opts['args']: books = [] - for a in args: + for a in opts['args']: if opts['book_id']: books += Book.objects.filter(id=int(a)).all() else: books += Book.objects.filter(slug=a).all() else: - books = list(Book.objects.all()) - + books = list(Book.objects.order_by('slug')) + start_from = opts.get('start_from') + stop_after = opts.get('stop_after') + if start_from: + start_from = start_from.replace('-', '') + if stop_after: + stop_after = stop_after.replace('-', '') while books: try: b = books[0] - print b.title - idx.index_book(b) - idx.index.commit() + slug = b.slug.replace('-', '') + if stop_after and slug > stop_after: + break + if not start_from or slug >= start_from: + print(b.slug) + idx.index_book(b) + idx.index.commit() books.pop(0) - except Exception, e: - print "Error occured: %s" % e + except: + traceback.print_exc() try: # we might not be able to rollback idx.index.rollback() @@ -83,6 +100,6 @@ class Command(BaseCommand): if not retry: break - print 'Reindexing tags.' + print('Reindexing tags.') idx.index_tags() idx.index.commit()