Skip to content

Commit

Permalink
Return package categories along community's details
Browse files Browse the repository at this point in the history
Cyberstorm uses package categories as filters on community's profile
page.

Refs TS-1860
  • Loading branch information
anttimaki committed Oct 19, 2023
1 parent b086bca commit 58dac46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/community.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from rest_framework import serializers

from thunderstore.community.api.experimental.serializers import (
PackageCategoryExperimentalSerializer,
)
from thunderstore.community.models import PackageCategory
from thunderstore.repository.api.experimental.serializers import (
CommunityFilteredModelChoiceField,
)


class CyberstormCommunitySerializer(serializers.Serializer):
name = serializers.CharField()
Expand All @@ -11,6 +19,7 @@ class CyberstormCommunitySerializer(serializers.Serializer):
icon_url = serializers.CharField(required=False)
total_download_count = serializers.SerializerMethodField()
total_package_count = serializers.SerializerMethodField()
package_categories = PackageCategoryExperimentalSerializer(many=True)

def get_total_download_count(self, obj) -> int:
return obj.aggregated.download_count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
from rest_framework.test import APIClient

from thunderstore.community.factories import PackageListingFactory
from thunderstore.community.models import CommunityAggregatedFields, CommunitySite
from thunderstore.community.models import (
CommunityAggregatedFields,
CommunitySite,
PackageCategory,
)


@pytest.mark.django_db
def test_api_cyberstorm_community_detail_success(
client: APIClient,
community_site: CommunitySite,
):
categories = [
PackageCategory.objects.create(
name=i,
slug=i,
community=community_site.community,
)
for i in range(3)
]

PackageListingFactory(
community_=community_site.community,
categories=[categories[0]],
package_version_kwargs={"downloads": 0},
)
PackageListingFactory(
community_=community_site.community,
categories=[categories[0], categories[1]],
package_version_kwargs={"downloads": 23},
)
PackageListingFactory(
Expand Down Expand Up @@ -43,6 +58,15 @@ def test_api_cyberstorm_community_detail_success(
assert c.description == response_data["description"]
assert c.discord_url == response_data["discord_url"]

# Include all community's categories even if they're currently not
# in use, so people see they exists.
assert type(response_data["package_categories"]) == list
assert len(response_data["package_categories"]) == 3
slugs = [c["slug"] for c in response_data["package_categories"]]
assert "0" in slugs
assert "1" in slugs
assert "2" in slugs


@pytest.mark.django_db
def test_api_cyberstorm_community_detail_failure(
Expand Down

0 comments on commit 58dac46

Please sign in to comment.