From: Radek Czajka Date: Fri, 31 Dec 2010 09:37:45 +0000 (+0100) Subject: importbooks: no-build-epub and wait-until options X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/f258a9896bbeff2a900d8bafb2aecfce03f19bd8?ds=inline;hp=-c importbooks: no-build-epub and wait-until options epub generation fix search fix blog feed fix --- f258a9896bbeff2a900d8bafb2aecfce03f19bd8 diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index ffe0de8ec..467dc14f5 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() @@ -60,7 +72,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'): @@ -105,6 +117,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 f201dc237..5f7eb1712 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -345,7 +345,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() FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save() @@ -363,7 +363,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) @@ -371,12 +371,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 @@ -480,7 +480,8 @@ class Book(models.Model): 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()) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 7bb82fd4a..29bf80557 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -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') - 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 2c798b116..c18f890a2 100644 --- a/wolnelektury/templates/catalogue/main_page.html +++ b/wolnelektury/templates/catalogue/main_page.html @@ -274,7 +274,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" %} ⇒