From a1a45eba2424317346be28ad6e253e9f09d8c9cf Mon Sep 17 00:00:00 2001 From: DIFEZI Tchakoli Chissou Date: Sat, 13 Jul 2024 18:26:30 +0000 Subject: [PATCH] Stop processing validators after fail [15](https://github.com/LaLogiaDePython/django-menu-generator/pull/15) --- menu_generator/menu.py | 18 ++++++++---------- menu_generator/tests/test_menu.py | 8 ++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/menu_generator/menu.py b/menu_generator/menu.py index 5e1015c..6fb219e 100755 --- a/menu_generator/menu.py +++ b/menu_generator/menu.py @@ -39,20 +39,18 @@ def _is_validated(self, item_dict): if not isinstance(validators, (list, tuple)): raise ImproperlyConfigured("validators must be a list") - result_validations = [] for validator in validators: if isinstance(validator, tuple): - if len(validator) <= 1: + func_or_path, *args = validator + if not args: raise ImproperlyConfigured("You are passing a tuple validator without args %s" % str(validator)) - func = get_callable(validator[0]) - # Using a python slice to get all items after the first to build function args - args = validator[1:] - # Pass the request as first arg by default - result_validations.append(func(self.request, *args)) else: - func = get_callable(validator) - result_validations.append(func(self.request)) # pragma: no cover - return all(result_validations) + func_or_path, args = validator, [] + + func = get_callable(func_or_path) + if not func(self.request, *args): + return False + return True def _has_attr(self, item_dict, attr): """ diff --git a/menu_generator/tests/test_menu.py b/menu_generator/tests/test_menu.py index bbfb897..2ccc957 100755 --- a/menu_generator/tests/test_menu.py +++ b/menu_generator/tests/test_menu.py @@ -362,3 +362,11 @@ def test_subsequent_requests(self): nav2 = get_menu(ctx, 'NAV_MENU') # Both menus should be equal self.assertEqual(nav1, nav2) + + def test_menu_multiple_validators_skip_on_false(self): + menu_dict = { + "validators": ["menu_generator.validators.is_staff", "this-is-never-evaluated"], + } + self.request.user = TestUser(authenticated=True) + self.menu.save_user_state(self.request) + self.assertFalse(self.menu._is_validated(menu_dict))