Random optimizations.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 19 Sep 2012 08:00:36 +0000 (10:00 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 19 Sep 2012 08:00:36 +0000 (10:00 +0200)
apps/catalogue/models/book.py
apps/catalogue/models/tag.py
apps/catalogue/templatetags/catalogue_tags.py
apps/social/templatetags/social_tags.py

index 6a31f4b..ea85645 100644 (file)
@@ -86,6 +86,11 @@ class Book(models.Model):
     def get_absolute_url(self):
         return ('catalogue.views.book_detail', [self.slug])
 
     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
     @property
     def name(self):
         return self.title
@@ -482,19 +487,19 @@ class Book(models.Model):
 
     def pretty_title(self, html_links=False):
         book = self
 
     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:
 
         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:
         else:
-            names = [tag.name for tag in names]
-
+            names = [tag[0] for tag in names]
         return ', '.join(names)
 
     @classmethod
         return ', '.join(names)
 
     @classmethod
index 353d567..3c4509d 100644 (file)
@@ -71,6 +71,13 @@ class Tag(TagBase):
     def get_absolute_url(self):
         return ('catalogue.views.tagged_object_list', [self.url_chunk])
 
     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')
     def has_description(self):
         return len(self.description) > 0
     has_description.short_description = _('description')
index ad60962..92827c1 100644 (file)
@@ -4,6 +4,7 @@
 #
 import datetime
 import feedparser
 #
 import datetime
 import feedparser
+from random import randint
 
 from django.conf import settings
 from django import template
 
 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:
                     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,
     }
     return {
         'books': related,
     }
@@ -407,9 +413,7 @@ def catalogue_menu():
 
 @register.simple_tag
 def tag_url(category, slug):
 
 @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
 
 
 @register.simple_tag
index 8891602..d6d3f71 100755 (executable)
@@ -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.
 #
 # 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
 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)
                 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
 
 
     return cite