+ meta_tags.append(tag)
+
+ book.tags = set(meta_tags + book_shelves)
+
+ book_tag = book.book_tag()
+
+ for n, child_book in enumerate(children):
+ child_book.parent = book
+ child_book.parent_number = n
+ child_book.save()
+
+ # Save XML and HTML files
+ book.xml_file.save('%s.xml' % book.slug, raw_file, save=False)
+
+ # delete old fragments when overwriting
+ book.fragments.all().delete()
+
+ if book.build_html():
+ if not settings.NO_BUILD_TXT and build_txt:
+ book.build_txt()
+
+ if not settings.NO_BUILD_EPUB and build_epub:
+ book.root_ancestor.build_epub()
+
+ if not settings.NO_BUILD_PDF and build_pdf:
+ book.root_ancestor.build_pdf()
+
+ if not settings.NO_BUILD_MOBI and build_mobi:
+ book.build_mobi()
+
+ if not settings.NO_SEARCH_INDEX and search_index:
+ book.search_index()
+
+ book_descendants = list(book.children.all())
+ # add l-tag to descendants and their fragments
+ # delete unnecessary EPUB files
+ while len(book_descendants) > 0:
+ child_book = book_descendants.pop(0)
+ child_book.tags = list(child_book.tags) + [book_tag]
+ child_book.save()
+ for fragment in child_book.fragments.all():
+ fragment.tags = set(list(fragment.tags) + [book_tag])
+ book_descendants += list(child_book.children.all())
+
+ book.save()
+
+ # refresh cache
+ book.reset_tag_counter()
+ book.reset_theme_counter()
+
+ return book
+
+ def reset_tag_counter(self):
+ if self.id is None:
+ return
+
+ cache_key = "Book.tag_counter/%d" % self.id
+ cache.delete(cache_key)
+ if self.parent:
+ self.parent.reset_tag_counter()
+
+ @property
+ def tag_counter(self):
+ if self.id:
+ cache_key = "Book.tag_counter/%d" % self.id
+ tags = cache.get(cache_key)
+ else:
+ tags = None
+
+ if tags is None:
+ tags = {}
+ for child in self.children.all().order_by():
+ for tag_pk, value in child.tag_counter.iteritems():
+ tags[tag_pk] = tags.get(tag_pk, 0) + value
+ for tag in self.tags.exclude(category__in=('book', 'theme', 'set')).order_by():
+ tags[tag.pk] = 1
+
+ if self.id:
+ cache.set(cache_key, tags, CACHE_FOREVER)
+ return tags
+
+ def reset_theme_counter(self):
+ if self.id is None:
+ return
+
+ cache_key = "Book.theme_counter/%d" % self.id
+ cache.delete(cache_key)
+ if self.parent:
+ self.parent.reset_theme_counter()
+
+ @property
+ def theme_counter(self):
+ if self.id:
+ cache_key = "Book.theme_counter/%d" % self.id
+ tags = cache.get(cache_key)
+ else:
+ tags = None
+
+ if tags is None:
+ tags = {}
+ for fragment in Fragment.tagged.with_any([self.book_tag()]).order_by():
+ for tag in fragment.tags.filter(category='theme').order_by():
+ tags[tag.pk] = tags.get(tag.pk, 0) + 1
+
+ if self.id:
+ cache.set(cache_key, tags, CACHE_FOREVER)
+ return tags
+
+ def pretty_title(self, html_links=False):
+ book = self
+ names = list(book.tags.filter(category='author'))
+
+ books = []
+ while book:
+ books.append(book)
+ book = book.parent
+ names.extend(reversed(books))
+
+ if html_links:
+ names = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
+ else:
+ names = [tag.name for tag in names]
+
+ return ', '.join(names)
+
+ @classmethod
+ def tagged_top_level(cls, tags):
+ """ Returns top-level books tagged with `tags'.
+
+ It only returns those books which don't have ancestors which are
+ also tagged with those tags.
+
+ """
+ # get relevant books and their tags
+ objects = cls.tagged.with_all(tags)
+ # eliminate descendants
+ l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects])
+ descendants_keys = [book.pk for book in cls.tagged.with_any(l_tags)]
+ if descendants_keys:
+ objects = objects.exclude(pk__in=descendants_keys)
+
+ return objects
+
+
+def _has_factory(ftype):
+ has = lambda self: bool(getattr(self, "%s_file" % ftype))
+ has.short_description = t.upper()
+ has.boolean = True
+ has.__name__ = "has_%s_file" % ftype
+ return has
+