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
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):
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
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
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:
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 """
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])
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):