Skip to content

Commit

Permalink
Add Nimbus background image to community model
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Nov 12, 2024
1 parent 6c3e20f commit 42bf528
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CyberstormCommunitySerializer(serializers.Serializer):
discord_url = serializers.CharField(required=False)
wiki_url = serializers.CharField(required=False)
datetime_created = serializers.DateTimeField()
background_image_url = serializers.CharField(required=False)
nimbus_background_image_url = serializers.CharField(required=False)
cover_image_url = serializers.CharField(required=False)
icon_url = serializers.CharField(required=False)
total_download_count = serializers.SerializerMethodField()
Expand Down
2 changes: 1 addition & 1 deletion django/thunderstore/api/cyberstorm/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_api_cyberstorm_community_detail_success(
assert c.identifier == response_data["identifier"]
assert c.aggregated.download_count == response_data["total_download_count"]
assert c.aggregated.package_count == response_data["total_package_count"]
assert c.background_image_url == response_data["background_image_url"]
assert c.nimbus_background_image_url == response_data["nimbus_background_image_url"]
assert c.cover_image_url == response_data["cover_image_url"]
assert c.description == response_data["description"]
assert c.discord_url == response_data["discord_url"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_api_cyberstorm_community_list_get_success(
)
community2 = CommunityFactory(
aggregated_fields=CommunityAggregatedFields.objects.create(),
background_image=dummy_image,
nimbus_background_image=dummy_image,
cover_image=dummy_cover_image,
)

Expand Down Expand Up @@ -66,7 +66,7 @@ def test_api_cyberstorm_community_list_get_success(
assert results[index]["identifier"] == c.identifier
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]["nimbus_background_image_url"] == c.background_image_url
assert results[index]["cover_image_url"] == c.cover_image_url
assert results[index]["description"] == c.description
assert results[index]["discord_url"] == c.discord_url
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 @@ -42,6 +42,8 @@ class CommunityAdmin(admin.ModelAdmin):
"identifier",
"background_image_width",
"background_image_height",
"nimbus_background_image_width",
"nimbus_background_image_height",
"icon_width",
"icon_height",
"cover_image_width",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.1.7 on 2024-11-12 15:39

from django.db import migrations, models
import thunderstore.community.models.community


class Migration(migrations.Migration):

dependencies = [
('community', '0029_packagelisting_is_auto_imported'),
]

operations = [
migrations.AddField(
model_name='community',
name='nimbus_background_image',
field=models.ImageField(blank=True, height_field='nimbus_background_image_height', null=True, upload_to=thunderstore.community.models.community.get_community_filepath, width_field='nimbus_background_image_width'),
),
migrations.AddField(
model_name='community',
name='nimbus_background_image_height',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='community',
name='nimbus_background_image_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 @@ -80,6 +80,16 @@ class Community(TimestampMixin, models.Model):
background_image_width = models.PositiveIntegerField(default=0)
background_image_height = models.PositiveIntegerField(default=0)

nimbus_background_image = models.ImageField(
upload_to=get_community_filepath,
width_field="nimbus_background_image_width",
height_field="nimbus_background_image_height",
blank=True,
null=True,
)
nimbus_background_image_width = models.PositiveIntegerField(default=0)
nimbus_background_image_height = models.PositiveIntegerField(default=0)

identifier = models.CharField(max_length=256, unique=True, db_index=True)
name = models.CharField(max_length=256)
discord_url = models.CharField(max_length=512, blank=True, null=True)
Expand Down Expand Up @@ -128,6 +138,17 @@ def save(self, *args, **kwargs):
"background_image_height",
),
)
if not self.nimbus_background_image:
self.nimbus_background_image_width = 0
self.nimbus_background_image_height = 0
if "update_fields" in kwargs:
kwargs["update_fields"] = set(
kwargs["update_fields"]
+ (
"nimbus_background_image_width",
"nimbus_background_image_height",
),
)
if not self.cover_image:
self.cover_image_width = 0
self.cover_image_height = 0
Expand Down Expand Up @@ -162,6 +183,13 @@ def background_image_url(self) -> Optional[str]:
"""
return None if not bool(self.background_image) else self.background_image.url

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

@cached_property
def cover_image_url(self) -> Optional[str]:
"""
Expand Down
14 changes: 14 additions & 0 deletions django/thunderstore/community/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ def test_background_image_url_when_community_has_image(dummy_image):
assert isinstance(url, str)
assert len(url) > 0

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


@pytest.mark.django_db
def test_nimbus_background_image_url_when_community_has_image(dummy_image):
community = CommunityFactory(nimbus_background_image=dummy_image)
url = community.nimbus_background_image_url
assert isinstance(url, str)
assert len(url) > 0


@pytest.mark.django_db
def test_cover_image_url_when_community_has_no_image():
Expand Down

0 comments on commit 42bf528

Please sign in to comment.