diff --git a/services/app/api/library_groups.py b/services/app/api/library_groups.py index 7640ed8d..86a45876 100644 --- a/services/app/api/library_groups.py +++ b/services/app/api/library_groups.py @@ -1,5 +1,6 @@ """Collection-only author and series shelves, grouped before pagination.""" +import re from typing import Literal from uuid import UUID @@ -40,6 +41,28 @@ def normalized(value): return func.lower(func.trim(func.regexp_replace(value, r"\s+", " ", "g"))) +# One shelf whatever the punctuation: "Discworld: Industrial Revolution" and +# "Discworld - Industrial Revolution". Series also drop a leading "The" and a +# trailing "Series", so "The Silver Ships" and "Silver Ships Series" meet. +SERIES_SUFFIX = r"^(.+?) series$" + + +def group_key(kind, value): + key = func.lower(value) + key = func.regexp_replace(key, "&", " and ", "g") + key = func.trim(func.regexp_replace(key, "[^[:alnum:]]+", " ", "g")) + if kind == "series": + key = func.regexp_replace(func.regexp_replace(key, "^the ", ""), SERIES_SUFFIX, r"\1") + return key + + +def name_key(kind, name): + key = re.sub(r"[\W_]+", " ", name.lower().replace("&", " and ")).strip() + if kind == "series": + key = re.sub(SERIES_SUFFIX, r"\1", re.sub(r"^the ", "", key)) + return key + + def array(value): return case((func.jsonb_typeof(value) == "array", value), else_=cast(literal("[]"), JSONB)) @@ -120,10 +143,10 @@ def group_rows(user, kind, medium, library_id): select( raw.c.work_id, func.trim(raw.c.name).label("name"), - normalized(raw.c.name).label("key"), + group_key(kind, raw.c.name).label("key"), raw.c.external_id, ) - .where(normalized(raw.c.name) != "") + .where(group_key(kind, raw.c.name) != "") .distinct() .subquery() ) @@ -151,7 +174,12 @@ async def groups( ).label("external_id"), ).group_by(rows.c.key) if q.strip(): - grouped = grouped.where(rows.c.key.contains(" ".join(q.lower().split()), autoescape=True)) + # Match any spelling in the group, but count the whole group. + grouped = grouped.having( + func.bool_or( + normalized(rows.c.name).contains(" ".join(q.lower().split()), autoescape=True) + ) + ) grouped = grouped.subquery() total = await db.scalar(select(func.count()).select_from(grouped)) selected = ( @@ -240,7 +268,7 @@ async def group_books( limit: int = Query(default=40, ge=1, le=100), ): rows = group_rows(user, kind, medium, library_id) - ids = select(rows.c.work_id).where(rows.c.key == " ".join(name.lower().split())).distinct() + ids = select(rows.c.work_id).where(rows.c.key == name_key(kind, name)).distinct() query = select(Work).where(Work.id.in_(ids)) total = await db.scalar(select(func.count()).select_from(query.subquery())) books = list(await db.scalars(query.order_by(Work.title, Work.id).offset(offset).limit(limit))) diff --git a/tests/integration/test_library_groups.py b/tests/integration/test_library_groups.py index d50d425a..5413dd4c 100644 --- a/tests/integration/test_library_groups.py +++ b/tests/integration/test_library_groups.py @@ -96,3 +96,41 @@ async def test_groups_and_book_details_respect_library_grants(client, admin, dat async def test_groups_require_login(client): assert (await client.get("/api/library/groups/authors")).status_code == 401 + + +async def test_names_differing_only_in_punctuation_the_or_series_share_one_shelf( + client, admin, database +): + async with database() as db, db.begin(): + for title, series, author in [ + ("Dead Man Walking", "Dan Deadman Space Detective", "J.R.R. Writer"), + ("Dead Man Talking", "Dan Deadman Space Detective Series", "J. R. R. Writer"), + ("Raising Steam", "Discworld: Industrial Revolution", "Library writer"), + ("Going Postal", "Discworld - Industrial Revolution", "Library writer"), + ("Mutiny", "The Silver Ships", "Library writer"), + ("Liberation", "Silver Ships", "Library writer"), + ("Origin", "Dan Deadman", "Library writer"), + ]: + work, _, _ = await add(db, title, metadata_snapshot={"series": [{"name": series}]}) + work.authors = [author] + shelves = (await browse(client, "series"))["items"] + assert [(s["name"], s["book_count"]) for s in shelves] == [ + ("Dan Deadman", 1), + ("Dan Deadman Space Detective", 2), + ("Discworld - Industrial Revolution", 2), + ("Silver Ships", 2), + ] + found = await browse(client, "series", q="detective series") + assert [(s["name"], s["book_count"]) for s in found["items"]] == [ + ("Dan Deadman Space Detective", 2) + ] + for name in ("Discworld: Industrial Revolution", "Discworld - Industrial Revolution"): + books = await client.get("/api/library/groups/series/books", params={"name": name}) + assert books.json()["total"] == 2 + authors = (await browse(client))["items"] + assert [(a["name"], a["book_count"]) for a in authors] == [ + ("J. R. R. Writer", 2), + ("Library writer", 5), + ] + books = await client.get("/api/library/groups/authors/books", params={"name": "J.R.R. Writer"}) + assert books.json()["total"] == 2