Skip to content

Commit 636207b

Browse files
authored
Merge pull request #1097 from thunderstore-io/media-asset-field-for-community-icons
Media asset field for community icons (TS-2285)
2 parents 7402cf4 + 37fb677 commit 636207b

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

django/thunderstore/api/cyberstorm/serializers/community.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CyberstormCommunitySerializer(serializers.Serializer):
1313
hero_image_url = serializers.CharField(required=False)
1414
cover_image_url = serializers.CharField(required=False)
1515
icon_url = serializers.CharField(required=False)
16+
community_icon_url = serializers.CharField(required=False)
1617
total_download_count = serializers.SerializerMethodField()
1718
total_package_count = serializers.SerializerMethodField()
1819

django/thunderstore/api/cyberstorm/tests/test_community.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def test_api_cyberstorm_community_detail_success(
4141
assert c.aggregated.package_count == response_data["total_package_count"]
4242
assert c.background_image_url == response_data["background_image_url"]
4343
assert c.hero_image_url == response_data["hero_image_url"]
44+
assert c.community_icon_url == response_data["community_icon_url"]
4445
assert c.cover_image_url == response_data["cover_image_url"]
4546
assert c.short_description == response_data["short_description"]
4647
assert c.description == response_data["description"]

django/thunderstore/api/cyberstorm/tests/test_community_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def test_api_cyberstorm_community_list_get_success(
6969
assert results[index]["total_download_count"] == c.aggregated.download_count
7070
assert results[index]["total_package_count"] == c.aggregated.package_count
7171
assert results[index]["background_image_url"] == c.background_image_url
72+
assert results[index]["community_icon_url"] == c.community_icon_url
7273
assert results[index]["hero_image_url"] == c.hero_image_url
7374
assert results[index]["cover_image_url"] == c.cover_image_url
7475
assert results[index]["description"] == c.description

django/thunderstore/community/admin/community.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class CommunityAdmin(admin.ModelAdmin):
4646
"hero_image_height",
4747
"icon_width",
4848
"icon_height",
49+
"community_icon_width",
50+
"community_icon_height",
4951
"cover_image_width",
5052
"cover_image_height",
5153
"datetime_created",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generated by Django 3.1.7 on 2025-02-20 10:29
2+
3+
from django.db import migrations, models
4+
5+
import thunderstore.community.models.community
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
("community", "0031_community_short_description"),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name="community",
17+
name="community_icon",
18+
field=models.ImageField(
19+
blank=True,
20+
height_field="community_icon_height",
21+
null=True,
22+
upload_to=thunderstore.community.models.community.get_community_filepath,
23+
width_field="community_icon_width",
24+
),
25+
),
26+
migrations.AddField(
27+
model_name="community",
28+
name="community_icon_height",
29+
field=models.PositiveIntegerField(default=0),
30+
),
31+
migrations.AddField(
32+
model_name="community",
33+
name="community_icon_width",
34+
field=models.PositiveIntegerField(default=0),
35+
),
36+
]

django/thunderstore/community/models/community.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ class Community(TimestampMixin, models.Model):
6161
icon_width = models.PositiveIntegerField(default=0)
6262
icon_height = models.PositiveIntegerField(default=0)
6363

64+
community_icon = models.ImageField(
65+
upload_to=get_community_filepath,
66+
width_field="community_icon_width",
67+
height_field="community_icon_height",
68+
blank=True,
69+
null=True,
70+
)
71+
community_icon_width = models.PositiveIntegerField(default=0)
72+
community_icon_height = models.PositiveIntegerField(default=0)
73+
6474
cover_image = models.ImageField(
6575
upload_to=get_community_filepath,
6676
width_field="cover_image_width",
@@ -161,6 +171,17 @@ def save(self, *args, **kwargs):
161171
"cover_image_height",
162172
),
163173
)
174+
if not self.community_icon:
175+
self.community_icon_width = 0
176+
self.community_icon_height = 0
177+
if "update_fields" in kwargs:
178+
kwargs["update_fields"] = set(
179+
kwargs["update_fields"]
180+
+ (
181+
"community_icon_width",
182+
"community_icon_height",
183+
),
184+
)
164185
return super().save(*args, **kwargs)
165186

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

222+
@cached_property
223+
def community_icon_url(self) -> Optional[str]:
224+
"""
225+
Return URL to the community's icon image if one exists.
226+
"""
227+
return None if not bool(self.community_icon) else self.community_icon.url
228+
201229
@cached_property
202230
def icon_url(self) -> Optional[str]:
203231
"""

django/thunderstore/community/tests/test_community.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ def test_community_ensure_user_can_manage_packages(
6767
assert error is None
6868

6969

70+
@pytest.mark.django_db
71+
def test_community_image_url_without_image():
72+
community = CommunityFactory()
73+
url = community.community_icon_url
74+
assert url is None
75+
76+
77+
@pytest.mark.django_db
78+
def test_community_image_url_with_image(dummy_image):
79+
community = CommunityFactory(community_icon=dummy_image)
80+
url = community.community_icon_url
81+
assert isinstance(url, str)
82+
83+
7084
@pytest.mark.django_db
7185
def test_background_image_url_when_community_has_no_image():
7286
community = CommunityFactory()
@@ -187,3 +201,11 @@ def test_community_should_use_old_urls(
187201

188202
def test_community_should_use_old_urls_no_community() -> None:
189203
assert Community.should_use_old_urls(None) is True
204+
205+
206+
@pytest.mark.django_db
207+
def test_community_validate_identifier_on_save():
208+
community = CommunityFactory()
209+
community.identifier = "test"
210+
with pytest.raises(ValidationError):
211+
community.save()

0 commit comments

Comments
 (0)