option to turn off ssify just for api + some optimizations
authorJan Szejko <jan.szejko@gmail.com>
Thu, 7 Apr 2016 16:22:35 +0000 (18:22 +0200)
committerJan Szejko <jan.szejko@gmail.com>
Thu, 7 Apr 2016 16:22:35 +0000 (18:22 +0200)
src/api/emitters.py
src/api/handlers.py
src/catalogue/helpers.py
src/catalogue/tests/book_import.py
src/catalogue/tests/tags.py
src/social/forms.py

index 40cc717..36babb2 100644 (file)
@@ -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):
index 0dc9cd6..b81afda 100644 (file)
@@ -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
 
 
index 38e2a87..e476079 100644 (file)
@@ -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:
index e5d5b0c..a445268 100644 (file)
@@ -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 """
index 8d651a1..d5aa72c 100644 (file)
@@ -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])
 
index 3891858..b3be6eb 100755 (executable)
@@ -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):