parent_number = book.parent_number
sort_key = book.sort_key
size_str = pretty_size(html_file_size)
- authors = ", ".join(t.name for t in book.tags.filter(category='author'))
+ authors = book.author_unicode()
db.execute(book_sql, locals())
return item.name
def item_categories(self, item):
- return sorted(set(author.name for author in
- item.book.tags.filter(category='author').iterator()))
+ return sorted(item.book.authors().values_list('name', flat=True))
def item_description(self, item):
lines = []
except AttributeError:
return ''
- def author_str(self):
- return ", ".join(str(t) for t in self.tags.filter(category='author'))
+ def authors(self):
+ return self.tags.filter(category='author')
+
+ def author_unicode(self):
+ return ", ".join(self.authors().values_list('name', flat=True))
def save(self, force_insert=False, force_update=False, **kwargs):
from sortify import sortify
self.title = unicode(self.title) # ???
try:
- author = self.tags.filter(category='author')[0].sort_key
- except IndexError:
+ author = self.authors().first().sort_key
+ except AttributeError:
author = u''
self.sort_key_author = author
return books
def pretty_title(self, html_links=False):
- names = [(tag.name, tag.get_absolute_url()) for tag in self.tags.filter(category='author')]
+ names = [(tag.name, tag.get_absolute_url()) for tag in self.authors().only('name', 'category', 'slug')]
books = self.parents() + [self]
names.extend([(b.title, b.get_absolute_url()) for b in books])
"""
books_by_parent = {}
- books = cls.objects.all().order_by('parent_number', 'sort_key').only(
- 'title', 'parent', 'slug')
+ books = cls.objects.order_by('parent_number', 'sort_key').only('title', 'parent', 'slug')
if book_filter:
books = books.filter(book_filter).distinct()
books_by_author[tag] = []
for book in books_by_parent.get(None, ()):
- authors = list(book.tags.filter(category='author'))
+ authors = list(book.authors().only('pk'))
if authors:
for author in authors:
books_by_author[author].append(book)
<a href="{{ book.get_absolute_url }}">
{% endif %}
{% if book.cover_thumb %}
- <img src="{{ book.cover_thumb.url }}" alt="{{ author_str }} – {{ book.title }}" class="cover" />
+ <img src="{{ book.cover_thumb.url }}" alt="{{ author }} – {{ book.title }}" class="cover" />
{% endif %}
{% if show_lang %}
<span class="language" title="{{ book.language_name }}">{{ book.language_code }}</span>
{% endif %}
<div class="desc">
- <span class="mono author">{{ author_str }}</span>
+ <span class="mono author">{{ author }}</span>
<span class="title">{{ book.title }}</span>
</div>
{% if with_link %}
initial = obj.get_initial().upper()
if initial != last_initial:
last_initial = initial
- names.append((obj.author_str() if by_author else initial, []))
+ names.append((obj.author_unicode() if by_author else initial, []))
names[-1][1].append(obj)
return locals()
book = models.Book.objects.get(slug='parent')
related_themes = book.related_themes()
- self.assertEqual([t.slug for t in book.tags.filter(category='author')],
+ self.assertEqual([t.slug for t in book.authors()],
['common-man'])
self.assertEqual([t.slug for t in book.tags.filter(category='kind')],
['kind'])
@ssi_included
def book_mini(request, pk, with_link=True):
- book = get_object_or_404(models.Book, pk=pk)
- author_str = ", ".join(tag.name for tag in book.tags.filter(category='author'))
+ # book = get_object_or_404(models.Book, pk=pk)
+ try:
+ book = models.Book.objects.only('cover_thumb', 'title', 'language', 'slug').get(pk=pk)
+ except models.Book.DoesNotExist:
+ raise Http404
return render(request, 'catalogue/book_mini_box.html', {
'book': book,
- 'author_str': author_str,
+ 'author': book.author_unicode(),
'with_link': with_link,
'show_lang': book.language_code() != settings.LANGUAGE_CODE,
})
def item_author_name(self, book):
try:
- return book.tags.filter(category='author')[0].name
- except KeyError:
+ return book.authors().first().name
+ except AttributeError:
return u''
def item_author_link(self, book):
try:
- return book.tags.filter(category='author')[0].get_absolute_url()
- except KeyError:
+ return book.authors().first().get_absolute_url()
+ except AttributeError:
return u''
def item_enclosure_url(self, book):
self.sort_key = sortify(self.title)[:120]
try:
- author = self.tags.filter(category='author')[0].sort_key
- except IndexError:
+ author = self.authors().first().sort_key
+ except AttributeError:
author = u''
self.sort_key_author = author
def __unicode__(self):
return self.title
- def author_str(self):
- return ", ".join(str(t) for t in self.tags.filter(category='author'))
+ def authors(self):
+ return self.tags.filter(category='author')
+
+ def author_unicode(self):
+ return ", ".join(self.authors().values_list('name', flat=True))
@permalink
def get_absolute_url(self):
pics_by_author[tag] = []
for pic in pics.iterator():
- authors = list(pic.tags.filter(category='author'))
+ authors = list(pic.authors().only('pk'))
if authors:
for author in authors:
pics_by_author[author].append(pic)
return self._info
def pretty_title(self, html_links=False):
- picture = self
- names = [(tag.name, tag.get_absolute_url())
- for tag in self.tags.filter(category='author')]
+ names = [(tag.name, tag.get_absolute_url()) for tag in self.authors().only('name', 'category', 'slug')]
names.append((self.title, self.get_absolute_url()))
if html_links:
<a href="{{ picture.get_absolute_url }}">
{% endif %}
{% if picture.image_file %}
- <img src="{% thumbnail picture.image_file "139x193" crop="center" as thumb %}{{ thumb.url }}{% empty %}{{ picture.image_file.url }}{% endthumbnail %}" alt="{{ author_str }} – {{ picture.title }}" class="cover" />
+ <img src="{% thumbnail picture.image_file "139x193" crop="center" as thumb %}{{ thumb.url }}{% empty %}{{ picture.image_file.url }}{% endthumbnail %}" alt="{{ author }} – {{ picture.title }}" class="cover" />
{% endif %}
<div class="desc">
- <span class="mono author">{{ author_str }}</span>
+ <span class="mono author">{{ author }}</span>
<span class="title">{{ picture.title }}</span>
</div>
{% if with_link %}
@ssi_included
def picture_mini(request, pk, with_link=True):
picture = get_object_or_404(Picture, pk=pk)
- author_str = ", ".join(tag.name for tag in picture.tags.filter(category='author'))
return render(request, 'picture/picture_mini_box.html', {
'picture': picture,
- 'author_str': author_str,
+ 'author': picture.author_unicode(),
'with_link': with_link,
})
# pass
# FIXME: find this theme and books properly.
- if Fragment.objects.count():
+ if Fragment.objects.exists():
while True:
ctx['theme'] = Tag.objects.filter(category='theme').order_by('?')[:1][0]
- tf = Fragment.tagged.with_any([ctx['theme']]).order_by('?')[:100]
+ tf = Fragment.tagged.with_any([ctx['theme']]).select_related('book').order_by('?')[:100]
if not tf:
continue
ctx['theme_fragment'] = tf[0]