From 9ec75ce00163d9f536b71f786bc89c4117bd7fa0 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 31 Dec 2010 10:37:45 +0100 Subject: [PATCH] importbooks: no-build-epub and wait-until options epub generation fix search fix blog feed fix --- .../management/commands/importbooks.py | 23 +++++++++++++++++-- apps/catalogue/models.py | 10 ++++---- apps/catalogue/views.py | 2 +- .../templates/catalogue/main_page.html | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index be65bd9a1..3daca8ac6 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -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() diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 1d0b83b65..0091d8a27 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -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()) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 82ca9fde0..124cd9313 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -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) )) diff --git a/wolnelektury/templates/catalogue/main_page.html b/wolnelektury/templates/catalogue/main_page.html index bda0bc143..d86085ad4 100644 --- a/wolnelektury/templates/catalogue/main_page.html +++ b/wolnelektury/templates/catalogue/main_page.html @@ -286,7 +286,7 @@

{% trans "News" %}

{% 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 %}

{% trans "See our blog" %} ⇒

-- 2.20.1