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

Cyberstorm API: return categories and sections for a team #895

Closed
wants to merge 2 commits into from

Conversation

anttimaki
Copy link
Collaborator

Cyberstorm API: add CyberstormPackageCategorySerializer

It felt a bit dirty to import such a simple serializer from another
app, especially since that serializer is marked experimental.

Refs TS-1860

Cyberstorm API: return team's categories and sections

If community is defined, return a list of community's PackageCategories
and PackageListingSections too. This is done via optional query
parameter so both views that have access to community (e.g. team's
package listing view) and don't have it (e.g. team's profile settings
view) can use the same endpoint.

Refs TS-1856

It felt a bit dirty to import such a simple serializer from another
app, especially since that serializer is marked experimental.

Refs TS-1860
If community is defined, return a list of community's PackageCategories
and PackageListingSections too. This is done via optional query
parameter so both views that have access to community (e.g. team's
package listing view) and don't have it (e.g. team's profile settings
view) can use the same endpoint.

Refs TS-1856
@anttimaki
Copy link
Collaborator Author

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@codecov
Copy link

codecov bot commented Oct 23, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8637060) 92.27% compared to head (e8fe3a9) 92.31%.

Additional details and impacted files
@@                 Coverage Diff                 @@
##           cyberstorm-team     #895      +/-   ##
===================================================
+ Coverage            92.27%   92.31%   +0.03%     
===================================================
  Files                  271      272       +1     
  Lines                 7820     7842      +22     
  Branches               743      745       +2     
===================================================
+ Hits                  7216     7239      +23     
+ Misses                 502      501       -1     
  Partials               102      102              
Files Coverage Δ
...understore/api/cyberstorm/serializers/community.py 100.00% <100.00%> (ø)
.../thunderstore/api/cyberstorm/serializers/shared.py 100.00% <100.00%> (ø)
...go/thunderstore/api/cyberstorm/serializers/team.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Base automatically changed from cyberstorm-team to master October 23, 2023 17:39
sections = serializers.SerializerMethodField()

def get_package_categories(self, obj):
community_id = self.context["request"].GET.get("community")
Copy link
Member

@MythicManiac MythicManiac Oct 23, 2023

Choose a reason for hiding this comment

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

I don't like the concept of having serializers depend on a request object. That's caused issues in the past and probably should be good to avoid in the future too. I'd go as far as saying using serializer context is a bad practice in general because of how easily it turns into a footgun.

I also feel like package_categories and sections don't really have a purpose existing under the serializer of a Team object. If they need to be bundled in to a response together, that seems like something the view class should handle sorting out?

I'm assuming these exist here to provide functionality for the team-filtered package search view. Perhaps editing the package search API view to take a team ID optional parameter would be a better approach to achieve that? That is after all already implicitly community-scoped, which would make this a lot more straight forward.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@MythicManiac

I don't like the concept of having serializers depend on a request object. That's caused issues in the past and probably should be good to avoid in the future too. I'd go as far as saying using serializer context is a bad practice in general because of how easily it turns into a footgun.

I also feel like package_categories and sections don't really have a purpose existing under the serializer of a Team object. If they need to be bundled in to a response together, that seems like something the view class should handle sorting out?

I've no strong feelings regarding this part, but it might be a bit irrelevant in this particular case, giving the rest of the reply.

I'm assuming these exist here to provide functionality for the team-filtered package search view.

Yes, this is due to trying to reuse the endpoint for both team-filtered package search view, and the main tab of team's settings view.

Perhaps editing the package search API view to take a team ID optional parameter would be a better approach to achieve that? That is after all already implicitly community-scoped, which would make this a lot more straight forward.

I don't like this idea. Currently I have the client side designed to have three layers:

  1. Layout components (currently one for a community and one for a team, in the future probably one for user too). This fetches package categories and sections from backend. The rationale being that they're static in the sense they only depend on the community (currently), so fetch them once on the top level to keep the other layers simpler. Doesn't have internal state.

  2. PackageSearch component, used by all the layout components. Handles the client side state: what filters are selected, what's written in the text search input. Doesn't communicate with the backend at all.

  3. PackageList component, used by PackageSearch component. Handless fetching the packages from the backend, mostly based on the props passed by PackageSearch. Doesn't fetch categories/sections, because filters don't affect them, e.g. refetching them on every change on the text search is unnecessary. I would have liked this component to have no internal state, but a) it handles the state for pagination, since pagination information is returned by backend when packages themselves are fetched, and b) it handles the state of ordering the results, just for UI layout reasons.


So, as a compromise, would it be better to have a separate endpoint for fetching the categories/sections? That way they wouldn't affect the serializers, and wouldn't mess with the design I've chosen.

Copy link
Member

Choose a reason for hiding this comment

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

So, as a compromise, would it be better to have a separate endpoint for fetching the categories/sections? That way they wouldn't affect the serializers, and wouldn't mess with the design I've chosen.

This sounds like it would address my primary concern, which is that of mixing data unrelated to teams in the team serializer. Also I want to clarify I'm not against having view-specific serializers (e.g. FooApiResponseSerializer is a pattern used in a few) that contain multiple different objects nested inside, the main part I didn't like here is how the serializer & view presented themselves as being bound to a specific object type in a CRUD like fashion, but still returned (arguably unrelated) data beyond that scope entirely.

@anttimaki
Copy link
Collaborator Author

Closing and reimplementing as a separate endpoint.

@anttimaki anttimaki closed this Oct 24, 2023
@anttimaki anttimaki deleted the cyberstorm-team-categories branch October 25, 2023 09:51
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.

2 participants