diff --git a/api/video/wishlist.py b/api/video/wishlist.py index 487d6278..87f56f95 100644 --- a/api/video/wishlist.py +++ b/api/video/wishlist.py @@ -39,7 +39,7 @@ def register_routes(bp): kind = request.args.get("kind") if kind in _KINDS: res = db.query_wishlist( - kind, search=request.args.get("search", ""), + kind, search=request.args.get("search", ""), sort=request.args.get("sort", "added"), page=request.args.get("page", 1), limit=request.args.get("limit", 60)) return jsonify({"success": True, "kind": kind, "counts": counts, **res}) return jsonify({"success": True, "counts": counts}) diff --git a/database/video_database.py b/database/video_database.py index f2111f9a..4708e22d 100644 --- a/database/video_database.py +++ b/database/video_database.py @@ -1578,9 +1578,10 @@ class VideoDatabase: finally: conn.close() - def query_wishlist(self, kind: str, *, search=None, page=1, limit=60) -> dict: + def query_wishlist(self, kind: str, *, search=None, sort="added", page=1, limit=60) -> dict: """One paged slice of the wishlist. kind='movie' → movie cards; kind='show' - → shows grouped show→season→episode with wanted/done roll-ups. {items, + → shows grouped show→season→episode with wanted/done roll-ups. ``sort`` ∈ + added | title | wanted (wanted = most episodes, shows only). {items, pagination} like the other paged queries.""" try: page = max(1, int(page or 1)) @@ -1595,10 +1596,12 @@ class VideoDatabase: if s: where.append("title LIKE ? COLLATE NOCASE"); args.append("%" + s + "%") wsql = " WHERE " + " AND ".join(where) + order = {"title": "title COLLATE NOCASE", + "added": "date_added DESC, id DESC"}.get(sort, "date_added DESC, id DESC") total = conn.execute("SELECT COUNT(*) c FROM video_wishlist" + wsql, args).fetchone()["c"] rows = conn.execute( "SELECT tmdb_id, title, poster_url, year, status, library_id, date_added " - "FROM video_wishlist" + wsql + " ORDER BY date_added DESC, id DESC LIMIT ? OFFSET ?", + "FROM video_wishlist" + wsql + " ORDER BY " + order + " LIMIT ? OFFSET ?", args + [limit, (page - 1) * limit]).fetchall() items = [{"kind": "movie", "tmdb_id": r["tmdb_id"], "title": r["title"], "poster_url": r["poster_url"], "year": r["year"], "status": r["status"], @@ -1610,13 +1613,15 @@ class VideoDatabase: wsql = " WHERE " + " AND ".join(where) total = conn.execute( "SELECT COUNT(DISTINCT tmdb_id) c FROM video_wishlist" + wsql, args).fetchone()["c"] + order = {"title": "title COLLATE NOCASE", "wanted": "wanted DESC, last_added DESC", + "added": "last_added DESC"}.get(sort, "last_added DESC") show_rows = conn.execute( "SELECT tmdb_id, MAX(title) AS title, MAX(poster_url) AS poster_url, " "MAX(library_id) AS library_id, COUNT(*) AS wanted, " "SUM(CASE WHEN status='downloaded' THEN 1 ELSE 0 END) AS done, " "MAX(date_added) AS last_added " "FROM video_wishlist" + wsql + - " GROUP BY tmdb_id ORDER BY last_added DESC LIMIT ? OFFSET ?", + " GROUP BY tmdb_id ORDER BY " + order + " LIMIT ? OFFSET ?", args + [limit, (page - 1) * limit]).fetchall() items = [] for sr in show_rows: diff --git a/tests/test_video_database.py b/tests/test_video_database.py index b63c26de..8fd7a8e8 100644 --- a/tests/test_video_database.py +++ b/tests/test_video_database.py @@ -930,3 +930,18 @@ def test_wishlist_keys_for_shows(db): keys = db.wishlist_keys_for_shows([1396, 1399, 9999]) assert keys[1396] == {"1_1", "2_3"} and keys[1399] == {"1_1"} and 9999 not in keys assert db.wishlist_keys_for_shows([]) == {} + + +def test_wishlist_query_sort(db): + db.add_episodes_to_wishlist(1, "Alpha", [{"season_number": 1, "episode_number": 1}]) # 1 ep + db.add_episodes_to_wishlist(2, "Zeta", [{"season_number": 1, "episode_number": i} for i in range(5)]) # 5 eps + # most-wanted first + w = [s["title"] for s in db.query_wishlist("show", sort="wanted")["items"]] + assert w[0] == "Zeta" + # A–Z + az = [s["title"] for s in db.query_wishlist("show", sort="title")["items"]] + assert az == ["Alpha", "Zeta"] + # movies A–Z + db.add_movie_to_wishlist(10, "Banana"); db.add_movie_to_wishlist(11, "Apple") + m = [x["title"] for x in db.query_wishlist("movie", sort="title")["items"]] + assert m == ["Apple", "Banana"] diff --git a/webui/index.html b/webui/index.html index 4e7fee96..56e5c3f5 100644 --- a/webui/index.html +++ b/webui/index.html @@ -1054,7 +1054,7 @@
Movies & episodes you want to grab
+Movies & episodes you want to grab