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 23:06:29 +0000 (00:06 +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 ffe0de8..467dc14 100644 (file)
@@ -4,6 +4,7 @@
 #
 import os
 import sys
 #
 import os
 import sys
+import time
 from optparse import make_option
 
 from django.core.management.base import BaseCommand
 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,
         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 ...]'
     )
     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)
 
         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()
         # Start transaction management.
         transaction.commit_unless_managed()
         transaction.enter_transaction_management()
@@ -60,7 +72,7 @@ class Command(BaseCommand):
 
                     # Import book files
                     try:
 
                     # 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'):
                         files_imported += 1
 
                         if os.path.isfile(file_base + '.pdf'):
@@ -105,6 +117,13 @@ class Command(BaseCommand):
             files_imported, files_skipped, files_imported + files_skipped)
         print
 
             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()
 
         transaction.commit()
         transaction.leave_transaction_management()
 
index f201dc2..5f7eb17 100644 (file)
@@ -345,7 +345,7 @@ class Book(models.Model):
 
         epub_file = StringIO()
         try:
 
         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()
             FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
             self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()), save=False)
             self.save()
             FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
@@ -363,7 +363,7 @@ class Book(models.Model):
 
 
     @classmethod
 
 
     @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)
 
         # use librarian to parse meta-data
         book_info = dcparser.parse(xml_file)
 
@@ -371,12 +371,12 @@ class Book(models.Model):
             xml_file = File(open(xml_file))
 
         try:
             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
         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
         from tempfile import NamedTemporaryFile
         from slughifi import slughifi
         from markupstring import MarkupString
@@ -480,7 +480,8 @@ class Book(models.Model):
                 new_fragment.save()
                 new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags)
 
                 new_fragment.save()
                 new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags)
 
-        if not book.parent:
+        if build_epub and not book.parent:
+            print 'epub'
             book.build_epub(remove_descendants=False)
 
         book_descendants = list(book.children.all())
             book.build_epub(remove_descendants=False)
 
         book_descendants = list(book.children.all())
index 7bb82fd..29bf805 100644 (file)
@@ -383,7 +383,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')
                       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)
              ))
                  (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 2c798b1..c18f890 100644 (file)
         <div id="latest-blog-posts">
             <h2>{% trans "News" %}</h2>
             {% cache 1800 latest-blog-posts %}
         <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>
             {% endcache %}
             <p class="see-more"><a href="http://www.nowoczesnapolska.org.pl/">{% trans "See our blog" %} ⇒</a></p>
         </div>