X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/e24e657ebf536f55c1bde66bd41563fae30a98b0..86530a9e72f32d28ef1971ac9fa705c85b1bd3b6:/src/catalogue/models/book.py diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 35d0e8c4e..7d3666277 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -1,5 +1,5 @@ -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # from collections import OrderedDict import json @@ -19,7 +19,7 @@ from django.utils.translation import gettext_lazy as _, get_language from fnpdjango.storage import BofhFileSystemStorage from lxml import html from librarian.cover import WLCover -from librarian.html import transform_abstrakt +from librarian.builders.html import AbstraktHtmlBuilder from librarian.builders import builders from newtagging import managers from catalogue import constants @@ -91,6 +91,7 @@ class Book(models.Model): tagged = managers.ModelTaggedItemManager(Tag) tags = managers.TagDescriptor(Tag) tag_relations = GenericRelation(Tag.intermediary_table_model) + translators = models.ManyToManyField(Tag, blank=True) html_built = django.dispatch.Signal() published = django.dispatch.Signal() @@ -154,12 +155,6 @@ class Book(models.Model): def genre_unicode(self): return self.tag_unicode('genre') - def translators(self): - translators = self.get_extra_info_json().get('translators') or [] - return [ - '\xa0'.join(reversed(translator.split(', ', 1))) for translator in translators - ] - def translator(self): translators = self.get_extra_info_json().get('translators') if not translators: @@ -331,6 +326,12 @@ class Book(models.Model): total += app_settings.GET_MP3_LENGTH(media.file.path) return int(total) + def get_time(self): + try: + return round(self.xml_file.size / 1000 * 40) + except ValueError: + return 0 + def has_media(self, type_): if type_ in Book.formats: return bool(getattr(self, "%s_file" % type_)) @@ -416,7 +417,7 @@ class Book(models.Model): has_daisy_file.boolean = True def has_sync_file(self): - return self.has_media("sync") + return settings.FEATURE_SYNCHRO and self.has_media("sync") def get_sync(self): with self.get_media('sync').first().file.open('r') as f: @@ -556,11 +557,8 @@ class Book(models.Model): urlretrieve('%s/%s' % (remote_gallery_url, ilustr_src), ilustr_path) def load_abstract(self): - abstract = self.wldocument(parse_dublincore=False).edoc.getroot().find('.//abstrakt') - if abstract is not None: - self.abstract = transform_abstrakt(abstract) - else: - self.abstract = '' + self.abstract = AbstraktHtmlBuilder().build( + self.wldocument2()).get_bytes().decode('utf-8') def load_toc(self): self.toc = '' @@ -594,7 +592,7 @@ class Book(models.Model): @classmethod def from_text_and_meta(cls, raw_file, book_info, overwrite=False, dont_build=None, search_index=True, - remote_gallery_url=None, days=0, findable=True): + remote_gallery_url=None, days=0, findable=True, logo=None, logo_mono=None, logo_alt=None): from catalogue import tasks if dont_build is None: @@ -641,21 +639,26 @@ class Book(models.Model): book.common_slug = book_info.variant_of.slug else: book.common_slug = book.slug - book.extra_info = json.dumps(book_info.to_dict()) + extra = book_info.to_dict() + if logo: + extra['logo'] = logo + if logo_mono: + extra['logo_mono'] = logo_mono + if logo_alt: + extra['logo_alt'] = logo_alt + book.extra_info = json.dumps(extra) book.load_abstract() book.load_toc() book.save() meta_tags = Tag.tags_from_info(book_info) - for tag in meta_tags: - if not tag.for_books: - tag.for_books = True - tag.save() - - book.tags = set(meta_tags + book_shelves) + just_tags = [t for (t, rel) in meta_tags if not rel] + book.tags = set(just_tags + book_shelves) book.save() # update sort_key_author + book.translators.set([t for (t, rel) in meta_tags if rel == 'translator']) + cover_changed = old_cover != book.cover_info() obsolete_children = set(b for b in book.children.all() if b not in children) @@ -714,13 +717,14 @@ class Book(models.Model): cls.published.send(sender=cls, instance=book) return book + # TODO TEST def update_references(self): Entity = apps.get_model('references', 'Entity') doc = self.wldocument2() - doc._compat_assign_section_ids() - doc._compat_assign_ordered_ids() + doc.assign_ids() + refs = {} - for ref_elem in doc.references(): + for i, ref_elem in enumerate(doc.references()): uri = ref_elem.attrib.get('href', '') if not uri: continue @@ -729,16 +733,18 @@ class Book(models.Model): else: entity, entity_created = Entity.objects.get_or_create(uri=uri) if entity_created: - entity.populate() - entity.save() + try: + entity.populate() + except: + pass + else: + entity.save() ref, ref_created = entity.reference_set.get_or_create(book=self) refs[uri] = ref if not ref_created: ref.occurence_set.all().delete() - sec = ref_elem.get_link() - m = re.match(r'sec(\d+)', sec) - assert m is not None - sec = int(m.group(1)) + anchor = ref_elem.get_link() + snippet = ref_elem.get_snippet() b = builders['html-snippet']() for s in snippet: @@ -746,7 +752,8 @@ class Book(models.Model): html = b.output().get_bytes().decode('utf-8') ref.occurence_set.create( - section=sec, + section=i, + anchor=anchor, html=html ) self.reference_set.exclude(entity__uri__in=refs).delete()