Skip to content

Commit

Permalink
Cyberstorm API: return team's categories and sections
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anttimaki committed Oct 23, 2023
1 parent 640a110 commit e8fe3a9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
class CyberstormPackageCategorySerializer(serializers.Serializer):
name = serializers.CharField()
slug = serializers.SlugField()


class CyberstormPackageListingSectionSerializer(serializers.Serializer):
name = serializers.CharField()
slug = serializers.SlugField()
priority = serializers.IntegerField()
36 changes: 36 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,54 @@

from rest_framework import serializers

from thunderstore.api.cyberstorm.serializers.shared import (
CyberstormPackageCategorySerializer,
CyberstormPackageListingSectionSerializer,
)
from thunderstore.community.models import PackageCategory, PackageListingSection
from thunderstore.social.utils import get_avatar_url


class CyberstormTeamSerializer(serializers.Serializer):
"""
This is for team's public profile and readably by anyone. Don't add
any sensitive information here.
package_categories and sections will be populated only if a
`community` query parameter is used with the request.
"""

identifier = serializers.IntegerField(source="id")
name = serializers.CharField()
donation_link = serializers.CharField(required=False)
package_categories = serializers.SerializerMethodField()
sections = serializers.SerializerMethodField()

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

Check warning on line 29 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L29

Added line #L29 was not covered by tests

if community_id:
categories = PackageCategory.objects.filter(

Check warning on line 32 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L32

Added line #L32 was not covered by tests
community__identifier=community_id,
)
else:
categories = PackageCategory.objects.none()

Check warning on line 36 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L36

Added line #L36 was not covered by tests

return CyberstormPackageCategorySerializer(categories, many=True).data

Check warning on line 38 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L38

Added line #L38 was not covered by tests

def get_sections(self, obj):
community_id = self.context["request"].GET.get("community")

Check warning on line 41 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L41

Added line #L41 was not covered by tests

if community_id:
sections = (

Check warning on line 44 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L44

Added line #L44 was not covered by tests
PackageListingSection.objects.listed()
.filter(community__identifier=community_id)
.order_by("priority")
)
else:
sections = PackageListingSection.objects.none()

Check warning on line 50 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L50

Added line #L50 was not covered by tests

return CyberstormPackageListingSectionSerializer(sections, many=True).data

Check warning on line 52 in django/thunderstore/api/cyberstorm/serializers/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/serializers/team.py#L52

Added line #L52 was not covered by tests


class CyberstormTeamMemberSerializer(serializers.Serializer):
Expand Down
48 changes: 48 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from rest_framework.test import APIClient

from thunderstore.account.factories import ServiceAccountFactory
from thunderstore.community.factories import PackageCategoryFactory
from thunderstore.community.models import Community, PackageListingSection
from thunderstore.core.types import UserType
from thunderstore.repository.factories import TeamFactory, TeamMemberFactory
from thunderstore.repository.models.team import Team
Expand All @@ -20,6 +22,52 @@ def test_team_detail_api_view__for_active_team__returns_data(
assert team.donation_link == result["donation_link"]


@pytest.mark.django_db
def test_team_detail_api_view__when_queried_with_community__returns_categories_and_sections(
api_client: APIClient,
community: Community,
team: Team,
):
category = PackageCategoryFactory(community=community, slug="only-category")
modpacks = PackageListingSection.objects.create(
community=community,
name="Modpacks",
slug="modpacks",
priority=9001,
)
mods = PackageListingSection.objects.create(
community=community,
name="Mods",
slug="mods",
priority=1,
)

# Return empty lists if community is not specified.
response = api_client.get(f"/api/cyberstorm/team/{team.name}/")
result = response.json()

assert response.status_code == 200
assert type(result["package_categories"]) == list
assert len(result["package_categories"]) == 0
assert type(result["sections"]) == list
assert len(result["sections"]) == 0

# Return community's categories/sections if community is specified.
response = api_client.get(
f"/api/cyberstorm/team/{team.name}/?community={community.identifier}",
)
result = response.json()

assert response.status_code == 200
assert type(result["package_categories"]) == list
assert len(result["package_categories"]) == 1
assert result["package_categories"][0]["slug"] == category.slug
assert type(result["sections"]) == list
assert len(result["sections"]) == 2
assert result["sections"][0]["slug"] == mods.slug
assert result["sections"][1]["slug"] == modpacks.slug


@pytest.mark.django_db
def test_team_detail_api_view__for_nonexisting_team__returns_404(api_client: APIClient):
response = api_client.get("/api/cyberstorm/team/bad/")
Expand Down

0 comments on commit e8fe3a9

Please sign in to comment.