Skip to content

Commit

Permalink
Avoid AttributeError in case of None request (jazzband#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkern authored Jan 13, 2022
1 parent 95dc760 commit b22a97e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion waffle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ def is_active_for_user(self, user):
return None

def _is_active_for_user(self, request):
return self.is_active_for_user(request.user)
user = getattr(request, "user", None)
if user:
return self.is_active_for_user(user)
return False

def _is_active_for_language(self, request):
if self.languages:
Expand Down
4 changes: 4 additions & 0 deletions waffle/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ def test_natural_keys(self):
self.assertEqual(get_waffle_flag_model().objects.get_by_natural_key('test-flag'), flag)
self.assertEqual(Switch.objects.get_by_natural_key('test-switch'), switch)
self.assertEqual(Sample.objects.get_by_natural_key('test-sample'), sample)

def test_flag_is_not_active_for_none_requests(self):
flag = get_waffle_flag_model().objects.create(name='test-flag')
self.assertEqual(flag.is_active(None), False)

0 comments on commit b22a97e

Please sign in to comment.