+ Makes sure that:
+ * the book has its parents book-tags,
+ * its fragments have the book's and its parents book-tags,
+ * runs those for every child book too,
+ * touches all relevant tags,
+ * resets tag and theme counter on the book and its ancestry.
+ """
+ def fix_subtree(book, parent_tags):
+ affected_tags = set(book.tags)
+ book.tags = list(book.tags.exclude(category='book')) + parent_tags
+ sub_parent_tags = parent_tags + [book.book_tag()]
+ for frag in book.fragments.all():
+ affected_tags.update(frag.tags)
+ frag.tags = list(frag.tags.exclude(category='book')) + sub_parent_tags
+ for child in book.children.all():
+ affected_tags.update(fix_subtree(child, sub_parent_tags))
+ return affected_tags
+
+ parent_tags = []
+ parent = self.parent
+ while parent is not None:
+ parent_tags.append(parent.book_tag())
+ parent = parent.parent
+
+ affected_tags = fix_subtree(self, parent_tags)
+ for tag in affected_tags:
+ tasks.touch_tag(tag)