From: Jan Szejko Date: Thu, 7 Apr 2016 16:22:35 +0000 (+0200) Subject: option to turn off ssify just for api + some optimizations X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/b9dc6590aa12ae1b04e11dbb57684c5630c1e3bd?ds=inline option to turn off ssify just for api + some optimizations --- diff --git a/src/api/emitters.py b/src/api/emitters.py index 40cc71787..36babb2bf 100644 --- a/src/api/emitters.py +++ b/src/api/emitters.py @@ -9,6 +9,7 @@ When outputting a queryset of selected models, instead of returning XML or JSON stanzas, SSI include statements are returned. """ +from django.conf import settings from django.core.urlresolvers import reverse from django.db.models.query import QuerySet from piston.emitters import Emitter, XMLEmitter, JSONEmitter @@ -41,10 +42,11 @@ class SsiQS(object): class SsiEmitterMixin(object): def construct(self): - if isinstance(self.data, QuerySet) and self.data.model in (Book, Fragment, Tag): + ssify_api = getattr(settings, 'SSIFY_API', True) + if ssify_api and isinstance(self.data, QuerySet) and self.data.model in (Book, Fragment, Tag): return SsiQS(self.data) else: - return super(SsiEmitterMixin, self).construct() # WTF + return super(SsiEmitterMixin, self).construct() class SsiJsonEmitter(SsiEmitterMixin, JSONEmitter): diff --git a/src/api/handlers.py b/src/api/handlers.py index 0dc9cd6d6..b81afdaf5 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -206,7 +206,9 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): if daisy: books = books.filter(media__type='daisy').distinct() - if books.exists(): + books = books.only('slug', 'title', 'cover') + + if books: return books else: return rc.NOT_FOUND @@ -249,7 +251,7 @@ def _tags_getter(category): def _tag_getter(category): @classmethod def get_tag(cls, book): - return ', '.join(tag.name for tag in book.tags.filter(category=category)) + return ', '.join(book.tags.filter(category=category).values_list('name', flat=True)) return get_tag diff --git a/src/catalogue/helpers.py b/src/catalogue/helpers.py index 38e2a87a1..e47607928 100644 --- a/src/catalogue/helpers.py +++ b/src/catalogue/helpers.py @@ -57,7 +57,7 @@ def update_counters(): def count_for_book(book, count_by_combination=None, parent_combinations=None): if not parent_combinations: parent_combinations = set() - tags = sorted(tuple(t.pk for t in book.tags.filter(category__in=('author', 'genre', 'epoch', 'kind')))) + tags = sorted(book.tags.filter(category__in=('author', 'genre', 'epoch', 'kind')).values_list('pk', flat=True)) combs = list(combinations(tags)) for c in combs: if c not in parent_combinations: diff --git a/src/catalogue/tests/book_import.py b/src/catalogue/tests/book_import.py index e5d5b0c72..a4452686c 100644 --- a/src/catalogue/tests/book_import.py +++ b/src/catalogue/tests/book_import.py @@ -97,7 +97,7 @@ class BookImportLogicTests(WLTestCase): book = models.Book.from_text_and_meta(ContentFile(book_text), self.book_info) self.assert_([('theme', 'love')], - [(tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme')]) + book.fragments.all()[0].tags.filter(category='theme').values_list('category', 'slug')) def test_book_with_no_theme(self): """ fragments with no themes shouldn't be created at all """ diff --git a/src/catalogue/tests/tags.py b/src/catalogue/tests/tags.py index 8d651a19b..d5aa72c49 100644 --- a/src/catalogue/tests/tags.py +++ b/src/catalogue/tests/tags.py @@ -232,7 +232,7 @@ class TestIdenticalTag(WLTestCase): related_themes = book.related_themes() for category in 'author', 'kind', 'genre', 'epoch': - self.assertTrue('tag' in [tag.slug for tag in book.tags.filter(category=category)], + self.assertTrue('tag' in book.tags.filter(category=category).values_list('slug', flat=True), 'missing related tag for %s' % category) self.assertTrue('tag' in [tag.slug for tag in related_themes]) diff --git a/src/social/forms.py b/src/social/forms.py index 389185889..b3be6ebda 100755 --- a/src/social/forms.py +++ b/src/social/forms.py @@ -27,7 +27,8 @@ class ObjectSetsForm(forms.Form): self._user = user data = kwargs.setdefault('data', {}) if 'tags' not in data and user.is_authenticated(): - data['tags'] = ', '.join(t.name for t in obj.tags.filter(category='set', user=user).iterator() if t.name) + data['tags'] = ', '.join( + obj.tags.filter(category='set', user=user).exclude(name__in=(None, '')).values_list('name', flat=True)) super(ObjectSetsForm, self).__init__(*args, **kwargs) def save(self, request):