- # Formats
- xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
- html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
- pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
- odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
- txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)
-
- parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
-
- objects = models.Manager()
- tagged = managers.ModelTaggedItemManager(Tag)
- tags = managers.TagDescriptor(Tag)
-
+ parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
+ objects = models.Manager()
+ tagged = managers.ModelTaggedItemManager(Tag)
+ tags = managers.TagDescriptor(Tag)
+
+ _tag_counter = JSONField(null=True, editable=False)
+ _theme_counter = JSONField(null=True, editable=False)
+
+ class AlreadyExists(Exception):
+ pass
+
+ class Meta:
+ ordering = ('title',)
+ verbose_name = _('book')
+ verbose_name_plural = _('books')
+
+ def __unicode__(self):
+ return self.title
+
+ def save(self, force_insert=False, force_update=False, reset_short_html=True, refresh_mp3=True, **kwargs):
+ if reset_short_html:
+ # Reset _short_html during save
+ update = {}
+ for key in filter(lambda x: x.startswith('_short_html'), self.__dict__):
+ update[key] = ''
+ self.__setattr__(key, '')
+ # Fragment.short_html relies on book's tags, so reset it here too
+ self.fragments.all().update(**update)
+
+ book = super(Book, self).save(force_insert, force_update)
+
+ if refresh_mp3 and self.has_media('mp3'):
+ file = self.get_media('mp3')[0]
+ #print file, file.path
+ extra_info = self.get_extra_info_value()
+ extra_info.update(self.get_mp3_info())
+ self.set_extra_info_value(extra_info)
+ book = super(Book, self).save(force_insert, force_update)
+ return book
+
+ @permalink
+ def get_absolute_url(self):
+ return ('catalogue.views.book_detail', [self.slug])
+
+ @property
+ def name(self):
+ return self.title
+
+ def book_tag_slug(self):
+ return ('l-' + self.slug)[:120]
+
+ def book_tag(self):
+ slug = self.book_tag_slug()
+ book_tag, created = Tag.objects.get_or_create(slug=slug, category='book')
+ if created:
+ book_tag.name = self.title[:50]
+ book_tag.sort_key = self.title.lower()
+ book_tag.save()
+ return book_tag
+
+ def has_media(self, type):
+ if type == 'xml':
+ if self.xml_file:
+ return True
+ else:
+ return False
+ elif type == 'html':
+ if self.html_file:
+ return True
+ else:
+ return False
+ elif type == 'txt':
+ if self.txt_file:
+ return True
+ else:
+ return False
+ elif type == 'pdf':
+ if self.pdf_file:
+ return True
+ else:
+ return False
+ elif type == 'epub':
+ if self.epub_file:
+ return True
+ else:
+ return False
+ else:
+ if self.medias.filter(book=self, type=type).count() > 0:
+ return True
+ else:
+ return False
+
+ def get_media(self, type):
+ if self.has_media(type):
+ if type == "xml":
+ return self.xml_file
+ elif type == "html":
+ return self.html_file
+ elif type == "epub":
+ return self.html_file
+ elif type == "txt":
+ return self.txt_file
+ elif type == "pdf":
+ return self.pdf_file
+ else:
+ return self.medias.filter(book=self, type=type)
+ else:
+ return None
+
+ def get_mp3(self):
+ return self.get_media("mp3")
+ def get_odt(self):
+ return self.get_media("odt")
+ def get_ogg(self):
+ return self.get_media("ogg")
+ def get_daisy(self):
+ return self.get_media("daisy")
+