Move the shelf names to a dict instead of a chain of if statements

The main reason to do this is that if we try to add another name then
pylint will complain that there are too many return statements. It might
be slightly faster too.

If I understand correctly it doesn’t matter that the _ function is being
called at module load time because it is mapped to gettext_lazy so the
actual translation will be done when the string is used.
This commit is contained in:
Neil Roberts 2023-04-06 15:55:11 +02:00
parent 36c14655ec
commit b0f90d05f2

View file

@ -9,6 +9,14 @@ from bookwyrm.utils import cache
register = template.Library() register = template.Library()
SHELF_NAMES = {
"all": _("All books"),
"to-read": _("To Read"),
"reading": _("Currently Reading"),
"read": _("Read"),
}
@register.filter(name="is_book_on_shelf") @register.filter(name="is_book_on_shelf")
def get_is_book_on_shelf(book, shelf): def get_is_book_on_shelf(book, shelf):
"""is a book on a shelf""" """is a book on a shelf"""
@ -42,14 +50,10 @@ def get_translated_shelf_name(shelf):
return "" return ""
# support obj or dict # support obj or dict
identifier = shelf["identifier"] if isinstance(shelf, dict) else shelf.identifier identifier = shelf["identifier"] if isinstance(shelf, dict) else shelf.identifier
if identifier == "all":
return _("All books") try:
if identifier == "to-read": return SHELF_NAMES[identifier]
return _("To Read") except KeyError:
if identifier == "reading":
return _("Currently Reading")
if identifier == "read":
return _("Read")
return shelf["name"] if isinstance(shelf, dict) else shelf.name return shelf["name"] if isinstance(shelf, dict) else shelf.name