+ class Meta:
+ ordering = ('type', 'name')
+ verbose_name = _('book media')
+ verbose_name_plural = _('book media')
+
+ def save(self, force_insert=False, force_update=False, **kwargs):
+ media = super(BookMedia, self).save(force_insert, force_update, **kwargs)
+ if self.type == 'mp3':
+ file = self.file
+ extra_info = self.get_extra_info_value()
+ extra_info.update(self.get_mp3_info())
+ self.set_extra_info_value(extra_info)
+ media = super(BookMedia, self).save(force_insert, force_update, **kwargs)
+ return media
+
+ def get_mp3_info(self):
+ """Retrieves artist and director names from audio ID3 tags."""
+ try:
+ audio = id3.ID3(self.file.path)
+ artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1'))
+ director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3'))
+ except:
+ artist_name = director_name = ''
+ return {'artist_name': artist_name, 'director_name': director_name}
+
+
+class Book(models.Model):
+ title = models.CharField(_('title'), max_length=120)
+ slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
+ description = models.TextField(_('description'), blank=True)
+ created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
+ changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
+ _short_html = models.TextField(_('short HTML'), editable=False)
+ parent_number = models.IntegerField(_('parent number'), default=0)
+ extra_info = JSONField(_('extra information'))
+ gazeta_link = models.CharField(blank=True, max_length=240)
+ wiki_link = models.CharField(blank=True, max_length=240)
+ # files generated during publication
+ 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)
+ epub_file = models.FileField(_('EPUB file'), upload_to=book_upload_path('epub'), blank=True)
+ txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)
+ # other files
+ medias = models.ManyToManyField(BookMedia, blank=True)