Skip to content

Commit

Permalink
Fix set_permissions on model update (#108)
Browse files Browse the repository at this point in the history
* Add permissions/set_permissions mapper

* Add py.typed

* Add tests

---------

Co-authored-by: tb1337 <[email protected]>
  • Loading branch information
tb1337 and tb1337 authored Feb 27, 2024
1 parent 1c47047 commit 6a819f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pypaperless/models/mixins/models/updatable.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""UpdatableMixin for PyPaperless models."""

from copy import deepcopy
from typing import Any

from pypaperless.models.base import PaperlessModelProtocol
from pypaperless.models.utils import object_to_dict_value

from .securable import SecurableMixin


class UpdatableMixin(PaperlessModelProtocol):
"""Provide the `update` method for PyPaperless models."""
Expand Down Expand Up @@ -36,6 +39,16 @@ async def update(self, only_changed: bool = True) -> bool:
self._set_dataclass_fields()
return updated

def _check_permissions_field(self, data: dict) -> None:
"""Check."""
if SecurableMixin not in type(self).__bases__:
return
if not self.has_permissions: # type: ignore[attr-defined]
return
if "permissions" in data:
data["set_permissions"] = deepcopy(data["permissions"])
del data["permissions"]

async def _patch_fields(self) -> bool:
"""Use the http `PATCH` method for updating only changed fields."""
changed = {}
Expand All @@ -48,6 +61,8 @@ async def _patch_fields(self) -> bool:
if len(changed) == 0:
return False

self._check_permissions_field(changed)

self._data = await self._api.request_json(
"patch",
self._api_path,
Expand All @@ -62,6 +77,9 @@ async def _put_fields(self) -> bool:
field.name: object_to_dict_value(getattr(self, field.name))
for field in self._get_dataclass_fields()
}

self._check_permissions_field(data)

self._data = await self._api.request_json(
"put",
self._api_path,
Expand Down
Empty file added pypaperless/py.typed
Empty file.
10 changes: 10 additions & 0 deletions tests/test_paperless_0_0_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ async def test_udpate(self, p: Paperless, mapping: ResourceTestMapping):
to_update.name = new_name
await to_update.update()
assert to_update.name == new_name
# no updates
assert not await to_update.update()
# force update
new_name = f"{to_update.name} again"
to_update.name = new_name
Expand All @@ -184,6 +186,14 @@ async def test_permissions(self, p: Paperless, mapping: ResourceTestMapping):
item = await getattr(p, mapping.resource)(1)
assert not item.has_permissions

async def test_permission_change(self, p: Paperless, mapping: ResourceTestMapping):
"""Test permission changes."""
getattr(p, mapping.resource).request_permissions = True
assert getattr(p, mapping.resource).request_permissions
item = await getattr(p, mapping.resource)(1)
item.permissions.view.users.append(23)
assert await item.update()


@pytest.mark.parametrize(
"mapping",
Expand Down

0 comments on commit 6a819f0

Please sign in to comment.