From: Marcin Koziej Date: Thu, 20 Oct 2011 12:30:04 +0000 (+0200) Subject: search index building on import X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/4fea8503c1f680b7e6942dfae6f0e7a8589e47ea?ds=inline;hp=--cc search index building on import --- 4fea8503c1f680b7e6942dfae6f0e7a8589e47ea diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index ecd3fcc97..4ea0fd359 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -28,6 +28,8 @@ class Command(BaseCommand): help='Don\'t build TXT file'), make_option('-P', '--no-build-pdf', action='store_false', dest='build_pdf', default=True, help='Don\'t build PDF file'), + make_option('-S', '--no-search-index', action='store_false', dest='search_index', default=True, + help='Don\'t build PDF file'), make_option('-w', '--wait-until', dest='wait_until', metavar='TIME', help='Wait until specified time (Y-M-D h:m:s)'), ) @@ -87,7 +89,8 @@ class Command(BaseCommand): build_epub=options.get('build_epub'), build_txt=options.get('build_txt'), build_pdf=options.get('build_pdf'), - build_mobi=options.get('build_mobi')) + build_mobi=options.get('build_mobi'), + search_index=options.get('search_index')) files_imported += 1 if os.path.isfile(file_base + '.pdf'): diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index d90299611..5846f5343 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -31,6 +31,8 @@ from slughifi import slughifi from sortify import sortify from os import unlink +import search + TAG_CATEGORIES = ( ('author', _('author')), ('epoch', _('epoch')), @@ -623,6 +625,10 @@ class Book(models.Model): result = create_zip.delay(paths, self.slug) return result.wait() + def search_index(self): + with search.Index() as idx: + idx.index_book(self) + @classmethod def from_xml_file(cls, xml_file, **kwargs): # use librarian to parse meta-data @@ -638,7 +644,8 @@ class Book(models.Model): @classmethod def from_text_and_meta(cls, raw_file, book_info, overwrite=False, - build_epub=True, build_txt=True, build_pdf=True, build_mobi=True): + build_epub=True, build_txt=True, build_pdf=True, build_mobi=True, + search_index=True): import re # check for parts before we do anything @@ -717,6 +724,9 @@ class Book(models.Model): if not settings.NO_BUILD_MOBI and build_mobi: book.build_mobi() + if not settings.NO_SEARCH_INDEX and search_index: + book.search_index() + book_descendants = list(book.children.all()) # add l-tag to descendants and their fragments # delete unnecessary EPUB files diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 527b70277..757447e71 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -60,6 +60,7 @@ USE_I18N = True # Example: "/home/media/media.lawrence.com/" MEDIA_ROOT = path.join(PROJECT_DIR, '../media') STATIC_ROOT = path.join(PROJECT_DIR, 'static') +SEARCH_INDEX = path.join(MEDIA_ROOT, 'search') # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). @@ -150,6 +151,10 @@ INSTALLED_APPS = [ 'sponsors', 'stats', 'suggest', + 'search', + + # + 'django_nose', ] #CACHE_BACKEND = 'locmem:///?max_entries=3000' @@ -228,6 +233,7 @@ NO_BUILD_EPUB = False NO_BUILD_TXT = False NO_BUILD_PDF = False NO_BUILD_MOBI = False +NO_SEARCH_INDEX = False ALL_EPUB_ZIP = 'wolnelektury_pl_epub' ALL_PDF_ZIP = 'wolnelektury_pl_pdf' @@ -246,6 +252,7 @@ BROKER_PASSWORD = "guest" BROKER_VHOST = "/" + # Load localsettings, if they exist try: from localsettings import *