- 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=True)
- _short_html = models.TextField(_('short HTML'), editable=False)
-
- # 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)
-
+ title = models.CharField(_('title'), max_length=120)
+ sort_key = models.CharField(_('sort_key'), max_length=120, db_index=True, editable=False)
+ 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)
+ parent_number = models.IntegerField(_('parent number'), default=0)
+ extra_info = JSONField(_('extra information'), default='{}')
+ 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)
+
+ parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
+ objects = models.Manager()
+ tagged = managers.ModelTaggedItemManager(Tag)
+ tags = managers.TagDescriptor(Tag)
+
+ html_built = django.dispatch.Signal()
+
+ class AlreadyExists(Exception):
+ pass
+
+ class Meta:
+ ordering = ('sort_key',)
+ 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, **kwargs):
+ self.sort_key = sortify(self.title)
+
+ ret = super(Book, self).save(force_insert, force_update)
+
+ if reset_short_html:
+ self.reset_short_html()
+
+ return ret
+
+ @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.media.filter(type=type).exists():
+ 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.epub_file
+ elif type == "txt":
+ return self.txt_file
+ elif type == "pdf":
+ return self.pdf_file
+ else:
+ return self.media.filter(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")
+
+ def reset_short_html(self):
+ if self.id is None:
+ return
+
+ cache_key = "Book.short_html/%d/%s"
+ for lang, langname in settings.LANGUAGES:
+ cache.delete(cache_key % (self.id, lang))
+ # Fragment.short_html relies on book's tags, so reset it here too
+ for fragm in self.fragments.all():
+ fragm.reset_short_html()
+