From da0924eb41a74776b2a8b2afc3fec08134f56430 Mon Sep 17 00:00:00 2001 From: Francisco Penedo Alvarez Date: Tue, 31 Mar 2026 13:31:50 +0200 Subject: [PATCH] Handle genres and authors casing --- src/hxbooks/cli.py | 6 +++--- src/hxbooks/library.py | 14 ++++++++------ src/hxbooks/templates/book/delete_confirm.html.j2 | 2 +- src/hxbooks/templates/components/book_card.html.j2 | 4 ++-- src/hxbooks/templates/components/book_form.html.j2 | 8 ++++---- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/hxbooks/cli.py b/src/hxbooks/cli.py index 09ca815..2cd8c20 100644 --- a/src/hxbooks/cli.py +++ b/src/hxbooks/cli.py @@ -380,7 +380,7 @@ def list_books( click.echo("-" * 75) for book in books: - authors_str = ", ".join(a.name for a in book.authors)[:22] + authors_str = ", ".join(a.name.title() for a in book.authors)[:22] if len(authors_str) == 22: authors_str += "..." owner_str = book.owner.username if book.owner else "" @@ -459,7 +459,7 @@ def search_books( click.echo("-" * 72) for book in books: - authors_str = ", ".join(a.name for a in book.authors)[:27] + authors_str = ", ".join(a.name.title() for a in book.authors)[:27] if len(authors_str) == 27: authors_str += "..." click.echo(f"{book.id:<4} {book.title[:32]:<35} {authors_str:<30}") @@ -502,7 +502,7 @@ def import_book( fetch_cover=not no_cover, ) click.echo( - f"Imported book: {book.title} by {', '.join(a.name for a in book.authors)} (ID: {book.id})" + f"Imported book: {book.title} by {', '.join(a.name.title() for a in book.authors)} (ID: {book.id})" ) except Exception as e: click.echo(f"Error importing book: {e}", err=True) diff --git a/src/hxbooks/library.py b/src/hxbooks/library.py index 70e3cb7..5389708 100644 --- a/src/hxbooks/library.py +++ b/src/hxbooks/library.py @@ -645,13 +645,14 @@ def get_books_by_location( def _get_or_create_author(name: str) -> Author: - """Get existing author or create a new one.""" + """Get existing author or create a new one. Always store as lowercase.""" + normalized = name.strip().lower() author = db.session.execute( - select(Author).filter(Author.name == name) + select(Author).filter(Author.name == normalized) ).scalar_one_or_none() if author is None: - author = Author(name=name) + author = Author(name=normalized) db.session.add(author) # Don't commit here - let the caller handle the transaction @@ -659,13 +660,14 @@ def _get_or_create_author(name: str) -> Author: def _get_or_create_genre(name: str) -> Genre: - """Get existing genre or create a new one.""" + """Get existing genre or create a new one. Always store as lowercase.""" + normalized = name.strip().lower() genre = db.session.execute( - select(Genre).filter(Genre.name == name) + select(Genre).filter(Genre.name == normalized) ).scalar_one_or_none() if genre is None: - genre = Genre(name=name) + genre = Genre(name=normalized) db.session.add(genre) # Don't commit here - let the caller handle the transaction diff --git a/src/hxbooks/templates/book/delete_confirm.html.j2 b/src/hxbooks/templates/book/delete_confirm.html.j2 index 12fd735..069caf4 100644 --- a/src/hxbooks/templates/book/delete_confirm.html.j2 +++ b/src/hxbooks/templates/book/delete_confirm.html.j2 @@ -22,7 +22,7 @@
{{ book.title }}
{% if book.authors %} -

by {{ book.authors | join(', ') }}

+

by {{ book.authors | map('title') | join(', ') }}

{% endif %} {% if book.isbn %}

ISBN: {{ book.isbn }}

diff --git a/src/hxbooks/templates/components/book_card.html.j2 b/src/hxbooks/templates/components/book_card.html.j2 index 14edc22..f612f04 100644 --- a/src/hxbooks/templates/components/book_card.html.j2 +++ b/src/hxbooks/templates/components/book_card.html.j2 @@ -45,7 +45,7 @@ {% if book.authors %}

- by {{ book.authors | join(', ') }} + by {{ book.authors | map('title') | join(', ') }}

{% endif %} @@ -63,7 +63,7 @@ {% if book.genres %}
{% for genre in book.genres[:2] %} - {{ genre }} + {{ genre|title }} {% endfor %} {% if book.genres|length > 2 %} +{{ book.genres|length - 2 }} diff --git a/src/hxbooks/templates/components/book_form.html.j2 b/src/hxbooks/templates/components/book_form.html.j2 index 6a3ec6e..6c827fb 100644 --- a/src/hxbooks/templates/components/book_form.html.j2 +++ b/src/hxbooks/templates/components/book_form.html.j2 @@ -21,12 +21,12 @@
+ placeholder="One author per line">{% if book and book.authors %}{{ book.authors | map('title') | join('\n') }}{% endif %}
+ placeholder="One genre per line">{% if book and book.genres %}{{ book.genres | map('title') | join('\n') }}{% endif %}
@@ -155,12 +155,12 @@ }; new Tagify(document.querySelector('#genres'), { ...commmon_settings, - whitelist: {{ genres | map(attribute = 'name') | list | pprint }}, + whitelist: {{ genres | map(attribute = 'name') | map('title') | list | pprint }}, dropdown: { enabled: 0, closeOnSelect: false } }); new Tagify(document.querySelector('#authors'), { ...commmon_settings, - whitelist: {{ authors | map(attribute = 'name') | list | pprint }}, + whitelist: {{ authors | map(attribute = 'name') | map('title') | list | pprint }}, dropdown: { enabled: 0, closeOnSelect: false } }); \ No newline at end of file