From: Jan Szejko Date: Thu, 11 Feb 2016 11:41:30 +0000 (+0100) Subject: #8 fixed tag lists for audiobooks X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/396d3bd5f45d384a1bfad490cefd3c2999e0685f #8 fixed tag lists for audiobooks --- diff --git a/src/catalogue/helpers.py b/src/catalogue/helpers.py index 38e2a87a1..bb7fa6821 100644 --- a/src/catalogue/helpers.py +++ b/src/catalogue/helpers.py @@ -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 diff --git a/src/catalogue/templatetags/catalogue_tags.py b/src/catalogue/templatetags/catalogue_tags.py index fa6436feb..105306e19 100644 --- a/src/catalogue/templatetags/catalogue_tags.py +++ b/src/catalogue/templatetags/catalogue_tags.py @@ -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 = [] diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 8f1ca2f00..e2d449c30 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -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',