X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/3050ec49d5392fe0120fad4ef02ada55d8b7d04e..884be9d702f99a38cd9b04c4fdedca887286f35d:/apps/catalogue/models/book.py?ds=inline diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 02051fb29..3c50a964c 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -3,7 +3,7 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # import re -from django.conf import settings as settings +from django.conf import settings from django.core.cache import get_cache from django.db import models from django.db.models import permalink @@ -14,7 +14,7 @@ import jsonfield from catalogue import constants from catalogue.fields import EbookField from catalogue.models import Tag, Fragment, BookMedia -from catalogue.utils import create_zip, split_tags, book_upload_path +from catalogue.utils import create_zip, split_tags, book_upload_path, related_tag_name from catalogue import app_settings from catalogue import tasks from newtagging import managers @@ -27,6 +27,7 @@ class Book(models.Model): """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) @@ -74,6 +75,7 @@ class Book(models.Model): 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) @@ -95,6 +97,12 @@ class Book(models.Model): 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] @@ -140,6 +148,14 @@ class Book(models.Model): 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') @@ -391,6 +407,10 @@ class Book(models.Model): 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: @@ -402,8 +422,15 @@ class Book(models.Model): 'author', 'kind', 'genre', 'epoch')) tags = split_tags(tags) for category in tags: - rel['tags'][category] = [ - (t.name, t.slug) for t in tags[category]] + cat = [] + for tag in tags[category]: + tag_info = {'slug': tag.slug, 'name': tag.name} + for lc, ln in settings.LANGUAGES: + tag_name = getattr(tag, "name_%s" % lc) + if tag_name: + tag_info["name_%s" % lc] = tag_name + cat.append(tag_info) + rel['tags'][category] = cat for media_format in BookMedia.formats: rel['media'][media_format] = self.has_media(media_format) @@ -487,8 +514,10 @@ class Book(models.Model): def pretty_title(self, html_links=False): book = self rel_info = book.related_info() - names = [(name, Tag.create_url('author', slug)) - for name, slug in rel_info['tags']['author']] + names = [(related_tag_name(tag), Tag.create_url('author', tag['slug'])) + for tag in rel_info['tags'].get('author', ())] + import logging + logging.info("%s, %s" % (book.slug, unicode(rel_info['tags'].get('author', ())))) if 'parents' in rel_info: books = [(name, Book.create_url(slug)) for name, slug in rel_info['parents']]