#8 fixed tag lists for audiobooks
authorJan Szejko <j-sz@o2.pl>
Thu, 11 Feb 2016 11:41:30 +0000 (12:41 +0100)
committerJan Szejko <j-sz@o2.pl>
Thu, 11 Feb 2016 11:41:30 +0000 (12:41 +0100)
src/catalogue/helpers.py
src/catalogue/templatetags/catalogue_tags.py
src/catalogue/views.py

index 38e2a87..bb7fa68 100644 (file)
@@ -3,6 +3,9 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
+from django.core.cache import cache
+
 from .models import Tag, Book
 from os.path import getmtime
 import cPickle
@@ -86,3 +89,16 @@ def update_counters():
 
     with open(settings.CATALOGUE_COUNTERS_FILE, 'w') as f:
         cPickle.dump(counters, f)
+
+
+def get_audiobook_tags():
+    audiobook_tag_ids = cache.get('audiobook_tags')
+    if audiobook_tag_ids is None:
+        books_with_audiobook = Book.objects.filter(media__type__in=('mp3', 'ogg'))\
+            .distinct().values_list('pk', flat=True)
+        audiobook_tag_ids = Tag.objects.filter(
+            items__content_type=ContentType.objects.get_for_model(Book),
+            items__object_id__in=list(books_with_audiobook)).distinct().values_list('pk', flat=True)
+        audiobook_tag_ids = list(audiobook_tag_ids)
+        cache.set('audiobook_tags', audiobook_tag_ids)
+    return audiobook_tag_ids
index fa6436f..105306e 100644 (file)
@@ -8,7 +8,6 @@ from django.contrib.contenttypes.models import ContentType
 
 from django.conf import settings
 from django import template
-from django.core.cache import cache
 from django.template import Node, Variable, Template, Context
 from django.core.urlresolvers import reverse
 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
@@ -16,6 +15,8 @@ from django.utils.cache import add_never_cache_headers
 from django.utils.translation import ugettext as _
 
 from ssify import ssi_variable
+
+from catalogue.helpers import get_audiobook_tags
 from catalogue.models import Book, BookMedia, Fragment, Tag, Source
 from catalogue.constants import LICENSES
 from picture.models import Picture
@@ -323,17 +324,7 @@ def tag_list(tags, choices=None, category=None, list_type='default'):
         ct = ContentType.objects.get_for_model(Picture if list_type == 'gallery' else Book)
         other = other.filter(items__content_type=ct).distinct()
         if list_type == 'audiobooks':
-            audiobook_tag_ids = cache.get('audiobook_tags')
-            if audiobook_tag_ids is None:
-                books_with_audiobook = Book.objects.filter(media__type__in=('mp3', 'ogg'))\
-                    .distinct().values_list('pk', flat=True)
-                audiobook_tag_ids = Tag.objects.filter(
-                    items__content_type=ct,
-                    items__object_id__in=list(books_with_audiobook)).distinct().values_list('pk', flat=True)
-                audiobook_tag_ids = list(audiobook_tag_ids)
-                cache.set('audiobook_tags', audiobook_tag_ids)
-
-            other = other.filter(id__in=audiobook_tag_ids)
+            other = other.filter(id__in=get_audiobook_tags())
     else:
         other = []
 
index 8f1ca2f..e2d449c 100644 (file)
@@ -182,30 +182,32 @@ def tagged_object_list(request, tags='', list_type='default'):
                     pk__in=tags_pks),
             )
         else:
-            if tags:
-                all_books = Book.tagged.with_all(tags)
-            else:
-                all_books = Book.objects.filter(parent=None)
-            if shelf_is_set:
+            if audiobooks:
+                all_books = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct()
+                if tags:
+                    all_books = Book.tagged.with_all(tags, all_books)
                 objects = all_books
+                # there's never only the daisy audiobook
+                daisy = objects.filter(media__type='daisy').distinct().order_by('sort_key_author', 'sort_key')
                 related_book_tags = Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
-                    objects = Book.tagged_top_level(tags)
+                    all_books = Book.tagged.with_all(tags)
                 else:
+                    all_books = Book.objects.filter(parent=None)
+                if shelf_is_set:
                     objects = all_books
-                # WTF: was outside if, overwriting value assigned if shelf_is_set
-                related_book_tags = get_top_level_related_tags(tags)
-
-            if audiobooks:
-                if objects != all_books:
-                    all_books = all_books.filter(media__type__in=('mp3', 'ogg')).distinct()
-                    objects = objects.filter(media__type__in=('mp3', 'ogg')).distinct()
+                    related_book_tags = Tag.objects.usage_for_queryset(
+                        objects, counts=True).exclude(
+                        category='set').exclude(pk__in=tags_pks)
                 else:
-                    all_books = objects = objects.filter(media__type__in=('mp3', 'ogg')).distinct()
-                daisy = objects.filter(media__type='daisy').distinct().order_by('sort_key_author', 'sort_key')
+                    if tags:
+                        objects = Book.tagged_top_level(tags)
+                    else:
+                        objects = all_books
+                    related_book_tags = get_top_level_related_tags(tags)
 
             fragments = Fragment.objects.filter(book__in=all_books)
 
@@ -228,7 +230,7 @@ def tagged_object_list(request, tags='', list_type='default'):
         if tag.category in ('theme', 'thing') and (
                 PictureArea.tagged.with_any([tag]).exists() or
                 Picture.tagged.with_any([tag]).exists()):
-            return redirect('tagged_object_list_gallery', raw_tags, permanent=False)
+            return redirect('tagged_object_list_gallery', raw_tags)
 
     return render_to_response(
         'catalogue/tagged_object_list.html',