More views in DRF.
[wolnelektury.git] / src / catalogue / api / helpers.py
1 from django.db.models import Q
2 from catalogue.models import Book
3
4
5 def books_after(books, after, new_api):
6     if not new_api:
7         return books.filter(slug__gt=after)
8     try:
9         author, title, book_id = after.split(Book.SORT_KEY_SEP)
10     except ValueError:
11         return Book.objects.none()
12     return books.filter(Q(sort_key_author__gt=author)
13                         | (Q(sort_key_author=author) & Q(sort_key__gt=title))
14                         | (Q(sort_key_author=author) & Q(sort_key=title) & Q(id__gt=int(book_id))))
15
16
17 def order_books(books, new_api):
18     if new_api:
19         return books.order_by('sort_key_author', 'sort_key', 'id')
20     else:
21         return books.order_by('slug')
22