importbooks: no-build-epub and wait-until options
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 31 Dec 2010 09:37:45 +0000 (10:37 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 31 Dec 2010 11:53:05 +0000 (12:53 +0100)
epub generation fix
search fix
blog feed fix

apps/catalogue/management/commands/importbooks.py
apps/catalogue/models.py
apps/catalogue/views.py
wolnelektury/templates/catalogue/main_page.html

index be65bd9..3daca8a 100644 (file)
@@ -4,6 +4,7 @@
 #
 import os
 import sys
+import time
 from optparse import make_option
 
 from django.core.management.base import BaseCommand
@@ -18,7 +19,11 @@ class Command(BaseCommand):
         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
         make_option('-f', '--force', action='store_true', dest='force', default=False,
-            help='Print status messages to stdout')
+            help='Print status messages to stdout'),
+        make_option('-E', '--no-build-epub', action='store_false', dest='build_epub', default=True,
+            help='Don\'t build EPUB file'),
+        make_option('-w', '--wait-until', dest='wait_until', metavar='TIME',
+            help='Wait until specified time (Y-M-D h:m:s)'),
     )
     help = 'Imports books from the specified directories.'
     args = 'directory [directory ...]'
@@ -32,6 +37,13 @@ class Command(BaseCommand):
         force = options.get('force')
         show_traceback = options.get('traceback', False)
 
+        if options.get('wait_until'):
+            wait_until = time.mktime(time.strptime(options.get('wait_until'), '%Y-%m-%d %H:%M:%S'))
+            if verbose > 0:
+                print "Will wait until %s; it's %f seconds from now" % (
+                    time.strftime('%Y-%m-%d %H:%M:%S', 
+                    time.localtime(wait_until)), wait_until - time.time())
+
         # Start transaction management.
         transaction.commit_unless_managed()
         transaction.enter_transaction_management()
@@ -64,7 +76,7 @@ class Command(BaseCommand):
 
                     # Import book files
                     try:
-                        book = Book.from_xml_file(file_path, overwrite=force)
+                        book = Book.from_xml_file(file_path, overwrite=force, build_epub=options.get('build_epub'))
                         files_imported += 1
 
                         if os.path.isfile(file_base + '.pdf'):
@@ -120,6 +132,13 @@ class Command(BaseCommand):
             files_imported, files_skipped, files_imported + files_skipped)
         print
 
+        if wait_until:
+            print 'Waiting...'
+            try:
+                time.sleep(wait_until - time.time())
+            except IOError:
+                print "it's already too late"
+
         transaction.commit()
         transaction.leave_transaction_management()
 
index 1d0b83b..0091d8a 100644 (file)
@@ -442,7 +442,7 @@ class Book(models.Model):
 
         epub_file = StringIO()
         try:
-            epub.transform(BookImportDocProvider(self), self.slug, epub_file)
+            epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file)
             self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()), save=False)
             self.save(refresh_mp3=False)
             FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
@@ -460,7 +460,7 @@ class Book(models.Model):
 
 
     @classmethod
-    def from_xml_file(cls, xml_file, overwrite=False):
+    def from_xml_file(cls, xml_file, overwrite=False, build_epub=True):
         # use librarian to parse meta-data
         book_info = dcparser.parse(xml_file)
 
@@ -468,12 +468,12 @@ class Book(models.Model):
             xml_file = File(open(xml_file))
 
         try:
-            return cls.from_text_and_meta(xml_file, book_info, overwrite)
+            return cls.from_text_and_meta(xml_file, book_info, overwrite, build_epub=build_epub)
         finally:
             xml_file.close()
 
     @classmethod
-    def from_text_and_meta(cls, raw_file, book_info, overwrite=False):
+    def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True):
         from tempfile import NamedTemporaryFile
         from slughifi import slughifi
         from markupstring import MarkupString
@@ -582,7 +582,7 @@ class Book(models.Model):
                 new_fragment.save()
                 new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags)
 
-        if not settings.NO_BUILD_EPUB:
+        if not settings.NO_BUILD_EPUB and build_epub:
             book.root_ancestor().build_epub()
 
         book_descendants = list(book.children.all())
index 82ca9fd..124cd93 100644 (file)
@@ -433,7 +433,7 @@ def find_best_matches(query, user=None):
                       if isinstance(match, models.Book))
     authors = set(match.name.lower() for match in result
                   if isinstance(match, models.Tag) and match.category=='author')
-    result = (res for res in result if not (
+    result = tuple(res for res in result if not (
                  (isinstance(res, pdcounter_models.BookStub) and res.pretty_title().lower() in book_titles)
                  or (isinstance(res, pdcounter_models.Author) and res.name.lower() in authors)
              ))
index bda0bc1..d86085a 100644 (file)
         <div id="latest-blog-posts">
             <h2>{% trans "News" %}</h2>
             {% cache 1800 latest-blog-posts %}
-            {% latest_blog_posts "http://www.nowoczesnapolska.org.pl/tematy/wolne-lektury/feed/" %}
+            {% latest_blog_posts "http://nowoczesnapolska.org.pl/category/wolne-lektury/feed/" %}
             {% endcache %}
             <p class="see-more"><a href="http://www.nowoczesnapolska.org.pl/">{% trans "See our blog" %} ⇒</a></p>
         </div>