From dacf2dfcd9bbefd3977075951978bf6b8cd9b750 Mon Sep 17 00:00:00 2001 From: Konrad Mohrfeldt <konrad.mohrfeldt@farbdev.org> Date: Sun, 9 Apr 2023 18:17:48 +0200 Subject: [PATCH] refactor: add dimensions to thumbnails in Image REST API Any frontend will need the dimensions of the supplied thumbnails to select the appropriate thumbnail in different rendering contexts. Apart from that the thumbnail name is not that helpful, as we need the full thumbnail URL to be able to display it. --- program/serializers.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/program/serializers.py b/program/serializers.py index f1c870b7..1cef8814 100644 --- a/program/serializers.py +++ b/program/serializers.py @@ -18,7 +18,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -from typing import List +from typing import List, TypedDict from rest_framework import serializers @@ -197,18 +197,31 @@ class HostLinkSerializer(serializers.ModelSerializer): fields = ("type", "url") +class Thumbnail(TypedDict): + width: float + height: float + url: str + + class ImageSerializer(serializers.ModelSerializer): ppoi = serializers.CharField(max_length=20) # PPOIField max_length thumbnails = serializers.SerializerMethodField() @staticmethod - def get_thumbnails(instance) -> List[str]: + def get_thumbnails(instance) -> List[Thumbnail]: """Returns thumbnails""" thumbnails = [] if instance.image.name and THUMBNAIL_SIZES: for size in THUMBNAIL_SIZES: - thumbnails.append(instance.image.crop[size].name) + [width, height] = size.split("x") + thumbnails.append( + { + "width": int(width), + "height": int(height), + "url": instance.image.crop[size].url, + } + ) return thumbnails -- GitLab