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
CBV mixin for checking permissions automatically based on view type #100
Conversation
Ah, and it depends on #99 being merged first. |
I've got an equivalent mixin for viewsets in django-rest-framework with support for custom actions as well, BTW. |
27bd4e4
to
80e3dd3
Compare
Even better. That's exactly my use case. Thanks so much for this work. |
The view mixin has already been tested and used in action, but not the rest part. Would be great if you could share your experience with it. |
I'll do that since I'm working on a DRF view using Rules for permission handling. It may take a few days to get to the point of testing. But before that I'll look at your code. Thanks again. |
@highpost Great, thanks. If you need a branch to test against which has both the model and viewset additions, you can use this line in requirements.txt for testing:
I'll keep the branch there for the next couple of weeks. |
Hey @efficiosoft, this is all much appreciated. From a quick look, the code looks solid, will probably only have to comment on a couple of typos when I sit down to properly review. My only concern is that I will have no idea if/how this works unless I invest significant effort into (re-)learning DRF -- I haven't used it for years -- and about how I'm going to maintain this code going forward. Any ideas for adding tests for this code? I'd probably feel even better if testing it didn't have to bring in DRF as a dependency. Can the semantics of DRF be mocked into a small class perhaps so we can focus on the code's expected behaviour? Edit: I'm obviously talking about the ViewSet mixin, the CBV looks great as it is, though that should part probably have some tests as well. |
Hi @dfunckt I'll see if I can get some tests done next week, but have no idea how to cleanly simulate the full view invocation with a request yet. If I get this done for the CBV mixin, it should be doable for the DRF mixin as well. What do you think? |
That sounds reasonable @efficiosoft. I suggested mocking DRF to avoid introducing the dependency for one, but also not having to deal with mocking a full request, thinking you could construct something that just meets the mixin’s requirements. Can the django test client be used to test both CBV and DRF mixins? Doesn’t DRF have something equivalent? |
DRF viewsets are just a bunch of regular Django views for which URLs are generated automatically by a router. So I expect to be able to use whatever Django provides for testing views for DRF, too. |
I just checked and yes, there is a convenient-to-use request factory for generating test requests for DRF. I'm going to write the tests within the next days. |
Hi @dfunckt Tests for the CBV mixin are ready. Are you ok with them? If so, I'd create similar ones for the DRF mixin. |
The tests require #99 being merged to pass. I added tests there as well. |
39406a0
to
98d7d54
Compare
Ok, just did it for the DRF mixin as well. The tests might seem a bit clumsy because I had to put guards in place to not run them under Python 2 everywhere, but this will go away :) |
ba45bc1
to
2b1cae3
Compare
@dfunckt Docs and tests complete, commits squashed |
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.
That’s amazing @efficiosoft ! Left a few comments, nothing significant.
Hi @dfunckt Thanks a lot for the thorough review! I applied some of your suggestions and commented on others which I think should not be changed. Can't await seeing this used in production :) |
7e19c7b
to
955145d
Compare
Ok @dfunckt Then all review comments are resolved now and we can finally merge :) |
Thanks @dfunckt for merging. Have you an idea when this will land in a release? |
Oh, already there, great! ;) |
As mentioned in #99 already, here is a possible implementation of the CBV mixin. It's rather short and simply extends
PermissionRequiredMixin
.While I already use a similar thing in production, I rewrote this one completely to be more generic, so don't expect it to be 100% complete. Docs are still missing as well.