X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/a791ae7c71b918ca39e7083209019de03a164252..bf99d8859b9b576381cde347bd8abd4544f09d38:/src/catalogue/api/helpers.py diff --git a/src/catalogue/api/helpers.py b/src/catalogue/api/helpers.py new file mode 100644 index 000000000..d872dde17 --- /dev/null +++ b/src/catalogue/api/helpers.py @@ -0,0 +1,22 @@ +from django.db.models import Q +from catalogue.models import Book + + +def books_after(books, after, new_api): + if not new_api: + return books.filter(slug__gt=after) + try: + author, title, book_id = after.split(Book.SORT_KEY_SEP) + except ValueError: + return Book.objects.none() + return books.filter(Q(sort_key_author__gt=author) + | (Q(sort_key_author=author) & Q(sort_key__gt=title)) + | (Q(sort_key_author=author) & Q(sort_key=title) & Q(id__gt=int(book_id)))) + + +def order_books(books, new_api): + if new_api: + return books.order_by('sort_key_author', 'sort_key', 'id') + else: + return books.order_by('slug') +