X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/276656dcc680e1a35103d310dea046152ddc3785..4cf27ad4ae5ac3610e857379b2a788582580aa5b:/apps/catalogue/models.py?ds=inline diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index ebecc36b4..a94411191 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -610,8 +610,9 @@ class Book(models.Model): text = fragment.to_string() short_text = '' - if (len(MarkupString(text)) > 240): - short_text = unicode(MarkupString(text)[:160]) + markup = MarkupString(text) + if (len(markup) > 240): + short_text = unicode(markup[:160]) new_fragment = Fragment.objects.create(anchor=fragment.id, book=self, text=text, short_text=short_text) @@ -781,14 +782,26 @@ class Book(models.Model): return self._related_info else: rel = {'tags': {}, 'media': {}} + tags = self.tags.filter(category__in=( 'author', 'kind', 'genre', 'epoch')) tags = split_tags(tags) for category in tags: rel['tags'][category] = [ - (t.name, t.get_absolute_url()) for t in tags[category]] + (t.name, t.slug) for t in tags[category]] + for media_format in BookMedia.formats: rel['media'][media_format] = self.has_media(media_format) + + book = self + parents = [] + while book.parent: + parents.append((book.parent.title, book.parent.slug)) + book = book.parent + parents = parents[::-1] + if parents: + rel['parents'] = parents + if self.pk: type(self).objects.filter(pk=self.pk).update(_related_info=rel) return rel @@ -935,6 +948,16 @@ class Book(models.Model): audiences = sorted(set([self._audiences_pl[a] for a in audiences])) return [a[1] for a in audiences] + def choose_fragment(self): + tag = self.book_tag() + fragments = Fragment.tagged.with_any([tag]) + if fragments.exists(): + return fragments.order_by('?')[0] + elif self.parent: + return self.parent.choose_fragment() + else: + return None + def _has_factory(ftype): has = lambda self: bool(getattr(self, "%s_file" % ftype))