From d54976be7b44bd0c8c3c2afac8f11b2408e6cb14 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 4 Sep 2014 15:14:42 +0200 Subject: [PATCH 1/1] Always attempt to build HTML and fix_tree_tags when publishing a book. Also, some test fixes. --- apps/api/tests.py | 1 + apps/catalogue/fields.py | 18 ++++++++++-------- apps/catalogue/models/book.py | 2 +- apps/opds/tests/__init__.py | 13 ++++++++----- apps/picture/tests/picture_import.py | 17 ++++++++++++----- apps/search/tests/index.py | 12 +++++++----- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/apps/api/tests.py b/apps/api/tests.py index 0a638dc3f..87c4f75d8 100644 --- a/apps/api/tests.py +++ b/apps/api/tests.py @@ -17,6 +17,7 @@ import picture.tests @override_settings( API_WAIT=-1, + NO_SEARCH_INDEX = True, CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}, 'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}, 'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}} diff --git a/apps/catalogue/fields.py b/apps/catalogue/fields.py index ebf71c222..89b76ee32 100644 --- a/apps/catalogue/fields.py +++ b/apps/catalogue/fields.py @@ -134,22 +134,23 @@ class BuildHtml(BuildEbook): book = fieldfile.instance - meta_tags = list(book.tags.filter( - category__in=('author', 'epoch', 'genre', 'kind'))) - book_tag = book.book_tag() - html_output = self.transform( book.wldocument(parse_dublincore=False), fieldfile) - lang = book.language - lang = LANGUAGES_3TO2.get(lang, lang) - if lang not in [ln[0] for ln in settings.LANGUAGES]: - lang = None # Delete old fragments, create from scratch if necessary. book.fragments.all().delete() if html_output: + meta_tags = list(book.tags.filter( + category__in=('author', 'epoch', 'genre', 'kind'))) + book_tag = book.book_tag() + + lang = book.language + lang = LANGUAGES_3TO2.get(lang, lang) + if lang not in [ln[0] for ln in settings.LANGUAGES]: + lang = None + fieldfile.save(None, ContentFile(html_output.get_string()), save=False) type(book).objects.filter(pk=book.pk).update(**{ @@ -209,6 +210,7 @@ class BuildHtml(BuildEbook): book.fix_tree_tags() book.html_built.send(sender=book) return True + book.fix_tree_tags() return False @BuildEbook.register('cover_thumb') diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 1ab2c42f5..c91ce1151 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -330,8 +330,8 @@ class Book(models.Model): book.cover_thumb.build_delay() # Build HTML and ebooks. + book.html_file.build_delay() if not children: - book.html_file.build_delay() for format_ in constants.EBOOK_FORMATS_WITHOUT_CHILDREN: if format_ not in dont_build: getattr(book, '%s_file' % format_).build_delay() diff --git a/apps/opds/tests/__init__.py b/apps/opds/tests/__init__.py index 8d51b7b92..ea72f06e6 100755 --- a/apps/opds/tests/__init__.py +++ b/apps/opds/tests/__init__.py @@ -2,7 +2,9 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from unittest import skipIf from lxml import etree +from django.conf import settings from django.core.files.base import ContentFile import catalogue from catalogue.test_utils import (BookInfoStub, PersonStub, info_args, @@ -14,6 +16,8 @@ from search import Index, Search AtomNS = XMLNamespace("http://www.w3.org/2005/Atom") +@skipIf(getattr(settings, 'NO_SEARCH_INDEX', False), + u'Requires search server and NO_SEARCH_INDEX=False.') class OpdsSearchTests(WLTestCase): """Tests search feed in OPDS..""" def setUp(self): @@ -22,11 +26,10 @@ class OpdsSearchTests(WLTestCase): index.index.delete_all() index.index.commit() - with self.settings(NO_SEARCH_INDEX=False): - self.do_doktora = Book.from_xml_file( - get_fixture('do-doktora.xml')) - self.do_anusie = Book.from_xml_file( - get_fixture('fraszka-do-anusie.xml', catalogue)) + self.do_doktora = Book.from_xml_file( + get_fixture('do-doktora.xml')) + self.do_anusie = Book.from_xml_file( + get_fixture('fraszka-do-anusie.xml', catalogue)) def assert_finds(self, query, books): """Takes a query and tests against books expected to be found.""" diff --git a/apps/picture/tests/picture_import.py b/apps/picture/tests/picture_import.py index 84009e674..1b01d334c 100644 --- a/apps/picture/tests/picture_import.py +++ b/apps/picture/tests/picture_import.py @@ -14,8 +14,15 @@ class PictureTest(WLTestCase): def test_import(self): picture = Picture.from_xml_file(path.join(path.dirname(__file__), "files/kandinsky-composition-viii.xml")) - motifs = set([tag.name for tag in picture.tags if tag.category == 'theme']) - assert motifs == set([u'nieporządek']), 'theme tags are wrong. %s' % motifs + themes = set() + for area in picture.areas.all(): + themes.update([(tag.category, tag.name) + for tag in area.tags if tag.category in (u'theme', u'thing')]) + assert themes == set([(u'theme', u'nieporządek'), (u'thing', u'kosmos')]), \ + 'Bad themes on Picture areas: %s' % themes + + pic_themes = set([tag.name for tag in picture.tags if tag.category in ('theme', 'object')]) + assert not pic_themes, 'Unwanted themes set on Pictures: %s' % pic_themes picture.delete() @@ -27,10 +34,10 @@ class PictureTest(WLTestCase): def test_import_2(self): - picture = Picture.from_xml_file(path.join(path.dirname(__file__), "files/pejzaz-i-miasto-krzyzanowski-chmury.xml"), - path.join(path.dirname(__file__), "files/pejzaz-i-miasto-krzyzanowski-chmury.jpg"), + picture = Picture.from_xml_file(path.join(path.dirname(__file__), "files/kandinsky-composition-viii.xml"), + path.join(path.dirname(__file__), "files/kandinsky-composition-viii.png"), overwrite=True) cats = set([t.category for t in picture.tags]) - assert 'genre' in cats + assert 'epoch' in cats assert 'kind' in cats diff --git a/apps/search/tests/index.py b/apps/search/tests/index.py index d244a2cb0..fc2da1e96 100644 --- a/apps/search/tests/index.py +++ b/apps/search/tests/index.py @@ -2,6 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from unittest import skipIf from django.conf import settings from django.test.utils import override_settings from catalogue.test_utils import WLTestCase, get_fixture @@ -16,6 +17,8 @@ import opds @override_settings( SEARCH_INDEX = tempfile.mkdtemp(prefix='djangotest_search_'), ) +@skipIf(getattr(settings, 'NO_SEARCH_INDEX', False), + u'Requires search server and NO_SEARCH_INDEX=False.') class BookSearchTests(WLTestCase): def setUp(self): WLTestCase.setUp(self) @@ -25,11 +28,10 @@ class BookSearchTests(WLTestCase): index.delete_query(self.search.index.query(uid="*")) index.index.commit() - with self.settings(NO_SEARCH_INDEX=False): - self.do_doktora = Book.from_xml_file( - get_fixture('do-doktora.xml', opds)) - self.do_anusie = Book.from_xml_file( - get_fixture('fraszka-do-anusie.xml', catalogue)) + self.do_doktora = Book.from_xml_file( + get_fixture('do-doktora.xml', opds)) + self.do_anusie = Book.from_xml_file( + get_fixture('fraszka-do-anusie.xml', catalogue)) def test_search_perfect_book_author(self): books = self.search.search_books(self.search.index.query(authors=u"sęp szarzyński")) -- 2.20.1