Skip to content
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

Merged
merged 2 commits into from Aug 11, 2019

Conversation

bob1de
Copy link
Contributor

@bob1de bob1de commented Jul 22, 2019

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.

@bob1de
Copy link
Contributor Author

bob1de commented Jul 22, 2019

Ah, and it depends on #99 being merged first.

@bob1de
Copy link
Contributor Author

bob1de commented Jul 23, 2019

I've got an equivalent mixin for viewsets in django-rest-framework with support for custom actions as well, BTW.

@bob1de bob1de force-pushed the auto-view-mixin branch 2 times, most recently from 27bd4e4 to 80e3dd3 Compare July 23, 2019 10:48
@highpost
Copy link

Even better. That's exactly my use case. Thanks so much for this work.

@bob1de
Copy link
Contributor Author

bob1de commented Jul 24, 2019

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.

@highpost
Copy link

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.

@bob1de
Copy link
Contributor Author

bob1de commented Jul 24, 2019

@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:

git+git://github.com/efficiosoft/django-rules@mix

I'll keep the branch there for the next couple of weeks.

@dfunckt
Copy link
Owner

dfunckt commented Aug 1, 2019

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.

@bob1de
Copy link
Contributor Author

bob1de commented Aug 3, 2019

Hi @dfunckt
Hm, I could of course construct a mock object trying to simulate the DRF viewset, but that would add another component which we'd need to make sure behaves like the real DRF, so I think I'm against this approach. DRF is relatively small and has no dependencies apart from Django itself, hence I'm for simply adding it to the testing dependencies.

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?

@dfunckt
Copy link
Owner

dfunckt commented Aug 3, 2019

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?

@bob1de
Copy link
Contributor Author

bob1de commented Aug 4, 2019

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.

@bob1de
Copy link
Contributor Author

bob1de commented Aug 4, 2019

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.

@bob1de
Copy link
Contributor Author

bob1de commented Aug 5, 2019

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.

@bob1de
Copy link
Contributor Author

bob1de commented Aug 5, 2019

The tests require #99 being merged to pass. I added tests there as well.

@bob1de bob1de force-pushed the auto-view-mixin branch 3 times, most recently from 39406a0 to 98d7d54 Compare August 5, 2019 20:26
@bob1de
Copy link
Contributor Author

bob1de commented Aug 5, 2019

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 :)

@bob1de
Copy link
Contributor Author

bob1de commented Aug 5, 2019

@highpost Please use the auto-view-mixin branch for testing, it now also contains the changes from #99 and I've dropped the mix branch.

@bob1de bob1de force-pushed the auto-view-mixin branch 2 times, most recently from ba45bc1 to 2b1cae3 Compare August 6, 2019 07:55
@bob1de
Copy link
Contributor Author

bob1de commented Aug 6, 2019

@dfunckt Docs and tests complete, commits squashed

Copy link
Owner

@dfunckt dfunckt left a 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.

README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
rules/contrib/models.py Outdated Show resolved Hide resolved
rules/contrib/models.py Outdated Show resolved Hide resolved
rules/contrib/rest_framework.py Outdated Show resolved Hide resolved
rules/contrib/views.py Outdated Show resolved Hide resolved
rules/contrib/views.py Outdated Show resolved Hide resolved
@bob1de
Copy link
Contributor Author

bob1de commented Aug 7, 2019

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 :)

@bob1de bob1de force-pushed the auto-view-mixin branch 2 times, most recently from 7e19c7b to 955145d Compare August 8, 2019 10:11
@bob1de
Copy link
Contributor Author

bob1de commented Aug 8, 2019

Ok @dfunckt

Then all review comments are resolved now and we can finally merge :)

@dfunckt dfunckt merged commit e1da97e into dfunckt:master Aug 11, 2019
@bob1de bob1de deleted the auto-view-mixin branch August 11, 2019 13:24
@bob1de
Copy link
Contributor Author

bob1de commented Aug 11, 2019

Thanks @dfunckt for merging. Have you an idea when this will land in a release?

@bob1de
Copy link
Contributor Author

bob1de commented Aug 11, 2019

Oh, already there, great! ;)

@highpost highpost mentioned this pull request Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants