From: Radek Czajka Date: Wed, 19 Sep 2012 08:00:36 +0000 (+0200) Subject: Random optimizations. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/c6db46d42f0a6b9dbd5abb6ce2be58fe306752b6?ds=inline Random optimizations. --- diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 6a31f4bff..ea85645fa 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -86,6 +86,11 @@ class Book(models.Model): 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 @@ -482,19 +487,19 @@ class Book(models.Model): 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 = ['%s' % (tag.get_absolute_url(), tag.name) for tag in names] + names = ['%s' % (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 diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index 353d567bb..3c4509dda 100644 --- a/apps/catalogue/models/tag.py +++ b/apps/catalogue/models/tag.py @@ -71,6 +71,13 @@ class Tag(TagBase): 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') diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index ad60962c5..92827c10d 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -4,6 +4,7 @@ # import datetime import feedparser +from random import randint from django.conf import settings from django import template @@ -388,9 +389,14 @@ def related_books(book, limit=6, random=1): 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, } @@ -407,9 +413,7 @@ def catalogue_menu(): @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 diff --git a/apps/social/templatetags/social_tags.py b/apps/social/templatetags/social_tags.py index 889160200..d6d3f7110 100755 --- a/apps/social/templatetags/social_tags.py +++ b/apps/social/templatetags/social_tags.py @@ -2,6 +2,7 @@ # 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 @@ -29,7 +30,16 @@ def choose_cite(context, ctx=None): 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