Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Also expose the exercise thumbnails over the API

Roland Geiderbbae9f45e24f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 306ebee04..a9e965949 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,9 @@
## Other changes
+* The exercise image API now exposes `thumbnails` with `small` and `medium`
+ URLs, consistent with the ingredient API
+
* The ingredient API filter now supports range lookups on `nutriscore`
(`gt`, `gte`, `lt`, `lte`) in addition to `exact` and `in`, enabling queries
like "better than C" (#2295).
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index b9b421f8e..27b8ce9e8 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -24,6 +24,8 @@ from django.db.models import Q
# Third Party
from actstream import action as actstream_action
+from easy_thumbnails.exceptions import InvalidImageFormatError
+from easy_thumbnails.files import get_thumbnailer
from rest_framework import serializers
# wger
@@ -120,6 +122,7 @@ class ExerciseImageSerializer(serializers.ModelSerializer):
author_history = serializers.ListSerializer(child=serializers.CharField(), read_only=True)
exercise_uuid = serializers.ReadOnlyField(source='exercise.uuid')
+ thumbnails = serializers.SerializerMethodField()
class Meta:
model = ExerciseImage
@@ -129,6 +132,7 @@ class ExerciseImageSerializer(serializers.ModelSerializer):
'exercise',
'exercise_uuid',
'image',
+ 'thumbnails',
'is_main',
'style',
'license',
@@ -141,6 +145,23 @@ class ExerciseImageSerializer(serializers.ModelSerializer):
'is_ai_generated',
)
+ def get_thumbnails(self, obj: ExerciseImage):
+ if not obj.image:
+ return None
+
+ request = self.context.get('request')
+ result = {}
+
+ thumbnailer = get_thumbnailer(obj.image)
+ try:
+ for alias in ('small', 'medium'):
+ opts = settings.THUMBNAIL_ALIASES[''][alias]
+ thumb = thumbnailer.get_thumbnail(opts)
+ result[alias] = request.build_absolute_uri(thumb.url)
+ except (InvalidImageFormatError, OSError):
+ return None
+ return result
+
class ExerciseVideoSerializer(serializers.ModelSerializer):
"""
diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.py
index 346776eac..0411c058a 100644
--- a/wger/exercises/tests/test_exercise_images.py
+++ b/wger/exercises/tests/test_exercise_images.py
@@ -15,6 +15,7 @@
# Django
from django.core.files import File
from django.test import SimpleTestCase
+from django.urls import reverse
# wger
from wger.core.tests import api_base_test
@@ -129,6 +130,21 @@ class MainImageTestCase(WgerTestCase):
self.assertTrue(image.is_ai_generated)
+ def test_api_exposes_thumbnails(self):
+ """
+ Tests that the exercise image API exposes thumbnail URLs
+ """
+ translation = Translation.objects.get(pk=2)
+ pk = self.save_image(translation.exercise, 'protestschwein.jpg')
+
+ response = self.client.get(reverse('exerciseimage-detail', kwargs={'pk': pk}))
+ self.assertEqual(response.status_code, 200)
+
+ thumbnails = response.json()['thumbnails']
+ self.assertEqual(set(thumbnails.keys()), {'small', 'medium'})
+ for url in thumbnails.values():
+ self.assertTrue(url.startswith('http'))
+
def test_replacing_image_keeps_new_file(self):
"""
Replacing the file on an existing ExerciseImage must keep the new file