Skip to content

Commit

Permalink
Fixed behavior of ForUserFlexQuery when no user to match the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Schindler committed Aug 8, 2019
1 parent fa7e3dd commit 59dfef9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions django_flexquery/contrib/for_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class ForUserFlexQuery(FlexQuery):
a ``django.http.HttpRequest`` or a user object as argument for the custom
function and passing the user through. When no user (or ``None``) is given,
the default behavior is to hide all objects. This can be changed by setting the
``none_if_no_user`` attribute to ``True``.
``all_if_no_user`` attribute to ``True``.
Because it can handle an ``HttpRequest`` directly, instances of this ``FlexQuery``
may also be used in conjunction with the django_filters library as the ``queryset``
parameter of filters.
"""

none_if_no_user = False
all_if_no_user = False

def call_bound(self, user, *args, **kwargs): # pylint: disable=arguments-differ
"""Calls the custom function with a user, followed by the remaining arguments.
Expand All @@ -38,8 +38,8 @@ def call_bound(self, user, *args, **kwargs): # pylint: disable=arguments-differ
except AttributeError:
user = None
if user is None:
if self.none_if_no_user:
return Q(pk__in=self.base.none())
return Q()
if self.all_if_no_user:
return Q()
return Q(pk__in=self.base.none())

return super().call_bound(user, *args, **kwargs)
4 changes: 2 additions & 2 deletions tests/testsuite/contrib/test_for_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_request(self):
self.assertEqual(self.man.fq(self.req).count(), 1)

def test_request_no_user(self):
self.assertEqual(self.man.fq(self.req).count(), 2)
self.assertEqual(self.man.fq(self.req).count(), 0)

def test_no_user_all(self):
self.man.fq.__class__.all_if_no_user = True
self.assertEqual(self.man.fq(None).count(), 2)

def test_no_user_none(self):
self.man.fq.__class__.none_if_no_user = True
self.assertEqual(self.man.fq(None).count(), 0)

0 comments on commit 59dfef9

Please sign in to comment.