-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Search: refactor serializer's context #9624
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
39b23f5
Convert api.py to module api/v2
stsewd 06efed6
Black
stsewd 12b56bd
Fix tests
stsewd 9172433
Search: refactor serializer's context
stsewd e145700
Move paginator to file
stsewd 2858e6e
Black
stsewd a91c929
Use method instead of duplicating logic
stsewd 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,7 @@ | |
from readthedocs.projects.models import Feature, Project | ||
from readthedocs.search.api.v2.serializers import ( | ||
PageSearchSerializer, | ||
ProjectData, | ||
ProjectSearchSerializer, | ||
VersionData, | ||
) | ||
from readthedocs.search.faceted_search import ALL_FACETS, PageSearch, ProjectSearch | ||
|
||
|
@@ -91,26 +89,6 @@ def _get_project(self, project_slug): | |
project = get_object_or_404(queryset, slug=project_slug) | ||
return project | ||
|
||
def _get_project_data(self, project, version_slug): | ||
docs_url = project.get_docs_url(version_slug=version_slug) | ||
version_data = VersionData( | ||
slug=version_slug, | ||
docs_url=docs_url, | ||
) | ||
project_data = { | ||
project.slug: ProjectData( | ||
alias=None, | ||
version=version_data, | ||
) | ||
} | ||
return project_data | ||
|
||
def get_serializer_context(self, project, version_slug): | ||
context = { | ||
'projects_data': self._get_project_data(project, version_slug), | ||
} | ||
return context | ||
|
||
def get(self, request, project_slug): | ||
project_obj = self._get_project(project_slug) | ||
use_advanced_query = not project_obj.has_feature( | ||
|
@@ -132,8 +110,7 @@ def get(self, request, project_slug): | |
use_advanced_query=use_advanced_query, | ||
) | ||
|
||
context = self.get_serializer_context(project_obj, user_input.version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing the context here was saving just one query, and to pass it again we will need to query the version (we have only the slug), so we are just omitting the |
||
results = PageSearchSerializer(results, many=True, context=context).data | ||
results = PageSearchSerializer(results, many=True).data | ||
|
||
template_context = user_input._asdict() | ||
template_context.update({ | ||
|
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.
I think it's better if we pass the
project
and theversion
object, in particular if it's a private method and won't be used from outside. Then we can consume whatever is required from the inner method.The API is easier to use and it's easier to think about while following the code, IMO.
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.
In fact, the similar version of this method (
_get_project_data
) was receiving the objects as I'm suggesting here :)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.
Oh, now I remember why this was changed, I was about to use it in this same class, to avoid duplicating the logic at
readthedocs.org/readthedocs/search/api/v2/serializers.py
Lines 117 to 132 in 2858e6e