Merge pull request #3345 from paperless-ngx/fix/issue-3341

This commit is contained in:
shamoon 2023-05-08 07:04:49 -07:00 committed by GitHub
commit 5b6956ff24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -3551,9 +3551,28 @@ class TestApiAuth(DirectoriesMixin, APITestCase):
)
def test_dynamic_permissions_fields(self):
Document.objects.create(title="Test", content="content 1", checksum="1")
user1 = User.objects.create_user(username="user1")
user1.user_permissions.add(*Permission.objects.filter(codename="view_document"))
user2 = User.objects.create_user(username="user2")
Document.objects.create(title="Test", content="content 1", checksum="1")
doc2 = Document.objects.create(
title="Test2",
content="content 2",
checksum="2",
owner=user2,
)
doc3 = Document.objects.create(
title="Test3",
content="content 3",
checksum="3",
owner=user2,
)
assign_perm("view_document", user1, doc2)
assign_perm("view_document", user1, doc3)
assign_perm("change_document", user1, doc3)
user1 = User.objects.create_superuser(username="test1")
self.client.force_authenticate(user1)
response = self.client.get(
@ -3567,6 +3586,9 @@ class TestApiAuth(DirectoriesMixin, APITestCase):
self.assertNotIn("permissions", resp_data["results"][0])
self.assertIn("user_can_change", resp_data["results"][0])
self.assertEqual(resp_data["results"][0]["user_can_change"], True) # doc1
self.assertEqual(resp_data["results"][1]["user_can_change"], False) # doc2
self.assertEqual(resp_data["results"][2]["user_can_change"], True) # doc3
response = self.client.get(
"/api/documents/?full_perms=true",

View File

@ -270,11 +270,9 @@ class DocumentViewSet(
return Document.objects.distinct().annotate(num_notes=Count("notes"))
def get_serializer(self, *args, **kwargs):
super().get_serializer(*args, **kwargs)
fields_param = self.request.query_params.get("fields", None)
fields = fields_param.split(",") if fields_param else None
truncate_content = self.request.query_params.get("truncate_content", "False")
serializer_class = self.get_serializer_class()
kwargs.setdefault("context", self.get_serializer_context())
kwargs.setdefault("fields", fields)
kwargs.setdefault("truncate_content", truncate_content.lower() in ["true", "1"])
@ -282,7 +280,7 @@ class DocumentViewSet(
"full_perms",
self.request.query_params.get("full_perms", False),
)
return serializer_class(*args, **kwargs)
return super().get_serializer(*args, **kwargs)
def update(self, request, *args, **kwargs):
response = super().update(request, *args, **kwargs)