-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow permission classes to customize the failure message. #2539
Conversation
change detail to message and update text
@@ -280,6 +282,8 @@ def check_permissions(self, request): | |||
""" | |||
for permission in self.get_permissions(): | |||
if not permission.has_permission(request, self): | |||
if hasattr(permission, 'message'): | |||
self.permission_denied(request, permission.message) | |||
self.permission_denied(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we simplify this to self.permission_denied(request, message=getattr(permission, 'message', None))
?
Thanks. Both suggestions worked and helped to simplify the code. I also made a small change to the custom message used in the tests. |
@tomchristie This seems like a good and simple addition, but perhaps it should go on 3.1. Thoughts? |
@jpadilla : Still pending a decision, but it doesn't matter that the pull request is against master - we're not planning on any further 3.0 releases so we'd be fine to just merge both this and the 3.1 branch to master. |
Given that we don't do this uniformly throughout other exception classes let's not just do this in a single place. |
The validator API uses the same convention. At the moment it's very brittle to create permissions with custom messages, so it would be worth thinking about other ways we could address this. The most natural, elegant solutions involve some kind of metaprogramming. |
Have you considered an approach where an exception takes a permission as a parameter? At the moment default_details are implemented on both the permission and exception. |
Reopening for later review. |
Sure. |
add message to custom permission
No description provided.