diff --git a/django/thunderstore/api/cyberstorm/serializers/shared.py b/django/thunderstore/api/cyberstorm/serializers/shared.py index eb5ffcebc..d57e280cd 100644 --- a/django/thunderstore/api/cyberstorm/serializers/shared.py +++ b/django/thunderstore/api/cyberstorm/serializers/shared.py @@ -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() diff --git a/django/thunderstore/api/cyberstorm/serializers/team.py b/django/thunderstore/api/cyberstorm/serializers/team.py index d0ece7e73..7a3ed7809 100644 --- a/django/thunderstore/api/cyberstorm/serializers/team.py +++ b/django/thunderstore/api/cyberstorm/serializers/team.py @@ -2,6 +2,11 @@ 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 @@ -9,11 +14,42 @@ 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") + + if community_id: + categories = PackageCategory.objects.filter( + community__identifier=community_id, + ) + else: + categories = PackageCategory.objects.none() + + return CyberstormPackageCategorySerializer(categories, many=True).data + + def get_sections(self, obj): + community_id = self.context["request"].GET.get("community") + + if community_id: + sections = ( + PackageListingSection.objects.listed() + .filter(community__identifier=community_id) + .order_by("priority") + ) + else: + sections = PackageListingSection.objects.none() + + return CyberstormPackageListingSectionSerializer(sections, many=True).data class CyberstormTeamMemberSerializer(serializers.Serializer): diff --git a/django/thunderstore/api/cyberstorm/tests/test_team.py b/django/thunderstore/api/cyberstorm/tests/test_team.py index cfece0de0..26b0cc99b 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_team.py +++ b/django/thunderstore/api/cyberstorm/tests/test_team.py @@ -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 @@ -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/")