def tag_dict(tag, fields=None): all_fields = ("name", "category", "sort_key", "description", "gazeta_link", "wiki_link", "url", "books") if fields: fields = (f for f in fields if f in all_fields) else: fields = all_fields obj = {} for field in fields: if field == "url": obj[field] = tag.get_absolute_url() elif field == "books": obj[field] = [b.id for b in Book.tagged_top_level([tag]).iterator()] elif field == "sort_key": obj[field] = tag.sort_key else: f = getattr(tag, field) if f: obj[field] = f obj["id"] = tag.id return obj
def read(self, request, tags, top_level=False, audiobooks=False, daisy=False): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories and slugs, i.e.: authors/an-author/epoch/an-epoch/ :param top_level: if True and a book is included in the results, it's children are aren't. By default all books matching the tags are returned. """ try: tags = read_tags(tags, allowed=book_tag_categories) except ValueError: return rc.NOT_FOUND if tags: if top_level: books = Book.tagged_top_level(tags) return books if books else rc.NOT_FOUND else: books = Book.tagged.with_all(tags) else: books = Book.objects.all() if top_level: books = books.filter(parent=None) if audiobooks: books = books.filter(media__type="mp3").distinct() if daisy: books = books.filter(media__type="daisy").distinct() if books.exists(): return books else: return rc.NOT_FOUND
def tag_dict(tag, fields=None): all_fields = ('name', 'category', 'sort_key', 'description', 'gazeta_link', 'wiki_link', 'url', 'books', ) if fields: fields = (f for f in fields if f in all_fields) else: fields = all_fields obj = {} for field in fields: if field == 'url': obj[field] = tag.get_absolute_url() elif field == 'books': obj[field] = [b.id for b in Book.tagged_top_level([tag]).iterator()] elif field == 'sort_key': obj[field] = tag.sort_key else: f = getattr(tag, field) if f: obj[field] = f obj['id'] = tag.id return obj
def add_tag(db, tag): books = Book.tagged_top_level([tag]) book_ids = ','.join(str(book_id) for book_id in books.values_list('id', flat=True)) db.execute(tag_sql, { 'category': categories[tag.category], 'name': tag.name, 'sort_key': tag.sort_key, 'book_ids': book_ids, })
def add_tag(db, tag): id = tag.id category = categories[tag.category] name = tag.name sort_key = tag.sort_key books = Book.tagged_top_level([tag]) book_ids = ','.join(str(b.id) for b in books) db.execute(tag_sql, locals())
def add_tag(db, tag): id = tag.id # category = categories[tag.category] # localized names here? category = tag.category name = tag.name sort_key = tag.sort_key books = Book.tagged_top_level([tag]) book_ids = ','.join(str(b.id) for b in books) db.execute(tag_sql, locals())
def add_tag(db, tag): books = Book.tagged_top_level([tag]) book_ids = ','.join( str(book_id) for book_id in books.values_list('id', flat=True)) db.execute( tag_sql, { 'category': categories[tag.category], 'name': tag.name, 'sort_key': tag.sort_key, 'book_ids': book_ids, })
def get_queryset(self): try: tags, ancestors = read_tags( self.kwargs.get('tags', ''), self.request, allowed=('author', 'epoch', 'kind', 'genre') ) except ValueError: raise Http404 new_api = self.request.query_params.get('new_api') after = self.request.query_params.get('after', self.kwargs.get('after')) count = self.request.query_params.get('count', self.kwargs.get('count')) if count: try: count = int(count) except TypeError: raise Http404 # Fixme if tags: if self.kwargs.get('top_level'): books = Book.tagged_top_level(tags) if not books: raise Http404 return books else: books = Book.tagged.with_all(tags) else: books = Book.objects.all() books = order_books(books, new_api) if self.kwargs.get('top_level'): books = books.filter(parent=None) if self.kwargs.get('audiobooks'): books = books.filter(media__type='mp3').distinct() if self.kwargs.get('daisy'): books = books.filter(media__type='daisy').distinct() if self.kwargs.get('recommended'): books = books.filter(recommended=True) if self.kwargs.get('newest'): books = books.order_by('-created_at') if after: books = books_after(books, after, new_api) prefetch_relations(books, 'author') prefetch_relations(books, 'genre') prefetch_relations(books, 'kind') prefetch_relations(books, 'epoch') if count: books = books[:count] return books
def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories and slugs, i.e.: authors/an-author/epoch/an-epoch/ :param top_level: if True and a book is included in the results, it's children are aren't. By default all books matching the tags are returned. """ if pk is not None: try: return Book.objects.get(pk=pk) except Book.DoesNotExist: return rc.NOT_FOUND try: tags, _ancestors = read_tags(tags, allowed=book_tag_categories) except ValueError: return rc.NOT_FOUND if tags: if top_level: books = Book.tagged_top_level(tags) return books if books else rc.NOT_FOUND else: books = Book.tagged.with_all(tags) else: books = Book.objects.all() if top_level: books = books.filter(parent=None) if audiobooks: books = books.filter(media__type='mp3').distinct() if daisy: books = books.filter(media__type='daisy').distinct() books = books.only('slug', 'title', 'cover', 'cover_thumb') for category in book_tag_categories: books = prefetch_relations(books, category) if books: return books else: return rc.NOT_FOUND
def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories and slugs, i.e.: authors/an-author/epoch/an-epoch/ :param top_level: if True and a book is included in the results, it's children are aren't. By default all books matching the tags are returned. """ if pk is not None: try: return Book.objects.get(pk=pk) except Book.DoesNotExist: return rc.NOT_FOUND try: tags, _ancestors = read_tags(tags, allowed=book_tag_categories) except ValueError: return rc.NOT_FOUND if tags: if top_level: books = Book.tagged_top_level(tags) return books if books else rc.NOT_FOUND else: books = Book.tagged.with_all(tags) else: books = Book.objects.all() if top_level: books = books.filter(parent=None) if audiobooks: books = books.filter(media__type='mp3').distinct() if daisy: books = books.filter(media__type='daisy').distinct() books = books.only('slug', 'title', 'cover', 'cover_thumb') for category in book_tag_categories: books = prefetch_relations(books, category) if books: return books else: return rc.NOT_FOUND
def tagged_object_list(request, tags, list_type): try: tags = analyse_tags(request, tags) except ResponseInstead as e: return e.response if list_type == 'gallery' and any(tag.category == 'set' for tag in tags): raise Http404 if any(tag.category in ('theme', 'thing') for tag in tags): return theme_list(request, tags, list_type=list_type) if list_type == 'books': books = Book.tagged.with_all(tags) if any(tag.category == 'set' for tag in tags): params = {'objects': books} else: params = { 'objects': Book.tagged_top_level(tags), 'fragments': Fragment.objects.filter(book__in=books), 'related_tags': get_top_level_related_tags(tags), } elif list_type == 'gallery': params = {'objects': Picture.tagged.with_all(tags)} elif list_type == 'audiobooks': audiobooks = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct() params = { 'objects': Book.tagged.with_all(tags, audiobooks), 'extra': { 'daisy': Book.tagged.with_all( tags, audiobooks.filter(media__type='daisy').distinct()), } } else: raise Http404 return object_list(request, tags=tags, list_type=list_type, **params)
def tagged_object_list(request, tags, list_type): try: tags = analyse_tags(request, tags) except ResponseInstead as e: return e.response if is_crawler(request) and len(tags) > 1: return HttpResponseForbidden('address removed from crawling. check robots.txt') if list_type == 'gallery' and any(tag.category == 'set' for tag in tags): raise Http404 if any(tag.category in ('theme', 'thing') for tag in tags): return theme_list(request, tags, list_type=list_type) if list_type == 'books': books = Book.tagged.with_all(tags) if any(tag.category == 'set' for tag in tags): params = {'objects': books} else: params = { 'objects': Book.tagged_top_level(tags), 'fragments': Fragment.objects.filter(book__in=books), 'related_tags': get_top_level_related_tags(tags), } elif list_type == 'gallery': params = {'objects': Picture.tagged.with_all(tags)} elif list_type == 'audiobooks': audiobooks = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct() params = { 'objects': Book.tagged.with_all(tags, audiobooks), 'extra': { 'daisy': Book.tagged.with_all(tags, audiobooks.filter(media__type='daisy').distinct()), } } else: raise Http404 return object_list(request, tags=tags, list_type=list_type, **params)
def read(self, request, tags, top_level=False, audiobooks=False, daisy=False): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories and slugs, i.e.: authors/an-author/epoch/an-epoch/ :param top_level: if True and a book is included in the results, it's children are aren't. By default all books matching the tags are returned. """ try: tags = read_tags(tags, allowed=book_tag_categories) except ValueError: return rc.NOT_FOUND if tags: if top_level: books = Book.tagged_top_level(tags) return books if books else rc.NOT_FOUND else: books = Book.tagged.with_all(tags) else: books = Book.objects.all() if top_level: books = books.filter(parent=None) if audiobooks: books = books.filter(media__type='mp3').distinct() if daisy: books = books.filter(media__type='daisy').distinct() if books.exists(): return books else: return rc.NOT_FOUND
def tag_dict(tag, fields=None): all_fields = ( 'name', 'category', 'sort_key', 'description', 'gazeta_link', 'wiki_link', 'url', 'books', ) if fields: fields = (f for f in fields if f in all_fields) else: fields = all_fields obj = {} for field in fields: if field == 'url': obj[field] = tag.get_absolute_url() elif field == 'books': obj[field] = [ b.id for b in Book.tagged_top_level([tag]).iterator() ] elif field == 'sort_key': obj[field] = tag.sort_key else: f = getattr(tag, field) if f: obj[field] = f obj['id'] = tag.id return obj
def items(self, tag): return Book.tagged_top_level([tag])
def for_set(cls, tag): books = Book.tagged_top_level([tag]) cont_tabs = (cls.get(b) for b in books.iterator()) return reduce(cls.join_conts, cont_tabs)
def items(self, tag): return Book.tagged_top_level([tag])
def for_set(cls, tag): books = Book.tagged_top_level([tag]) cont_tabs = (cls.get(b) for b in books.iterator()) return reduce(cls.join_conts, cont_tabs)