More views in DRF.
[wolnelektury.git] / src / catalogue / api / helpers.py
diff --git a/src/catalogue/api/helpers.py b/src/catalogue/api/helpers.py
new file mode 100644 (file)
index 0000000..d872dde
--- /dev/null
@@ -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')
+