+ book_tags.append(tag)
+
+ book.tags = book_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()
+
+ html_file = NamedTemporaryFile()
+ if html.transform(book.xml_file.path, html_file, parse_dublincore=False):
+ book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
+
+ # get ancestor l-tags for adding to new fragments
+ ancestor_tags = []
+ p = book.parent
+ while p:
+ ancestor_tags.append(p.book_tag())
+ p = p.parent
+
+ # Extract fragments
+ closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
+ for fragment in closed_fragments.values():
+ try:
+ theme_names = [s.strip() for s in fragment.themes.split(',')]
+ except AttributeError:
+ continue
+ themes = []
+ for theme_name in theme_names:
+ if not theme_name:
+ continue
+ tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name), category='theme')
+ if created:
+ tag.name = theme_name
+ tag.sort_key = theme_name.lower()
+ tag.save()
+ themes.append(tag)
+ if not themes:
+ continue
+
+ text = fragment.to_string()
+ short_text = ''
+ if (len(MarkupString(text)) > 240):
+ short_text = unicode(MarkupString(text)[:160])
+ new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
+ defaults={'text': text, 'short_text': short_text})
+
+ new_fragment.save()
+ new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags)
+
+ if not book.parent:
+ book.build_epub(remove_descendants=False)
+
+ 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]
+ if child_book.has_epub_file():
+ child_book.epub_file.delete()
+ 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())
+
+ # refresh cache
+ book.reset_tag_counter()
+ book.reset_theme_counter()
+
+ book.save()
+ return book
+
+
+ def refresh_tag_counter(self):
+ 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
+ self.set__tag_counter_value(tags)
+ self.save(reset_short_html=False, refresh_mp3=False)
+ return tags
+
+ def reset_tag_counter(self):
+ self._tag_counter = None
+ self.save(reset_short_html=False, refresh_mp3=False)
+ if self.parent:
+ self.parent.reset_tag_counter()
+
+ @property
+ def tag_counter(self):
+ if self._tag_counter is None:
+ return self.refresh_tag_counter()
+ return dict((int(k), v) for k, v in self.get__tag_counter_value().iteritems())
+
+ def refresh_theme_counter(self):
+ 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
+ self.set__theme_counter_value(tags)
+ self.save(reset_short_html=False, refresh_mp3=False)
+ return tags
+
+ def reset_theme_counter(self):
+ self._theme_counter = None
+ self.save(reset_short_html=False, refresh_mp3=False)
+ if self.parent:
+ self.parent.reset_theme_counter()
+
+ @property
+ def theme_counter(self):
+ if self._theme_counter is None:
+ return self.refresh_theme_counter()
+ return dict((int(k), v) for k, v in self.get__theme_counter_value().iteritems())