- html.transform(book.xml_file.path, html_file)
- book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
-
- # Extract fragments
- closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
- book_themes = []
- for fragment in closed_fragments.values():
- text = fragment.to_string()
- short_text = ''
- if (len(MarkupString(text)) > 240):
- short_text = unicode(MarkupString(text)[:160])
- new_fragment = Fragment(text=text, short_text=short_text, anchor=fragment.id, book=book)
-
- theme_names = [s.strip() for s in fragment.themes.split(',')]
- themes = []
- for theme_name in theme_names:
- tag, created = Tag.objects.get_or_create(name=theme_name,
- slug=slughifi(theme_name), sort_key=slughifi(theme_name), category='theme')
- tag.save()
- themes.append(tag)
- new_fragment.save()
- new_fragment.tags = list(book.tags) + themes
- book_themes += themes
-
- book_themes = set(book_themes)
- book.tags = list(book.tags) + list(book_themes)
- return book.save()
-
- @permalink
- def get_absolute_url(self):
- return ('catalogue.views.book_detail', [self.slug])
-
- class Meta:
- ordering = ('title',)
- verbose_name = _('book')
- verbose_name_plural = _('books')
+ 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)
+
+ # Create EPUB
+ epub_file = StringIO()
+ epub.transform(book.xml_file, epub_file)
+ book.epub_file.save('%s.epub' % book.slug, ContentFile(epub_file.getvalue()), save=False)
+ FileRecord(slug=book.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
+
+ # 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 = slughifi(theme_name)
+ 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])
+
+ # refresh cache
+ book.tag_counter
+ book.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())