Skip to content

Commit

Permalink
fix: pass correct request param for relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz authored and David Vogt committed Jan 8, 2024
1 parent 25eed91 commit 6954d41
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion generic_permissions/visibilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def validate(self, *args, **kwargs):
# Find the relations which the request can include.
queryset = getattr(self.instance, key).all()
for handler in VisibilitiesConfig.get_handlers(queryset.model):
queryset = handler(queryset, self.context)
queryset = handler(queryset, self.context["request"])

# Add remaining relations which can not be included in the request.
validated_data[key] += getattr(self.instance, key).exclude(pk__in=queryset)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ packages = [
]

[tool.poetry.dependencies]
python = ">=3.8.1, <3.13"
python = ">=3.8.1"
django = ">=3.2"
djangorestframework = "^3.14"

Expand Down
4 changes: 4 additions & 0 deletions tests/test_visibilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse
from rest_framework import serializers
from rest_framework.request import Request
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND

from generic_permissions.config import VisibilitiesConfig
Expand Down Expand Up @@ -29,6 +30,7 @@ def test_visibility(
class TestVisibility:
@filter_queryset_for(Model1)
def filter_queryset_for_document(self, queryset, request):
assert isinstance(request, Request)
if request.user.username != "admin":
return queryset.none()
return queryset.exclude(text="bar")
Expand Down Expand Up @@ -220,6 +222,7 @@ def test_visibility_relation(db, admin_user, admin_client, filter_relation):
class TestVisibility:
@filter_queryset_for(Model2)
def filter_queryset_for_document(self, queryset, request):
assert isinstance(request, Request)
if filter_relation:
return queryset.exclude(text="apple")
return queryset
Expand Down Expand Up @@ -257,6 +260,7 @@ def test_visibility_relation_patch(db, admin_user, admin_client):
class TestVisibility:
@filter_queryset_for(Model2)
def filter_queryset_for_document(self, queryset, request):
assert isinstance(request, Request)
return queryset.exclude(text="apple")

VisibilitiesConfig.clear_handlers()
Expand Down

0 comments on commit 6954d41

Please sign in to comment.