Skip to content

Commit

Permalink
Merge branch 'new-field-mixin' of github.com:gianpieropa/drf-access-p…
Browse files Browse the repository at this point in the history
…olicy into new-field-mixin
  • Loading branch information
Gianpiero Papappicco committed Apr 11, 2022
2 parents 1a247ec + 2952a55 commit 5d5cb3c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ drf_access_policy.egg-info
site/
venv.sh
.venv
.coverage
.coverage

9 changes: 3 additions & 6 deletions rest_access_policy/field_access_mixin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from typing import List

from rest_framework.request import Request

from .access_policy import AccessPolicy


Expand All @@ -11,6 +8,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._apply_fields_access()


@property
def access_policy(self) -> AccessPolicy:
meta = getattr(self, "Meta", None)
Expand All @@ -23,7 +21,7 @@ def access_policy(self) -> AccessPolicy:
if not access_policy:
raise Exception("Must set access_policy inside Meta for FieldAccessMixin")

if getattr(access_policy,"scope_fields",None) is None:
if getattr(access_policy, "scope_fields", None) is None:
raise Exception("Must define scope_fields method on access_policy")

return access_policy
Expand All @@ -41,7 +39,6 @@ def _apply_fields_access(self):
fields = self.access_policy.scope_fields(self.request, self.fields, instance=self.instance)
if fields is None:
raise Exception("scope_fields method must return fields variable")

self.fields = self.access_policy.scope_fields(self.request, self.fields, instance=self.instance)



1 change: 1 addition & 0 deletions test_project/testapp/access_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def scope_fields(cls, request, fields: dict, instance=None):
fields["status"].read_only = True
return fields


class LogsAccessPolicy(AccessPolicy):
statements = [
{"principal": "group:admin", "action": "*", "effect": "allow"},
Expand Down

0 comments on commit 5d5cb3c

Please sign in to comment.