From d1543e7d69bae98f1a8fe8818692259fce9b7d5f Mon Sep 17 00:00:00 2001
From: Ernesto Rico Schmidt <ernesto@helsinki.at>
Date: Wed, 26 Jun 2024 19:02:50 -0400
Subject: [PATCH] fix: update admin interface and settings serializer

---
 program/admin.py       | 25 ++++++++++++++++++++++++-
 program/serializers.py | 18 ++++++++++++++++--
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/program/admin.py b/program/admin.py
index 63e295d6..08410e57 100644
--- a/program/admin.py
+++ b/program/admin.py
@@ -1,9 +1,11 @@
 from django_json_widget.widgets import JSONEditorWidget
 
+from django.conf import settings
 from django.contrib import admin
 from django.contrib.auth.admin import UserAdmin
 from django.contrib.auth.models import User
 from django.db.models import JSONField
+from django.utils.safestring import mark_safe
 from program.models import (
     Category,
     FundingCategory,
@@ -104,7 +106,17 @@ admin.site.register(User, UserProfileUserAdmin)
 @admin.register(RadioSettings)
 class RadioSettingsAdmin(admin.ModelAdmin):
     fieldsets = [
-        (None, {"fields": ["station_name", "station_website", "station_logo"]}),
+        (
+            None,
+            {
+                "fields": [
+                    "station_name",
+                    "station_website",
+                    "station_logo",
+                    "station_logo_preview",
+                ]
+            },
+        ),
         (
             "Image requirements",
             {
@@ -135,3 +147,14 @@ class RadioSettingsAdmin(admin.ModelAdmin):
             )
         },
     }
+    readonly_fields = ["station_logo_preview"]
+
+    @staticmethod
+    def station_logo_preview(obj):
+        url = obj.station_logo.url
+        height = obj.station_logo.height
+        width = obj.station_logo.width
+
+        return mark_safe(
+            f'<img src="{settings.SITE_URL}/{url}" width="{width}" height="{height}"/>'
+        )
diff --git a/program/serializers.py b/program/serializers.py
index b134f6bd..6ed9770a 100644
--- a/program/serializers.py
+++ b/program/serializers.py
@@ -1210,9 +1210,15 @@ class RadioPlayoutSettings(TypedDict):
     pools: PlayoutPools
 
 
+class Logo(TypedDict):
+    url: str
+    height: int
+    width: int
+
+
 class RadioStationSettings(TypedDict):
     name: str
-    logo_id: int | None
+    logo: Logo | None
     website: str
 
 
@@ -1331,7 +1337,15 @@ class RadioSettingsSerializer(serializers.ModelSerializer):
     def get_station(obj) -> RadioStationSettings:
         return {
             "name": obj.station_name,
-            "logo_id": obj.station_logo.id if obj.station_logo else None,
+            "logo": (
+                Logo(
+                    url=f"{settings.SITE_URL}{obj.station_logo.url}",
+                    height=obj.station_logo.height,
+                    width=obj.station_logo.width,
+                )
+                if obj.station_logo
+                else None
+            ),
             "website": obj.station_website,
         }
 
-- 
GitLab