Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop processing validators after fail (#13) #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions menu_generator/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ 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):
"""
Expand Down
8 changes: 8 additions & 0 deletions menu_generator/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ def test_menu_multiple_validators(self):
self.menu.save_user_state(self.request)
self.assertFalse(self.menu._is_validated(menu_dict))

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))

def test_generate_menu_submenu_attribute_inheritance(self):
self.request.user = TestUser(staff=True, authenticated=True, happy=True)
self.menu.save_user_state(self.request)
Expand Down