-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
APIv3 CRUD for Redirect objects #5879
Conversation
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.
I know this is a work in progress but it is looking pretty good to me.
One thing that we should probably handle is that the queryset is not using select_related
. Perhaps we could have a get_queryset
method like this:
def get_queryset(self):
qs = super().get_queryset()
qs = qs.select_related('project')
return qs
@@ -29,3 +29,13 @@ def has_permission(self, request, view): | |||
return True | |||
|
|||
return False | |||
|
|||
|
|||
class IsProjectAdmin(BasePermission): |
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.
This permission seems to be generating something like 6 extra queries which was surprising. However, it is not generating N queries where N is the number of redirects.
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.
Well, this is the basic permission check that we need in all the endpoints for user admin of a project. I split it here to make it simpler and have it isolated.
So, if this check is slow, we should take care of it since it's used a on most of the endpoints.
Basically, it does:
- get a Project (from slug in the URL)
- performs a
Project.objects.filter(users__in=[user])
I think we can use .only('id')
in ProjectQuerySetMixin.has_admin_permission
in the if, to avoid bringing the full objects since we only want to make a simple check.
In my very simple local tests, there is a timing difference. I'll add a commit for this.
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.
Also, some of these method are called more than once (at least for the BrowsableAPIRenderer) and returned value won't change in the same request, so we could do something there if we care enough.
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.
Overall it probably isn't a big deal either way. Some number of queries is necessary for the permission check but 6 just seemed high. I do wonder if we shouldn't just cache the project object onto the view object since most of our views relate to projects.
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.
I think we can optimize it later if needed --I think it's a good option. Although, I don't want to introduce a "buggy cached object" at this point before knowing that everything is working as we expect :)
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.
I'm ok with that for now. Let's keep it in mind though.
This reduces the numbers of queries in N, where N is the number of Redirect objects returned: 💯 --at least in a simple test, but makes sense. |
This PR is ready for review. The same pattern followed for this Redirect resource/objects will be followed for other ones as well, like |
/api/v3/projects/<project_slug>/redirects/
RedirectQuerySet
to follow the.api
method pattern.public
,.private
, etc are not needed because they do not make sense (considering the previous item)redirects/managers.py
toredirects/querysets.py
to be consistent with the other appspermission_classes
, like after the refactor at Make APIv3 permission classes modular #5858.only
in one of the methods that just checks forin