From 9d1323dae8ec7be81df09739844fed7b880c7367 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sun, 25 Sep 2022 00:05:36 -0300 Subject: [PATCH] fix: Simplify logic for Panel.enabled property With this change, having a panel status set in the cookies exits earlier, as it isn't needed to retrieve the Django settings and panel name to calculate the default value. --- debug_toolbar/panels/__init__.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/debug_toolbar/panels/__init__.py b/debug_toolbar/panels/__init__.py index ea8ff8e9c..6d9491b1f 100644 --- a/debug_toolbar/panels/__init__.py +++ b/debug_toolbar/panels/__init__.py @@ -20,7 +20,12 @@ def panel_id(self): return self.__class__.__name__ @property - def enabled(self): + def enabled(self) -> bool: + # The user's cookies should override the default value + cookie_value = self.toolbar.request.COOKIES.get("djdt" + self.panel_id) + if cookie_value is not None: + return cookie_value == "on" + # Check to see if settings has a default value for it disabled_panels = dt_settings.get_config()["DISABLE_PANELS"] panel_path = get_name_from_obj(self) @@ -28,16 +33,10 @@ def enabled(self): # panel module, but can be disabled without panel in the path. # For that reason, replace .panel. in the path and check for that # value in the disabled panels as well. - disable_panel = ( - panel_path in disabled_panels - or panel_path.replace(".panel.", ".") in disabled_panels + return ( + panel_path not in disabled_panels + and panel_path.replace(".panel.", ".") not in disabled_panels ) - if disable_panel: - default = "off" - else: - default = "on" - # The user's cookies should override the default value - return self.toolbar.request.COOKIES.get("djdt" + self.panel_id, default) == "on" # Titles and content