Skip to content
Snippets Groups Projects
Commit dacf2dfc authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

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.
parent 7e487608
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment