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()
SHELF_NAMES = {
"all": _("All books"),
"to-read": _("To Read"),
"reading": _("Currently Reading"),
"read": _("Read"),
}
@register.filter(name="is_book_on_shelf")
def get_is_book_on_shelf(book, shelf):
"""is a book on a shelf"""
@ -42,15 +50,11 @@ def get_translated_shelf_name(shelf):
return ""
# support obj or dict
identifier = shelf["identifier"] if isinstance(shelf, dict) else shelf.identifier
if identifier == "all":
return _("All books")
if identifier == "to-read":
return _("To Read")
if identifier == "reading":
return _("Currently Reading")
if identifier == "read":
return _("Read")
return shelf["name"] if isinstance(shelf, dict) else shelf.name
try:
return SHELF_NAMES[identifier]
except KeyError:
return shelf["name"] if isinstance(shelf, dict) else shelf.name
@register.simple_tag(takes_context=True)