From ba72b4e59fb64cbd53198ccd65799e200d61c644 Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 15 Jun 2025 18:01:39 +0200 Subject: [PATCH] feat: Get optional photo --- src/fellchensammlung/api/serializers.py | 24 +++++++++++++++--- .../partials/bulma-partial-map.html | 25 +++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/fellchensammlung/api/serializers.py b/src/fellchensammlung/api/serializers.py index 974474c..20e9e64 100644 --- a/src/fellchensammlung/api/serializers.py +++ b/src/fellchensammlung/api/serializers.py @@ -2,6 +2,7 @@ from ..models import Animal, RescueOrganization, AdoptionNotice, Species, Image, from rest_framework import serializers import math + class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer): location = serializers.PrimaryKeyRelatedField( queryset=Location.objects.all(), @@ -38,10 +39,13 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer): url = serializers.SerializerMethodField() location_hr = serializers.SerializerMethodField() coordinates = serializers.SerializerMethodField() + image_url = serializers.SerializerMethodField() + image_alt = serializers.SerializerMethodField() class Meta: model = AdoptionNotice - fields = ('id', 'species', 'title', 'description', 'url', 'location_hr', 'coordinates') + fields = ('id', 'species', 'title', 'description', 'url', 'location_hr', 'coordinates', 'image_url', + 'image_alt') def get_species(self, obj): return None @@ -49,6 +53,18 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer): def get_url(self, obj): return obj.get_absolute_url() + def get_image_url(self, obj): + photo = obj.get_photo() + if photo is not None: + return obj.get_photo().image.url + return None + + def get_image_alt(self, obj): + photo = obj.get_photo() + if photo is not None: + return obj.get_photo().alt_text + return None + def get_coordinates(self, obj): """ Coordinates are randomly moved around real location, roughly in a circle. The object id is used as angle so that @@ -57,9 +73,9 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer): It's not exactly a circle, because the earth is round. """ if obj.location: - longitude_addition = math.sin(obj.id)/2000 - latitude_addition = math.cos(obj.id)/2000 - return [obj.location.longitude+longitude_addition, obj.location.latitude+latitude_addition] + longitude_addition = math.sin(obj.id) / 2000 + latitude_addition = math.cos(obj.id) / 2000 + return [obj.location.longitude + longitude_addition, obj.location.latitude + latitude_addition] return None def get_location_hr(self, obj): diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/bulma-partial-map.html b/src/fellchensammlung/templates/fellchensammlung/partials/bulma-partial-map.html index 77ca56d..c7bc83e 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/bulma-partial-map.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/bulma-partial-map.html @@ -145,6 +145,8 @@ const url = e.features[0].properties.url; const description = e.features[0].properties.description; const location_hr = e.features[0].properties.location_hr; + const image_url = e.features[0].properties.image_url; + const image_alt = e.features[0].properties.image_alt; // Ensure that if the map is zoomed out such that // multiple copies of the feature are visible, the @@ -153,21 +155,30 @@ coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360; } - new maplibregl.Popup() - .setLngLat(coordinates) - .setHTML( - `