From b0f90d05f24c997d326ce625c69337e383eeb8c8 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Thu, 6 Apr 2023 15:55:11 +0200 Subject: [PATCH] Move the shelf names to a dict instead of a chain of if statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- bookwyrm/templatetags/shelf_tags.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/bookwyrm/templatetags/shelf_tags.py b/bookwyrm/templatetags/shelf_tags.py index 1fb799883..ea093b8a5 100644 --- a/bookwyrm/templatetags/shelf_tags.py +++ b/bookwyrm/templatetags/shelf_tags.py @@ -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)