Skip to content

Commit

Permalink
Merge pull request #1097 from thunderstore-io/media-asset-field-for-c…
Browse files Browse the repository at this point in the history
…ommunity-icons

Media asset field for community icons (TS-2285)
  • Loading branch information
MythicManiac authored Feb 20, 2025
2 parents 7402cf4 + 37fb677 commit 636207b
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CyberstormCommunitySerializer(serializers.Serializer):
hero_image_url = serializers.CharField(required=False)
cover_image_url = serializers.CharField(required=False)
icon_url = serializers.CharField(required=False)
community_icon_url = serializers.CharField(required=False)
total_download_count = serializers.SerializerMethodField()
total_package_count = serializers.SerializerMethodField()

Expand Down
1 change: 1 addition & 0 deletions django/thunderstore/api/cyberstorm/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_api_cyberstorm_community_detail_success(
assert c.aggregated.package_count == response_data["total_package_count"]
assert c.background_image_url == response_data["background_image_url"]
assert c.hero_image_url == response_data["hero_image_url"]
assert c.community_icon_url == response_data["community_icon_url"]
assert c.cover_image_url == response_data["cover_image_url"]
assert c.short_description == response_data["short_description"]
assert c.description == response_data["description"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_api_cyberstorm_community_list_get_success(
assert results[index]["total_download_count"] == c.aggregated.download_count
assert results[index]["total_package_count"] == c.aggregated.package_count
assert results[index]["background_image_url"] == c.background_image_url
assert results[index]["community_icon_url"] == c.community_icon_url
assert results[index]["hero_image_url"] == c.hero_image_url
assert results[index]["cover_image_url"] == c.cover_image_url
assert results[index]["description"] == c.description
Expand Down
2 changes: 2 additions & 0 deletions django/thunderstore/community/admin/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CommunityAdmin(admin.ModelAdmin):
"hero_image_height",
"icon_width",
"icon_height",
"community_icon_width",
"community_icon_height",
"cover_image_width",
"cover_image_height",
"datetime_created",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.1.7 on 2025-02-20 10:29

from django.db import migrations, models

import thunderstore.community.models.community


class Migration(migrations.Migration):

dependencies = [
("community", "0031_community_short_description"),
]

operations = [
migrations.AddField(
model_name="community",
name="community_icon",
field=models.ImageField(
blank=True,
height_field="community_icon_height",
null=True,
upload_to=thunderstore.community.models.community.get_community_filepath,
width_field="community_icon_width",
),
),
migrations.AddField(
model_name="community",
name="community_icon_height",
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name="community",
name="community_icon_width",
field=models.PositiveIntegerField(default=0),
),
]
28 changes: 28 additions & 0 deletions django/thunderstore/community/models/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class Community(TimestampMixin, models.Model):
icon_width = models.PositiveIntegerField(default=0)
icon_height = models.PositiveIntegerField(default=0)

community_icon = models.ImageField(
upload_to=get_community_filepath,
width_field="community_icon_width",
height_field="community_icon_height",
blank=True,
null=True,
)
community_icon_width = models.PositiveIntegerField(default=0)
community_icon_height = models.PositiveIntegerField(default=0)

cover_image = models.ImageField(
upload_to=get_community_filepath,
width_field="cover_image_width",
Expand Down Expand Up @@ -161,6 +171,17 @@ def save(self, *args, **kwargs):
"cover_image_height",
),
)
if not self.community_icon:
self.community_icon_width = 0
self.community_icon_height = 0
if "update_fields" in kwargs:
kwargs["update_fields"] = set(
kwargs["update_fields"]
+ (
"community_icon_width",
"community_icon_height",
),
)
return super().save(*args, **kwargs)

def __str__(self):
Expand Down Expand Up @@ -198,6 +219,13 @@ def cover_image_url(self) -> Optional[str]:
"""
return None if not bool(self.cover_image) else self.cover_image.url

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

@cached_property
def icon_url(self) -> Optional[str]:
"""
Expand Down
22 changes: 22 additions & 0 deletions django/thunderstore/community/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ def test_community_ensure_user_can_manage_packages(
assert error is None


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


@pytest.mark.django_db
def test_community_image_url_with_image(dummy_image):
community = CommunityFactory(community_icon=dummy_image)
url = community.community_icon_url
assert isinstance(url, str)


@pytest.mark.django_db
def test_background_image_url_when_community_has_no_image():
community = CommunityFactory()
Expand Down Expand Up @@ -187,3 +201,11 @@ 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_community_validate_identifier_on_save():
community = CommunityFactory()
community.identifier = "test"
with pytest.raises(ValidationError):
community.save()

0 comments on commit 636207b

Please sign in to comment.