- 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, db_index=True)
+ language = models.CharField(_('language code'), max_length=3, db_index=True,
+ default=settings.CATALOGUE_DEFAULT_LANGUAGE)
+ 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
+
+ cover = models.FileField(_('cover'), upload_to=book_upload_path('png'),
+ null=True, blank=True)
+ ebook_formats = ['pdf', 'epub', 'mobi', 'txt']
+ formats = ebook_formats + ['html', 'xml']
+
+ 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()
+ published = django.dispatch.Signal()
+
+ URLID_RE = r'[a-z0-9-]+(?:/[a-z]{3})?'
+ FILEID_RE = r'[a-z0-9-]+(?:_[a-z]{3})?'
+
+ class AlreadyExists(Exception):
+ pass
+
+ class Meta:
+ unique_together = [['slug', 'language']]
+ ordering = ('sort_key',)
+ verbose_name = _('book')
+ verbose_name_plural = _('books')
+
+ def __unicode__(self):
+ return self.title
+
+ def urlid(self, sep='/'):
+ stem = self.slug
+ if self.language != settings.CATALOGUE_DEFAULT_LANGUAGE:
+ stem += sep + self.language
+ return stem
+
+ def fileid(self):
+ return self.urlid('_')
+
+ @staticmethod
+ def split_urlid(urlid, sep='/', default_lang=settings.CATALOGUE_DEFAULT_LANGUAGE):
+ """Splits a URL book id into slug and language code.
+
+ Returns a dictionary usable i.e. for object lookup, or None.
+
+ >>> Book.split_urlid("a-slug/pol", default_lang="eng")
+ {'slug': 'a-slug', 'language': 'pol'}
+ >>> Book.split_urlid("a-slug", default_lang="eng")
+ {'slug': 'a-slug', 'language': 'eng'}
+ >>> Book.split_urlid("a-slug_pol", "_", default_lang="eng")
+ {'slug': 'a-slug', 'language': 'pol'}
+ >>> Book.split_urlid("a-slug/eng", default_lang="eng")
+
+ """
+ parts = urlid.rsplit(sep, 1)
+ if len(parts) == 2:
+ if parts[1] == default_lang:
+ return None
+ return {'slug': parts[0], 'language': parts[1]}
+ else:
+ return {'slug': urlid, 'language': default_lang}
+
+ @classmethod
+ def split_fileid(cls, fileid):
+ return cls.split_urlid(fileid, '_')
+
+ def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs):
+ from sortify import sortify
+
+ 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.urlid()])
+
+ @property
+ def name(self):
+ return self.title
+
+ def book_tag_slug(self):
+ stem = 'l-' + self.slug
+ if self.language != settings.CATALOGUE_DEFAULT_LANGUAGE:
+ return stem[:116] + ' ' + self.language
+ else:
+ return stem[: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 in Book.formats:
+ return bool(getattr(self, "%s_file" % type))
+ else:
+ return self.media.filter(type=type).exists()
+
+ def get_media(self, type):
+ if self.has_media(type):
+ if type in Book.formats:
+ return getattr(self, "%s_file" % type)
+ 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))
+ cache.delete("Book.mini_box/%d" % (self.id, ))
+ # Fragment.short_html relies on book's tags, so reset it here too
+ for fragm in self.fragments.all():
+ fragm.reset_short_html()
+