Fixes #3211, fixes #3211: related books and pictures bars.
authorRadek Czajka <radekczajka@nowoczesnapolska.org.pl>
Thu, 16 Oct 2014 14:38:29 +0000 (16:38 +0200)
committerRadek Czajka <radekczajka@nowoczesnapolska.org.pl>
Thu, 16 Oct 2014 14:38:29 +0000 (16:38 +0200)
apps/catalogue/__init__.py
apps/catalogue/templates/catalogue/related_books.html
apps/catalogue/templatetags/catalogue_tags.py
apps/catalogue/urls.py
apps/picture/models.py
apps/picture/templates/picture/picture_detail.html
apps/picture/templates/picture/picture_list_thumb.html
apps/picture/templates/picture/picture_mini_box.html
apps/picture/templates/picture/picture_wide.html
apps/picture/templatetags/picture_tags.py
apps/picture/views.py

index f3d44e1..eaeb7d4 100644 (file)
@@ -24,6 +24,7 @@ class Settings(AppSettings):
 
     REDAKCJA_URL = "http://redakcja.wolnelektury.pl"
     GOOD_LICENSES = set([r'CC BY \d\.\d', r'CC BY-SA \d\.\d'])
+    RELATED_RANDOM_PICTURE_CHANCE = .5
 
     def _more_DONT_BUILD(self, value):
         for format_ in ['cover', 'pdf', 'epub', 'mobi', 'fb2', 'txt']:
index 48fb2ee..6407d5a 100755 (executable)
@@ -1,16 +1,25 @@
 {% spaceless %}
 {% load catalogue_random_book from catalogue_tags %}
+{% load picture_random_picture from picture_tags %}
 {% load ssi_include from ssify %}
 
+{% for pic in pics %}
+    {% ssi_include 'picture_mini' pk=pic.pk %}
+{% endfor %}
+
 {% for book in books %}
     {% ssi_include 'catalogue_book_mini' pk=book.pk %}
 {% endfor %}
 
 {% if random %}
-    {% catalogue_random_book random_excluded as random_pk %}
-    {{ random_pk.if }}
-        {% ssi_include 'catalogue_book_mini' pk=random_pk %}
-    {{ random_pk.endif }}
+    {% catalogue_random_book random_excluded_books as random_book_pk %}
+    {% picture_random_picture random_excluded_pics unless=random_book_pk as random_pic_pk %}
+    {{ random_book_pk.if }}
+        {% ssi_include 'catalogue_book_mini' pk=random_book_pk %}
+    {{ random_book_pk.endif }}
+    {{ random_pic_pk.if }}
+        {% ssi_include 'picture_mini' pk=random_pic_pk %}
+    {{ random_pic_pk.endif }}
 {% endif %}
 
 {% endspaceless %}
\ No newline at end of file
index f9c69ea..6e81cae 100644 (file)
@@ -2,7 +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 random import randint, random
 from urlparse import urlparse
 
 from django.conf import settings
@@ -311,18 +311,39 @@ def work_list(context, object_list):
     return locals()
 
 
+# TODO: These are no longer just books.
 @register.inclusion_tag('catalogue/related_books.html', takes_context=True)
-def related_books(context, book, limit=6, random=1, taken=0):
+def related_books(context, instance, limit=6, random=1, taken=0):
     limit = limit - taken
-    related = Book.tagged.related_to(book,
-            Book.objects.exclude(common_slug=book.common_slug)
-            ).exclude(ancestor=book)[:limit-random]
-    random_excluded = [b.pk for b in related] + [book.pk]
+    max_books = limit - random
+    is_picture = isinstance(instance, Picture)
+
+    pics_qs = Picture.objects.all()
+    if is_picture:
+        pics_qs = pics_qs.exclude(pk=instance.pk)
+    pics = Picture.tagged.related_to(instance, pics_qs)
+    if pics.exists():
+        # Reserve one spot for an image.
+        max_books -= 1
+
+    books_qs = Book.objects.all()
+    if not is_picture:
+        books_qs = books_qs.exclude(common_slug=instance.common_slug).exclude(ancestor=instance)
+    books = Book.tagged.related_to(instance, books_qs)[:max_books]
+
+    pics = pics[:1 + max_books - books.count()]
+
+    random_excluded_books = [b.pk for b in books]
+    random_excluded_pics = [p.pk for p in pics]
+    (random_excluded_pics if is_picture else random_excluded_books).append(instance.pk)
+
     return {
         'request': context['request'],
-        'books': related,
+        'books': books,
+        'pics': pics,
         'random': random,
-        'random_excluded': random_excluded,
+        'random_excluded_books': random_excluded_books,
+        'random_excluded_pics': random_excluded_pics,
     }
 
 
@@ -395,6 +416,9 @@ def source_name(url):
 
 @ssi_variable(register, patch_response=[add_never_cache_headers])
 def catalogue_random_book(request, exclude_ids):
+    from .. import app_settings
+    if random() < app_settings.RELATED_RANDOM_PICTURE_CHANCE:
+        return None
     queryset = Book.objects.exclude(pk__in=exclude_ids)
     count = queryset.count()
     if count:
index 05f8766..6e98fb0 100644 (file)
@@ -18,6 +18,7 @@ urlpatterns = patterns('picture.views',
     url(r'^obraz/(?P<slug>%s).html$' % SLUG, 'picture_viewer', name='picture_viewer'),
     url(r'^obraz/(?P<slug>%s)/$' % SLUG, 'picture_detail'),
 
+    url(r'^p/(?P<pk>\d+)/mini\.(?P<lang>.+)\.html', 'picture_mini', name='picture_mini'),
     url(r'^p/(?P<pk>\d+)/short\.(?P<lang>.+)\.html', 'picture_short', name='picture_short'),
     url(r'^pa/(?P<pk>\d+)/short\.(?P<lang>.+)\.html', 'picturearea_short', name='picture_area_short'),
 )
index 120750d..97dbd0e 100644 (file)
@@ -340,6 +340,7 @@ class Picture(models.Model):
             template % (self.pk, lang)
             for template in [
                 '/katalog/p/%d/short.%s.html',
+                '/katalog/p/%d/mini.%s.html',
                 ]
             for lang in languages
             ])
index 51bd6b9..514f8c3 100644 (file)
 
 {% block body %}
   {% picture_wide picture %}
+
+{% spaceless %}
+
+<section class="see-also" style="display: inline-block;">
+<h1>{% trans "See also" %}:</h1>
+{% related_books picture %}
+</section>
+{% endspaceless %}
+
 {% endblock %}
index 4fce98a..4c2933c 100644 (file)
@@ -1,5 +1,6 @@
 {% extends "base.html" %}
 {% load i18n %}
+{% load catalogue_tags %}
 {% load static %}
 {% load ssi_include from ssify %}
 
     </div>
 
     <div class='clearboth'></div>
-<div id="books-list">
-  <ol class="work-list">{% spaceless %}
-{% for picture in book_list %}
-   <li class="Picture-item">
-   {% ssi_include 'picture_short' pk=picture.pk %}
-   </li>
-{% endfor %}
-  {% endspaceless %}</ol>
-</div>
+
+
+
+{% work_list book_list %}
+
 
 
 
index 5df989e..d8ebbf7 100644 (file)
@@ -1,21 +1,23 @@
+{% spaceless %}
 
 {% load thumbnail %}
 <div class="book-mini-box">
-    <a href="{{ book.get_absolute_url }}">
-        {% if picture.cover %}
-            <img src="
-                {% thumbnail book.cover "139x193" as thumb %}
-                    {{ thumb.url }}
-                {% empty %}
-                    {{ book.cover.url }}
-                {% endthumbnail %}
-            " alt="{{ author_str }} – {{ book.title }}" class="cover" />
+    <div class="book-mini-box-inner">
+    {% if with_link %}
+    <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" />
         {% endif %}
         <div class="desc">
             <span class="mono author">{{ author_str }}</span>
-            <span class="title">{{ book.title }}</span>
+            <span class="title">{{ picture.title }}</span>
         </div>
+    {% if with_link %}
     </a>
+    {% endif %}
+    </div>
 </div>
 
 
+{% endspaceless %}
\ No newline at end of file
index 920a9c0..e2fab8c 100644 (file)
@@ -7,7 +7,7 @@
 
 {% block picture-view %}
 <a href="{{ main_link }}">
-{% thumbnail picture.image_file "535" upscale="false" as thumb %}
+{% thumbnail picture.image_file "535" upscale=0 as thumb %}
 <img class="cover" src="{{thumb.url}}"/></a>
 {% endthumbnail %}
 {% endblock %}
index 76ae1de..02d80b0 100644 (file)
@@ -2,12 +2,17 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+import logging
+from random import randint
 from django import template
 from django.core.urlresolvers import reverse
+from django.utils.cache import add_never_cache_headers
+import sorl.thumbnail.default
+from ssify import ssi_variable
 from catalogue.utils import split_tags
 from ..engine import CustomCroppingEngine
-import sorl.thumbnail.default
-import logging
+from ..models import Picture
+
 
 register = template.Library()
 
@@ -56,3 +61,15 @@ def area_thumbnail_url(area, geometry):
     sorl.thumbnail.default.engine = _engine
 
     return th.url
+
+
+@ssi_variable(register, patch_response=[add_never_cache_headers])
+def picture_random_picture(request, exclude_ids, unless=None):
+    if unless:
+        return None
+    queryset = Picture.objects.exclude(pk__in=exclude_ids).exclude(image_file='')
+    count = queryset.count()
+    if count:
+        return queryset[randint(0, count - 1)].pk
+    else:
+        return None
index 4bc2ab0..78700d1 100644 (file)
@@ -88,6 +88,18 @@ def import_picture(request):
         return HttpResponse(_("Error importing file: %r") % import_form.errors)
 
 
+@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,
+        'with_link': with_link,
+    })
+
+
 @ssi_included
 def picture_short(request, pk):
     picture = get_object_or_404(Picture, pk=pk)