Skip to content

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 7 commits into from
Oct 6, 2022

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Sep 26, 2022

We were generating the context of the serializer
from outside, but we can do this from
the serializer itself,
this way we don't have to duplicate this logic.

We have been calling this "projects", but it's a combination of project and version (tuple), not sure about a better name for this.

This is on top of #9616


📚 Documentation previews 📚

@stsewd stsewd requested a review from a team as a code owner September 26, 2022 20:04
@stsewd stsewd requested a review from humitos September 26, 2022 20:04
@@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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 projects parameter here for now (we would use it again when using the new search)

@stsewd stsewd mentioned this pull request Sep 27, 2022
Base automatically changed from move-search-api to main September 29, 2022 00:13
We were generating the context of the serializer
from outside, but we can do this from
the serializer itself,
this way we don't have to duplicate this logic.
@stsewd stsewd force-pushed the refactor-search-serializer-context branch from e84b905 to e145700 Compare September 29, 2022 00:26
@stsewd stsewd requested a review from ericholscher September 29, 2022 18:55
Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure to understand the benefits of it but I don't see anything wrong, tho. So, I think we are fine moving forward with this PR since I assume it will make the new API simpler.

if projects:
context = kwargs.setdefault("context", {})
context["projects_data"] = {
project.slug: self._build_project_data(project, version.slug)
Copy link
Member

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 the version 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.

Copy link
Member

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

Copy link
Member Author

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

if project:
docs_url = project.get_docs_url(version_slug=obj.version)
project_alias = project.superprojects.values_list(
"alias", flat=True
).first()
projects_data = self.context.setdefault("projects_data", {})
version_data = VersionData(
slug=obj.version,
docs_url=docs_url,
)
projects_data[obj.project] = ProjectData(
alias=project_alias,
version=version_data,
)
return projects_data[obj.project]

@stsewd stsewd merged commit 32ef653 into main Oct 6, 2022
@stsewd stsewd deleted the refactor-search-serializer-context branch October 6, 2022 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants