# 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
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
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
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
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 = []
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)
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',