"""Represents a book imported from WL-XML."""
title = models.CharField(_('title'), max_length=120)
sort_key = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False)
+ sort_key_author = models.CharField(_('sort key by author'), max_length=120, db_index=True, editable=False, default=u'')
slug = models.SlugField(_('slug'), max_length=120, db_index=True,
unique=True)
common_slug = models.SlugField(_('slug'), max_length=120, db_index=True)
cover = EbookField('cover', _('cover'),
upload_to=book_upload_path('jpg'), null=True, blank=True)
+ # Cleaner version of cover for thumbs
+ cover_thumb = EbookField('cover_thumb', _('cover thumbnail'),
+ upload_to=book_upload_path('th.jpg'), null=True, blank=True)
ebook_formats = constants.EBOOK_FORMATS
formats = ebook_formats + ['html', 'xml']
from sortify import sortify
self.sort_key = sortify(self.title)
+ self.title = unicode(self.title) # ???
ret = super(Book, self).save(force_insert, force_update, **kwargs)
def name(self):
return self.title
+ def language_code(self):
+ return constants.LANGUAGES_3TO2.get(self.language, self.language)
+
+ def language_name(self):
+ return dict(settings.LANGUAGES).get(self.language_code(), "")
+
def book_tag_slug(self):
return ('l-' + self.slug)[:120]
if self.has_media(type_):
if type_ in Book.formats:
return getattr(self, "%s_file" % type_)
- else:
+ else:
return self.media.filter(type=type_)
else:
return None
def get_ogg(self):
return self.get_media("ogg")
def get_daisy(self):
- return self.get_media("daisy")
+ return self.get_media("daisy")
def reset_short_html(self):
if self.id is None:
for fragm in self.fragments.all().iterator():
fragm.reset_short_html()
+ try:
+ author = self.tags.filter(category='author')[0].sort_key
+ except IndexError:
+ author = u''
+ type(self).objects.filter(pk=self.pk).update(sort_key_author=author)
+
+
+
def has_description(self):
return len(self.description) > 0
has_description.short_description = _('description')
tasks.fix_tree_tags.delay(book)
if 'cover' not in dont_build:
book.cover.build_delay()
-
+ book.cover_thumb.build_delay()
+
# No saves behind this point.
if has_own_text:
if not self.cover_info(inherit=False):
if 'cover' not in app_settings.DONT_BUILD:
self.cover.build_delay()
+ self.cover_thumb.build_delay()
for format_ in constants.EBOOK_FORMATS_WITH_COVERS:
if format_ not in app_settings.DONT_BUILD:
getattr(self, '%s_file' % format_).build_delay()
for child in self.children.all():
child.parent_cover_changed()
+ def other_versions(self):
+ """Find other versions (i.e. in other languages) of the book."""
+ return type(self).objects.filter(common_slug=self.common_slug).exclude(pk=self.pk)
+
def related_info(self):
"""Keeps info about related objects (tags, media) in cache field."""
if self._related_info is not None:
for category in tags:
cat = []
for tag in tags[category]:
- tag_info = {'slug': tag.slug}
+ tag_info = {'slug': tag.slug, 'name': tag.name}
for lc, ln in settings.LANGUAGES:
tag_name = getattr(tag, "name_%s" % lc)
if tag_name:
book = self
rel_info = book.related_info()
names = [(related_tag_name(tag), Tag.create_url('author', tag['slug']))
- for tag in rel_info['tags']['author']]
+ for tag in rel_info['tags'].get('author', ())]
if 'parents' in rel_info:
books = [(name, Book.create_url(slug))
for name, slug in rel_info['parents']]
"""
# get relevant books and their tags
objects = cls.tagged.with_all(tags)
- parents = objects.filter(html_file='').only('slug')
+ parents = objects.exclude(children=None).only('slug')
# eliminate descendants
l_tags = Tag.objects.filter(category='book',
slug__in=[book.book_tag_slug() for book in parents.iterator()])
'title', 'parent', 'slug')
if filter:
books = books.filter(filter).distinct()
-
+
book_ids = set(b['pk'] for b in books.values("pk").iterator())
for book in books.iterator():
parent = book.parent_id
for tag in Tag.objects.filter(category='author').iterator():
books_by_author[tag] = []
- for book in books_by_parent.get(None,()):
+ for book in books_by_parent.get(None, ()):
authors = list(book.tags.filter(category='author'))
if authors:
for author in authors: