Skip to content

Commit

Permalink
Add initial support for simple DRF Views (non-Viewset)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanpetrea committed Mar 18, 2024
1 parent 40d1299 commit b42dab3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a check for the first field in HasRelatedResourcePerms.relation
- Add uniqueness checks for PermEnum values
- Provide a DRF AuthorizationModelViewSet class for convenience
- Add initial support for simple DRF Views (non-Viewset)

### Changed
- Lax some in-code assumptions to allow using non-Model classes
Expand Down
15 changes: 13 additions & 2 deletions django_woah/drf/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ def has_permission(self, request, view):
return view.http_method_not_allowed(request)

if not hasattr(view, "action"):
return view.permission_denied(request)
if request.method == "POST":
return view.is_authorized_for_unsaved_resource()

return self.has_object_permission(
request,
view,
obj=view.get_authorization_model_object(skip_authorization=True),
)

if getattr(view, "action", None):
if view.action == "list" or (
Expand All @@ -55,7 +62,11 @@ def has_object_permission(self, request, view, obj):
# TODO: see what to do with "obj" parameter
# return obj == view.get_authorization_model_object() breaks some cases

if view.action == "create":
if not hasattr(view, "action"):
if request.method == "POST":
return view.is_authorized_for_unsaved_resource()

elif view.action == "create":
return view.is_authorized_for_unsaved_resource()

obj = view.get_authorization_model_object()
Expand Down

0 comments on commit b42dab3

Please sign in to comment.