Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make community_list endpoint support required functionality #863

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions django/thunderstore/api/cyberstorm/serializers/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
class CyberstormCommunitySerializer(serializers.Serializer):
name = serializers.CharField()
identifier = serializers.CharField()
description = serializers.CharField(required=False)
discord_url = serializers.CharField(required=False)
datetime_created = serializers.DateTimeField()
background_image_url = serializers.CharField(required=False)
icon_url = serializers.CharField(required=False)
total_download_count = serializers.IntegerField()
total_package_count = serializers.IntegerField()
background_image_url = serializers.CharField(required=False)
description = serializers.CharField()
discord_url = serializers.CharField(required=False)
8 changes: 5 additions & 3 deletions django/thunderstore/api/cyberstorm/views/community_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from rest_framework.filters import SearchFilter
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination

Expand All @@ -7,14 +8,15 @@


class CommunityPaginator(PageNumberPagination):
page_size = 100
page_size = 150


class CommunityListAPIView(ListAPIView):
permission_classes = []
serializer_class = CyberstormCommunitySerializer
pagination_class = CommunityPaginator
queryset = Community.objects.listed()
filter_backends = [StrictOrderingFilter]
ordering_fields = ["identifier"]
filter_backends = [SearchFilter, StrictOrderingFilter]
search_fields = ["description", "name"]
ordering_fields = ["datetime_created", "identifier", "name"]
ordering = ["identifier"]
29 changes: 27 additions & 2 deletions django/thunderstore/community/models/community.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, Optional

from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Manager, QuerySet
Expand Down Expand Up @@ -66,12 +67,29 @@
@property
def total_package_count(self) -> int:
# TODO: Implement as a cached value with background updates
return -1
if not settings.DEBUG:
return -1

Check warning on line 71 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L71

Added line #L71 was not covered by tests

listings = self.package_listings.active()

Check warning on line 73 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L73

Added line #L73 was not covered by tests

if self.require_package_listing_approval:
listings = listings.approved()

Check warning on line 76 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L76

Added line #L76 was not covered by tests

return listings.count()

Check warning on line 78 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L78

Added line #L78 was not covered by tests

@property
def total_download_count(self) -> int:
# TODO: Implement as a cached value with background updates
return -1
# TODO: also figure out more efficient way to handle this
if not settings.DEBUG:
return -1

Check warning on line 85 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L85

Added line #L85 was not covered by tests

listings = self.package_listings.active()

Check warning on line 87 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L87

Added line #L87 was not covered by tests

if self.require_package_listing_approval:
listings = listings.approved()

Check warning on line 90 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L90

Added line #L90 was not covered by tests

return sum([l.total_downloads for l in listings])

def save(self, *args, **kwargs):
if self.pk:
Expand Down Expand Up @@ -123,6 +141,13 @@
"""
return None if not bool(self.background_image) else self.background_image.url

@cached_property
def icon_url(self) -> Optional[str]:
"""
Return URL to the community's icon image if one exists.
"""
return None if not bool(self.icon) else self.icon.url

Check warning on line 149 in django/thunderstore/community/models/community.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/community/models/community.py#L149

Added line #L149 was not covered by tests

def ensure_user_can_manage_packages(self, user: Optional[UserType]) -> None:
if not user or not user.is_authenticated:
raise ValidationError("Must be authenticated")
Expand Down
106 changes: 105 additions & 1 deletion django/thunderstore/community/tests/test_community.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest
from django.core.exceptions import ValidationError
from django.test import override_settings

from conftest import TestUserTypes
from thunderstore.community.consts import PackageListingReviewStatus
from thunderstore.community.factories import CommunityFactory, CommunitySiteFactory
from thunderstore.community.factories import (
CommunityFactory,
CommunitySiteFactory,
PackageListingFactory,
)
from thunderstore.community.models import (
Community,
CommunityMemberRole,
Expand Down Expand Up @@ -82,6 +87,21 @@ def test_background_image_url_when_community_has_image(dummy_image):
assert len(url) > 0


@pytest.mark.django_db
def test_icon_url_when_community_has_no_image():
community = CommunityFactory()
url = community.icon_url
assert url is None


@pytest.mark.django_db
def test_icon_url_when_community_has_image(dummy_image):
community = CommunityFactory(icon=dummy_image)
url = community.icon_url
assert isinstance(url, str)
assert len(url) > 0


@pytest.mark.django_db
def test_community_site_get_absolute_url(community_site: CommunitySite) -> None:
assert community_site.get_absolute_url == "/c/test/"
Expand Down Expand Up @@ -141,3 +161,87 @@ def test_community_should_use_old_urls(

def test_community_should_use_old_urls_no_community() -> None:
assert Community.should_use_old_urls(None) is True


@pytest.mark.django_db
def test_total_package_count():
# Method is not implemented yet and always returns -1.
community = CommunityFactory()
assert community.total_package_count == -1

PackageListingFactory(community_=community)
PackageListingFactory(
community_=community, review_status=PackageListingReviewStatus.approved
)
PackageListingFactory()
assert community.total_package_count == -1

community.require_package_listing_approval = True
community.save()
assert community.total_package_count == -1


@override_settings(DEBUG=True)
@pytest.mark.django_db
def test_experimental_total_package_count():
community = CommunityFactory()
assert community.total_package_count == 0

PackageListingFactory(community_=community)
PackageListingFactory(
community_=community, review_status=PackageListingReviewStatus.approved
)
PackageListingFactory()
assert community.total_package_count == 2

community.require_package_listing_approval = True
community.save()
assert community.total_package_count == 1


@pytest.mark.django_db
def test_total_download_count():
# Method is not implemented yet and always returns -1.
community = CommunityFactory()
assert community.total_download_count == -1

PackageListingFactory(community_=community, package_version_kwargs={"downloads": 0})
assert community.total_download_count == -1

PackageListingFactory(
community_=community, package_version_kwargs={"downloads": 23}
)
PackageListingFactory(
community_=community,
package_version_kwargs={"downloads": 11},
review_status=PackageListingReviewStatus.approved,
)
assert community.total_download_count == -1

community.require_package_listing_approval = True
community.save()
assert community.total_download_count == -1


@override_settings(DEBUG=True)
@pytest.mark.django_db
def test_experimental_total_download_count():
community = CommunityFactory()
assert community.total_download_count == 0

PackageListingFactory(community_=community, package_version_kwargs={"downloads": 0})
assert community.total_download_count == 0

PackageListingFactory(
community_=community, package_version_kwargs={"downloads": 23}
)
PackageListingFactory(
community_=community,
package_version_kwargs={"downloads": 11},
review_status=PackageListingReviewStatus.approved,
)
assert community.total_download_count == 23 + 11

community.require_package_listing_approval = True
community.save()
assert community.total_download_count == 11
Loading