-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Schindler
committed
Aug 8, 2019
1 parent
0ed3e53
commit 3dd4c82
Showing
7 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
FlexQuery variant that filters the base QuerySet for a given user. | ||
""" | ||
|
||
from django.http import HttpRequest | ||
|
||
from ..flexquery import FlexQuery | ||
from ..q import Q | ||
|
||
|
||
__all__ = ["ForUserFlexQuery"] | ||
|
||
|
||
class ForUserFlexQuery(FlexQuery): | ||
""" | ||
This is a slightly modified ``FlexQuery`` implementation, accepting either | ||
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``. | ||
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 | ||
|
||
def call_bound(self, user, *args, **kwargs): # pylint: disable=arguments-differ | ||
"""Calls the custom function with a user, followed by the remaining arguments. | ||
:param user: User to filter the queryset for | ||
:type user: django.contrib.auth.models.User | django.http.HttpRequest | None | ||
:returns Q: | ||
""" | ||
try: | ||
user = user.user if isinstance(user, HttpRequest) else user | ||
except AttributeError: | ||
user = None | ||
if user is None: | ||
if self.none_if_no_user: | ||
return Q(pk__in=self.base.none()) | ||
return Q() | ||
|
||
return super().call_bound(user, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.contrib.auth.models import AnonymousUser | ||
from django.test import RequestFactory, TestCase | ||
|
||
from django_flexquery import * | ||
from django_flexquery.contrib.for_user import * | ||
from django_flexquery_tests.models import * | ||
|
||
|
||
class ForUserFlexQueryTestCase(TestCase): | ||
def setUp(self): | ||
class Man(Manager): | ||
@ForUserFlexQuery.from_q | ||
def fq(base, user): | ||
assert isinstance(user, AnonymousUser) | ||
return Q(a=42) | ||
|
||
self.man = Man() | ||
self.man.model = AModel | ||
self.req = RequestFactory().get("/") | ||
|
||
AModel.objects.create(a=24) | ||
AModel.objects.create(a=42) | ||
|
||
def test_user(self): | ||
self.assertEqual(self.man.fq(AnonymousUser()).count(), 1) | ||
|
||
def test_request(self): | ||
self.req.user = AnonymousUser() | ||
self.assertEqual(self.man.fq(self.req).count(), 1) | ||
|
||
def test_request_no_user(self): | ||
self.assertEqual(self.man.fq(self.req).count(), 2) | ||
|
||
def test_no_user_all(self): | ||
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) |