from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
-from django.db.models import Prefetch
from django.utils.functional import lazy
from piston.handler import AnonymousBaseHandler, BaseHandler
from piston.utils import rc
from catalogue.forms import BookImportForm
from catalogue.models import Book, Tag, BookMedia, Fragment, Collection
-from catalogue.models.tag import TagRelation
+from catalogue.models.tag import prefetch_relations
from picture.models import Picture
from picture.forms import PictureImportForm
books = books.only('slug', 'title', 'cover', 'cover_thumb')
for category in book_tag_categories:
- books = books.prefetch_related(
- Prefetch(
- 'tag_relations',
- queryset=TagRelation.objects.filter(tag__category=category)
- .select_related('tag').only('tag__name_pl', 'object_id'),
- to_attr='%s_relations' % category))
-
+ books = prefetch_relations(books, category)
if books:
return books
else:
def _tag_getter(category):
@classmethod
def get_tag(cls, book):
- if hasattr(book, '%s_relations' % category):
- return ', '.join(rel.tag.name for rel in getattr(book, '%s_relations' % category))
- return ', '.join(book.tags.filter(category=category).values_list('name', flat=True))
+ return book.tag_unicode(category)
return get_tag
from catalogue.fields import EbookField
from catalogue.models import Tag, Fragment, BookMedia
from catalogue.utils import create_zip, gallery_url, gallery_path
+from catalogue.models.tag import prefetched_relations
from catalogue import app_settings
from catalogue import tasks
from wolnelektury.utils import makedirs
def authors(self):
return self.tags.filter(category='author')
+ def tag_unicode(self, category):
+ relations = prefetched_relations(self, category)
+ if relations:
+ return ', '.join(rel.tag.name for rel in relations)
+ else:
+ return ', '.join(self.tags.filter(category=category).values_list('name', flat=True))
+
def author_unicode(self):
- return ", ".join(self.authors().values_list('name', flat=True))
+ return self.tag_unicode('author')
def save(self, force_insert=False, force_update=False, **kwargs):
from sortify import sortify
from django.contrib.auth.models import User
from django.db import models
from django.db.models import permalink
+from django.db.models.query import Prefetch
from django.dispatch import Signal
from django.utils.translation import ugettext_lazy as _
# Pickle complains about not having this.
TagRelation = Tag.intermediary_table_model
+
+
+def prefetch_relations(objects, category, only_name=True):
+ queryset = TagRelation.objects.filter(tag__category=category).select_related('tag')
+ if only_name:
+ queryset = queryset.only('tag__name_pl', 'object_id')
+ return objects.prefetch_related(
+ Prefetch('tag_relations', queryset=queryset, to_attr='%s_relations' % category))
+
+
+def prefetched_relations(obj, category):
+ if hasattr(obj, '%s_relations' % category):
+ return getattr(obj, '%s_relations' % category)
+ else:
+ return None
other = other.filter(items__content_type=ct).distinct()
if list_type == 'audiobooks':
other = other.filter(id__in=get_audiobook_tags())
+ other = other.only('name', 'slug', 'category')
else:
other = []
# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-from collections import defaultdict
import hashlib
import os.path
import random
import re
import time
from base64 import urlsafe_b64encode
-
-from django.http import HttpResponse
-from django.core.files.uploadedfile import UploadedFile
-from django.core.files.storage import DefaultStorage
-from django.utils.encoding import force_unicode
-from django.conf import settings
-from os import mkdir, path, unlink
+from collections import defaultdict
from errno import EEXIST, ENOENT
from fcntl import flock, LOCK_EX
+from os import mkdir, path, unlink
from zipfile import ZipFile
+from django.conf import settings
+from django.core.files.storage import DefaultStorage
+from django.core.files.uploadedfile import UploadedFile
+from django.http import HttpResponse
+from django.utils.encoding import force_unicode
+
from reporting.utils import read_chunks
# Use the system (hardware-based) random number generator if it exists.
from django.shortcuts import render_to_response, get_object_or_404, render, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect, JsonResponse
from django.core.urlresolvers import reverse
-from django.db.models import Q
+from django.db.models import Q, QuerySet
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.http import urlquote_plus
from django.utils import translation
from catalogue.helpers import get_top_level_related_tags
from catalogue.models import Book, Collection, Tag, Fragment
from catalogue.utils import split_tags
+from catalogue.models.tag import prefetch_relations
staff_required = user_passes_test(lambda user: user.is_staff)
else:
fragments = Fragment.objects.filter(book__in=objects)
related_tag_lists.append(
- Tag.objects.usage_for_queryset(fragments, counts=True).filter(category='theme').exclude(pk__in=tag_ids))
+ Tag.objects.usage_for_queryset(fragments, counts=True).filter(category='theme').exclude(pk__in=tag_ids)
+ .only('name', 'sort_key', 'category', 'slug'))
+ if isinstance(objects, QuerySet):
+ objects = prefetch_relations(objects, 'author')
categories = split_tags(*related_tag_lists)
from django.utils.datastructures import SortedDict
from fnpdjango.utils.text.slughifi import slughifi
from ssify import flush_ssi_includes
+
+from catalogue.models.tag import prefetched_relations
from picture import tasks
from StringIO import StringIO
import jsonfield
return self.tags.filter(category='author')
def tag_unicode(self, category):
- return ", ".join(self.tags.filter(category=category).values_list('name', flat=True))
+ relations = prefetched_relations(self, category)
+ if relations:
+ return ', '.join(rel.tag.name for rel in relations)
+ else:
+ return ', '.join(self.tags.filter(category=category).values_list('name', flat=True))
def author_unicode(self):
return self.tag_unicode('author')