+ if self.id:
+ cache_key = "Book.theme_counter/%d" % self.id
+ tags = permanent_cache.get(cache_key)
+ else:
+ tags = None
+
+ if tags is None:
+ tags = {}
+ for fragment in Fragment.tagged.with_any([self.book_tag()]).order_by().iterator():
+ for tag in fragment.tags.filter(category='theme').order_by().iterator():
+ tags[tag.pk] = tags.get(tag.pk, 0) + 1
+
+ if self.id:
+ permanent_cache.set(cache_key, tags)
+ return tags
+
+ def pretty_title(self, html_links=False):
+ book = self
+ names = list(book.tags.filter(category='author'))
+
+ books = []
+ while book:
+ books.append(book)
+ book = book.parent
+ names.extend(reversed(books))
+
+ if html_links:
+ names = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
+ else:
+ names = [tag.name for tag in names]
+
+ return ', '.join(names)
+
+ @classmethod
+ def tagged_top_level(cls, tags):
+ """ Returns top-level books tagged with `tags`.
+
+ It only returns those books which don't have ancestors which are
+ also tagged with those tags.
+
+ """
+ # get relevant books and their tags
+ objects = cls.tagged.with_all(tags)
+ # eliminate descendants
+ l_tags = Tag.objects.filter(category='book',
+ slug__in=[book.book_tag_slug() for book in objects.iterator()])
+ descendants_keys = [book.pk for book in cls.tagged.with_any(l_tags).iterator()]
+ if descendants_keys:
+ objects = objects.exclude(pk__in=descendants_keys)
+
+ return objects
+
+ @classmethod
+ def book_list(cls, filter=None):
+ """Generates a hierarchical listing of all books.
+
+ Books are optionally filtered with a test function.
+
+ """
+
+ books_by_parent = {}
+ books = cls.objects.all().order_by('parent_number', 'sort_key').only(
+ '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
+ if parent not in book_ids:
+ parent = None
+ books_by_parent.setdefault(parent, []).append(book)
+ else:
+ for book in books.iterator():
+ books_by_parent.setdefault(book.parent_id, []).append(book)
+
+ orphans = []
+ books_by_author = SortedDict()
+ for tag in Tag.objects.filter(category='author').iterator():
+ books_by_author[tag] = []
+
+ for book in books_by_parent.get(None,()):
+ authors = list(book.tags.filter(category='author'))
+ if authors:
+ for author in authors:
+ books_by_author[author].append(book)
+ else:
+ orphans.append(book)
+
+ return books_by_author, orphans, books_by_parent
+
+ _audiences_pl = {
+ "SP1": (1, u"szkoła podstawowa"),
+ "SP2": (1, u"szkoła podstawowa"),
+ "P": (1, u"szkoła podstawowa"),
+ "G": (2, u"gimnazjum"),
+ "L": (3, u"liceum"),
+ "LP": (3, u"liceum"),
+ }
+ def audiences_pl(self):
+ audiences = self.extra_info.get('audiences', [])
+ audiences = sorted(set([self._audiences_pl[a] for a in audiences]))
+ return [a[1] for a in audiences]
+
+ def choose_fragment(self):
+ tag = self.book_tag()
+ fragments = Fragment.tagged.with_any([tag])
+ if fragments.exists():
+ return fragments.order_by('?')[0]
+ elif self.parent:
+ return self.parent.choose_fragment()
+ else:
+ return None
+
+
+def _has_factory(ftype):
+ has = lambda self: bool(getattr(self, "%s_file" % ftype))
+ has.short_description = ftype.upper()
+ has.__doc__ = None
+ has.boolean = True
+ has.__name__ = "has_%s_file" % ftype
+ return has
+