add committing, too
[wolnelektury.git] / apps / search / management / commands / reindex.py
1 from django.core.management.base import BaseCommand
2
3 from optparse import make_option
4 class Command(BaseCommand):
5     help = 'Reindex everything.'
6     args = ''
7     
8     option_list = BaseCommand.option_list + (
9         make_option('-n', '--book-id', action='store_true', dest='book_id', default=False,
10             help='book id instead of slugs'),
11         make_option('-t', '--just-tags', action='store_true', dest='just_tags', default=False,
12             help='just reindex tags'),
13     )
14     def handle(self, *args, **opts):
15         from catalogue.models import Book
16         import search
17         idx = search.Index()
18         
19         if not opts['just_tags']:
20             if args:
21                 books = []
22                 for a in args:
23                     if opts['book_id']:
24                         books += Book.objects.filter(id=int(a)).all()
25                     else:
26                         books += Book.objects.filter(slug=a).all()
27             else:
28                 books = Book.objects.all()
29
30             try:
31                 for b in books:
32                     print b.title
33                     idx.index_book(b)
34                     idx.index.commit()
35             except:
36                 idx.index.rollback()
37         print 'Reindexing tags.'
38         idx.index_tags()
39         idx.index.commit()