search index building on import
authorMarcin Koziej <marcin@rho.(none)>
Thu, 20 Oct 2011 12:30:04 +0000 (14:30 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Tue, 15 Nov 2011 13:24:20 +0000 (14:24 +0100)
apps/catalogue/management/commands/importbooks.py
apps/catalogue/models.py
wolnelektury/settings.py

index ecd3fcc..4ea0fd3 100644 (file)
@@ -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'),
             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)'),
     )
         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_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'):
                         files_imported += 1
 
                         if os.path.isfile(file_base + '.pdf'):
index d902996..5846f53 100644 (file)
@@ -31,6 +31,8 @@ from slughifi import slughifi
 from sortify import sortify
 from os import unlink
 
 from sortify import sortify
 from os import unlink
 
+import search
+
 TAG_CATEGORIES = (
     ('author', _('author')),
     ('epoch', _('epoch')),
 TAG_CATEGORIES = (
     ('author', _('author')),
     ('epoch', _('epoch')),
@@ -623,6 +625,10 @@ class Book(models.Model):
         result = create_zip.delay(paths, self.slug)
         return result.wait()
 
         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
     @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,
 
     @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
         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_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
         book_descendants = list(book.children.all())
         # add l-tag to descendants and their fragments
         # delete unnecessary EPUB files
index 527b702..757447e 100644 (file)
@@ -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')
 # 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).
 
 # 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',
     'sponsors',
     'stats',
     'suggest',
+    'search',
+
+    #
+    'django_nose',
 ]
 
 #CACHE_BACKEND = 'locmem:///?max_entries=3000'
 ]
 
 #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_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'
 
 ALL_EPUB_ZIP = 'wolnelektury_pl_epub'
 ALL_PDF_ZIP = 'wolnelektury_pl_pdf'
@@ -246,6 +252,7 @@ BROKER_PASSWORD = "guest"
 BROKER_VHOST = "/"
 
 
 BROKER_VHOST = "/"
 
 
+
 # Load localsettings, if they exist
 try:
     from localsettings import *
 # Load localsettings, if they exist
 try:
     from localsettings import *