Skip to content
Merged
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
13 changes: 10 additions & 3 deletions src/opds_server/db/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ async def get_author_name(author_id: int, config: Config) -> str:
row = await cursor.fetchone()
if not row:
raise HTTPException(status_code=404, detail="Author not found")
return row[0]
# Calibre escapes commas within author names as pipes in storage.
return row[0].replace("|", ",")


async def add_authors(books: list, config: Config) -> dict[int, dict]:
Expand All @@ -222,7 +223,9 @@ async def add_authors(books: list, config: Config) -> dict[int, dict]:
book_ids,
) as cursor:
async for book_id, author_id, name in cursor:
authors_by_book[book_id].append({"id": author_id, "name": name})
authors_by_book[book_id].append(
{"id": author_id, "name": name.replace("|", ",")}
)

result = {}
for book_id, title, last_modified in books:
Expand Down Expand Up @@ -332,7 +335,11 @@ async def get_authors(page: int, config: Config) -> tuple[list, bool, bool]:
has_next = len(authors) > limit
has_previous = offset > 0 and bool(authors)

return authors[:limit], has_previous, has_next
return (
[(author_id, name.replace("|", ",")) for author_id, name in authors[:limit]],
has_previous,
has_next,
)


async def get_author_books(
Expand Down
38 changes: 38 additions & 0 deletions tests/test_catalog_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,44 @@ def test_author_feed_omits_navigation_beyond_last_page(catalog_client):
assert not links(beyond, "previous") and not links(beyond, "next")


@pytest.mark.parametrize(
"stored_name",
["Asimov| Isaac", "Анисимов| В. И.| & <Соавтор>", "Ada & Sons"],
)
def test_calibre_author_names_decode_commas_without_changing_storage(
client_factory, stored_name
):
"""Decode every escaped comma across feeds without splitting authors."""
library, client = client_factory(page_size=100)
with sqlite3.connect(library / "metadata.db") as connection:
connection.execute("UPDATE authors SET name = ? WHERE id = 1", (stored_name,))

expected_name = stored_name.replace("|", ",")
listing = parse_atom(client.get("/opds/by-author"))
assert entries(listing)[0].findtext("atom:title", namespaces=NS) == expected_name
detail = parse_atom(client.get("/opds/author/1"))
assert detail.findtext("atom:title", namespaces=NS) == f"Books by {expected_name}"
for endpoint in (
"/opds/author/1",
"/opds/by-title",
"/opds/by-newest",
"/opds/search?q=Practical",
):
feed = parse_atom(client.get(endpoint))
book = next(
entry
for entry in entries(feed)
if entry.findtext("atom:title", namespaces=NS) == "A <Practical> Book"
)
assert book.findtext("atom:author/atom:name", namespaces=NS) == expected_name
assert book.findtext("atom:author/atom:uri", namespaces=NS) == "/opds/author/1"

with sqlite3.connect(library / "metadata.db") as connection:
assert connection.execute(
"SELECT name FROM authors WHERE id = 1"
).fetchone() == (stored_name,)


def test_author_navigation_detail_and_authorless_books(catalog_client):
"""Follow author links and keep authorless or multiply-authored books
readable."""
Expand Down
Loading