From 58dac46078ecf58db98bb1205a64a881ca8099a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Tue, 17 Oct 2023 15:22:48 +0300 Subject: [PATCH] Return package categories along community's details Cyberstorm uses package categories as filters on community's profile page. Refs TS-1860 --- .../api/cyberstorm/serializers/community.py | 9 +++++++ .../cyberstorm/tests/test_community_detail.py | 26 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/django/thunderstore/api/cyberstorm/serializers/community.py b/django/thunderstore/api/cyberstorm/serializers/community.py index f8087cb03..71061ee22 100644 --- a/django/thunderstore/api/cyberstorm/serializers/community.py +++ b/django/thunderstore/api/cyberstorm/serializers/community.py @@ -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() @@ -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 diff --git a/django/thunderstore/api/cyberstorm/tests/test_community_detail.py b/django/thunderstore/api/cyberstorm/tests/test_community_detail.py index e36e44541..9aa0625a7 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_community_detail.py +++ b/django/thunderstore/api/cyberstorm/tests/test_community_detail.py @@ -2,7 +2,11 @@ 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 @@ -10,12 +14,23 @@ 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( @@ -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(