def get_absolute_url(self):
return ('catalogue.views.book_detail', [self.slug])
+ @staticmethod
+ @permalink
+ def create_url(slug):
+ return ('catalogue.views.book_detail', [slug])
+
@property
def name(self):
return self.title
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))
+ rel_info = book.related_info()
+ names = [(name, Tag.create_url('author', slug))
+ for name, slug in rel_info['tags']['author']]
+ if 'parents' in rel_info:
+ books = [(name, Book.create_url(slug))
+ for name, slug in rel_info['parents']]
+ names.extend(reversed(books))
+ names.append((self.title, self.get_absolute_url()))
if html_links:
- names = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
+ names = ['<a href="%s">%s</a>' % (tag[1], tag[0]) for tag in names]
else:
- names = [tag.name for tag in names]
-
+ names = [tag[0] for tag in names]
return ', '.join(names)
@classmethod
def get_absolute_url(self):
return ('catalogue.views.tagged_object_list', [self.url_chunk])
+ @classmethod
+ @permalink
+ def create_url(cls, category, slug):
+ return ('catalogue.views.tagged_object_list', [
+ '/'.join((cls.categories_dict[category], slug))
+ ])
+
def has_description(self):
return len(self.description) > 0
has_description.short_description = _('description')
#
import datetime
import feedparser
+from random import randint
from django.conf import settings
from django import template
ignore_by_tag=book.book_tag())[:limit-random]
cache.set(cache_key, related, 1800)
if random:
- related += list(Book.objects.exclude(
- pk__in=[b.pk for b in related] + [book.pk]
- ).order_by('?')[:random])
+ random_books = Book.objects.exclude(
+ pk__in=[b.pk for b in related] + [book.pk])
+ if random == 1:
+ count = random_books.count()
+ if count:
+ related.append(random_books[randint(0, count - 1)])
+ else:
+ related += list(random_books.order_by('?')[:random])
return {
'books': related,
}
@register.simple_tag
def tag_url(category, slug):
- return reverse('catalogue.views.tagged_object_list', args=[
- '/'.join((Tag.categories_dict[category], slug))
- ])
+ return Tag.create_url(category, slug)
@register.simple_tag
# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
+from random import randint
from django import template
from catalogue.models import Book
from social.models import Cite
cites = cites_for_tags([ctx.book_tag()])
else:
cites = cites_for_tags(ctx)
- cite = cites.order_by('-sticky', '?')[0] if cites.exists() else None
+ stickies = cites.filter(sticky=True)
+ count = stickies.count()
+ if count:
+ cite = stickies[randint(0, count - 1)]
+ else:
+ count = cites.count()
+ if count:
+ cite = cites[randint(0, count - 1)]
+ else:
+ cite = None
return cite