Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions services/app/api/library_groups.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Collection-only author and series shelves, grouped before pagination."""

import re
from typing import Literal
from uuid import UUID

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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)))
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/test_library_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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