Skip to content

Commit

Permalink
Merge pull request #988 from thunderstore-io/cyberstorm-deactivated-d…
Browse files Browse the repository at this point in the history
…ependencies

Cyberstorm: show deprecated package dependencies
  • Loading branch information
anttimaki authored Jan 3, 2024
2 parents a04f8b2 + 6e1b65d commit 463470a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
54 changes: 53 additions & 1 deletion django/thunderstore/api/cyberstorm/tests/test_package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import pytest
from rest_framework.test import APIClient

from thunderstore.api.cyberstorm.views.package_listing import get_custom_package_listing
from thunderstore.api.cyberstorm.views.package_listing import (
DependencySerializer,
get_custom_package_listing,
)
from thunderstore.community.factories import (
CommunityFactory,
PackageCategoryFactory,
Expand Down Expand Up @@ -280,5 +283,54 @@ def test_package_listing_view__serializes_url_correctly(api_client: APIClient) -
assert actual["website_url"] is None


@pytest.mark.django_db
@pytest.mark.parametrize(
("package_is_active", "version_is_active"),
(
(False, False),
(True, False),
(False, True),
(True, True),
),
)
def test_dependency_serializer__reads_is_active_from_correct_field(
package_is_active: bool,
version_is_active: bool,
) -> None:
dependant = PackageVersionFactory()
dependency = PackageVersionFactory(is_active=version_is_active)
dependency.package.is_active = package_is_active
dependency.package.save()
dependant.dependencies.set([dependency])

# community_identifier is normally added using annotations, but
# it's irrelavant for this test case.
dependency.community_identifier = "greendale"

actual = DependencySerializer(dependency).data

assert actual["is_active"] == (package_is_active and version_is_active)


@pytest.mark.django_db
def test_dependency_serializer__when_dependency_is_not_active__censors_icon_and_description() -> None:
# community_identifier is normally added using annotations, but
# it's irrelavant for this test case.
dependency = PackageVersionFactory()
dependency.community_identifier = "greendale"

actual = DependencySerializer(dependency).data

assert actual["description"].startswith("Desc_")
assert actual["icon_url"].startswith("http")

dependency.is_active = False
del dependency.is_effectively_active # Clear cached property
actual = DependencySerializer(dependency).data

assert actual["description"] == "This package has been removed."
assert actual["icon_url"] is None


def _date_to_z(value: datetime) -> str:
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
28 changes: 22 additions & 6 deletions django/thunderstore/api/cyberstorm/views/package_listing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from django.db.models import (
BooleanField,
CharField,
Expand Down Expand Up @@ -27,17 +29,32 @@ class DependencySerializer(serializers.Serializer):
"""
Dependencies of a given PackageVersion, listed in a given Community.
community_identifier and namespace are not present by default and
need to be annotated to the object.
community_identifier is not present by default and needs to be
annotated to the object.
Description and icon is not shown to clients if the dependency is
deactivated, since the fields may contain the very reason for the
deactivation.
"""

community_identifier = serializers.CharField()
description = serializers.CharField()
icon_url = serializers.CharField(source="icon.url")
description = serializers.SerializerMethodField()
icon_url = serializers.SerializerMethodField()
is_active = serializers.BooleanField(source="is_effectively_active")
name = serializers.CharField()
namespace = serializers.CharField(source="package.namespace.name")
version_number = serializers.CharField()

def get_description(self, obj: PackageVersion) -> str:
return (
obj.description
if obj.is_effectively_active
else "This package has been removed."
)

def get_icon_url(self, obj: PackageVersion) -> Optional[str]:
return obj.icon.url if obj.is_effectively_active else None


class TeamSerializer(serializers.Serializer):
"""
Expand Down Expand Up @@ -165,8 +182,7 @@ def get_custom_package_listing(
)

dependencies = (
listing.package.latest.dependencies.active()
.listed_in(community_id)
listing.package.latest.dependencies.listed_in(community_id)
.annotate(
community_identifier=Value(community_id, CharField()),
)
Expand Down
4 changes: 4 additions & 0 deletions django/thunderstore/repository/models/package_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def owner(self):
def is_deprecated(self):
return self.package.is_deprecated

@cached_property
def is_effectively_active(self):
return self.is_active and self.package.is_active

@cached_property
def full_version_name(self):
return f"{self.package.full_package_name}-{self.version_number}"
Expand Down
22 changes: 21 additions & 1 deletion django/thunderstore/repository/tests/test_package_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from thunderstore.community.factories import PackageListingFactory
from thunderstore.community.models.package_listing import PackageListing
from thunderstore.repository.factories import PackageVersionFactory
from thunderstore.repository.factories import PackageFactory, PackageVersionFactory
from thunderstore.repository.models import PackageVersion
from thunderstore.repository.package_formats import PackageFormats

Expand Down Expand Up @@ -109,3 +109,23 @@ def test_package_version_chunked_enumerate() -> None:
package_ids.remove(entry.pk)

assert len(package_ids) == 0


@pytest.mark.django_db
@pytest.mark.parametrize(
("package_is_active", "version_is_active"),
(
(False, False),
(True, False),
(False, True),
(True, True),
),
)
def test_package_version_is_effectively_active(
package_is_active: bool,
version_is_active: bool,
) -> None:
package = PackageFactory(is_active=package_is_active)
version = PackageVersionFactory(package=package, is_active=version_is_active)

assert version.is_effectively_active == (package_is_active and version_is_active)

0 comments on commit 463470a

Please sign in to comment.