-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
APIv3 CRUD for Redirect objects #5879
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f9302c
Follow same pattern of .api method for RedirectQuerySet as well
humitos 04e675f
APIv3 CRUD for Redirect objects
humitos 89d2cb0
Split Redirect serializer to allow sanitize fields
humitos 37663c5
Small queryset optimization (select_related)
humitos e1e92d3
Small optimization on base has_admin_permission check
humitos f9346d3
Include 'redirects' in '_links' responses
humitos 1dacb63
Add some tests for Redirect endpoints
humitos ba2dfe2
Test for PUTi and DELETE methods
humitos 213db7f
Rename `redirect_type` to `type` in the APIv3 response
humitos ba61d0c
Small lint fix
humitos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Queryset for the redirects app.""" | ||
|
||
from django.db import models | ||
|
||
from readthedocs.core.utils.extend import SettingsOverrideObject | ||
from readthedocs.projects import constants | ||
|
||
|
||
class RedirectQuerySetBase(models.QuerySet): | ||
|
||
"""Redirects take into account their own privacy_level setting.""" | ||
|
||
use_for_related_fields = True | ||
|
||
def _add_user_repos(self, queryset, user): | ||
if user.is_authenticated: | ||
projects_pk = user.projects.all().values_list('pk', flat=True) | ||
user_queryset = self.filter(project__in=projects_pk) | ||
queryset = user_queryset | queryset | ||
return queryset.distinct() | ||
|
||
def api(self, user=None, detail=True): | ||
queryset = self.none() | ||
if user: | ||
queryset = self._add_user_repos(queryset, user) | ||
return queryset | ||
|
||
def get_redirect_path_with_status(self, path, language=None, version_slug=None): | ||
for redirect in self.select_related('project'): | ||
new_path = redirect.get_redirect_path( | ||
path=path, | ||
language=language, | ||
version_slug=version_slug, | ||
) | ||
if new_path: | ||
return new_path, redirect.http_status | ||
return (None, None) | ||
|
||
|
||
class RedirectQuerySet(SettingsOverrideObject): | ||
_default_class = RedirectQuerySetBase |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Uh oh!
There was an error while loading. Please reload this page.
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:
Project.objects.filter(users__in=[user])
I think we can use
.only('id')
inProjectQuerySetMixin.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.