From 97ca6dd54563a1546d53ffcce90e04fce8b3d7f7 Mon Sep 17 00:00:00 2001 From: Lukas Vinclav Date: Fri, 22 Dec 2023 14:39:58 +0100 Subject: [PATCH] fix: lazy loaded permission callback (#234) --- src/unfold/sites.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/unfold/sites.py b/src/unfold/sites.py index 4d22fe34..62471d32 100644 --- a/src/unfold/sites.py +++ b/src/unfold/sites.py @@ -233,9 +233,8 @@ def _get_is_active(link: str) -> bool: allowed_items = [] for item in group["items"]: - # Permission callbacks + # Skip item if permission check fails if not self._call_permission_callback(item.get("permission"), request): - # Skip item if permission check fails continue item["active"] = False @@ -280,8 +279,8 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]: allowed_items = [] for item in tab["items"]: + # Skip item if permission check fails if not self._call_permission_callback(item.get("permission"), request): - # Skip item if permission check fails continue allowed_items.append(item) @@ -306,14 +305,22 @@ def _get_mode_images( def _call_permission_callback( self, callback: Union[str, Callable, None], request: HttpRequest - ): + ) -> bool: if callback is None: return True if isinstance(callback, str): - callback = import_string(callback) + try: + callback = import_string(callback) + except ImportError: + pass + + if isinstance(callback, str) or isinstance(callback, Callable): + # We are not able to use here "is" because type is lazy loaded function + if lazy(callback)(request) == True: # noqa: E712 + return True - return callback(request) + return False def _get_value( self, instance: Union[str, Callable, None], *args: Any