Skip to content

Commit

Permalink
mapping property can be a callable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirek Simek committed Jun 1, 2018
1 parent b869979 commit dfbd4a9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions rest_delegated_permissions/permissions.py
Expand Up @@ -99,7 +99,8 @@ def __init__(self, rest_permissions, *delegated_fields, mapping=None, delegated_
:param rest_permissions:
:param delegated_fields:
:param mapping:
:param mapping: either a string (all actions will be mapped to this string), dict or
callable taking a request and view and returning action name
:param delegated_objects_getter:
:param allowed_safe_actions: If set, these actions will be granted initial access for the processing.
Later filtering is then used for estimating if user has permissions.
Expand All @@ -119,7 +120,7 @@ def _internal_has_permission(self, request, view, obj):
if not delegated_obj:
continue
delegated_permissions = self.rest_permissions.permissions_for_model(delegated_obj)
delegated_action = self._get_delegated_action(view.action)
delegated_action = self._get_delegated_action(request, view)
delegated_view = DelegatedPermission.DelegatedView(
self.rest_permissions, type(delegated_obj), request, delegated_action)

Expand All @@ -132,8 +133,11 @@ def has_permission(self, request, view):
return True
return self._internal_has_permission(request, view, None)

def _get_delegated_action(self, action):
def _get_delegated_action(self, request, view):
action = view.action
if self.mapping:
if callable(self.mapping):
return self.mapping(request, view)
if isinstance(self.mapping, str):
action = self.mapping
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -34,7 +34,7 @@ class InvoiceViewSet(ModelViewSet):

setup(
name='django-rest-delegated-permissions',
version='1.1.3',
version='1.1.4',
packages=[
'rest_delegated_permissions',
],
Expand Down

0 comments on commit dfbd4a9

Please sign in to comment.