feat: Get optional photo
This commit is contained in:
@@ -2,6 +2,7 @@ from ..models import Animal, RescueOrganization, AdoptionNotice, Species, Image,
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer):
|
class AdoptionNoticeSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
location = serializers.PrimaryKeyRelatedField(
|
location = serializers.PrimaryKeyRelatedField(
|
||||||
queryset=Location.objects.all(),
|
queryset=Location.objects.all(),
|
||||||
@@ -38,10 +39,13 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer):
|
|||||||
url = serializers.SerializerMethodField()
|
url = serializers.SerializerMethodField()
|
||||||
location_hr = serializers.SerializerMethodField()
|
location_hr = serializers.SerializerMethodField()
|
||||||
coordinates = serializers.SerializerMethodField()
|
coordinates = serializers.SerializerMethodField()
|
||||||
|
image_url = serializers.SerializerMethodField()
|
||||||
|
image_alt = serializers.SerializerMethodField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AdoptionNotice
|
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):
|
def get_species(self, obj):
|
||||||
return None
|
return None
|
||||||
@@ -49,6 +53,18 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer):
|
|||||||
def get_url(self, obj):
|
def get_url(self, obj):
|
||||||
return obj.get_absolute_url()
|
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):
|
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
|
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.
|
It's not exactly a circle, because the earth is round.
|
||||||
"""
|
"""
|
||||||
if obj.location:
|
if obj.location:
|
||||||
longitude_addition = math.sin(obj.id)/2000
|
longitude_addition = math.sin(obj.id) / 2000
|
||||||
latitude_addition = math.cos(obj.id)/2000
|
latitude_addition = math.cos(obj.id) / 2000
|
||||||
return [obj.location.longitude+longitude_addition, obj.location.latitude+latitude_addition]
|
return [obj.location.longitude + longitude_addition, obj.location.latitude + latitude_addition]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_location_hr(self, obj):
|
def get_location_hr(self, obj):
|
||||||
|
@@ -145,6 +145,8 @@
|
|||||||
const url = e.features[0].properties.url;
|
const url = e.features[0].properties.url;
|
||||||
const description = e.features[0].properties.description;
|
const description = e.features[0].properties.description;
|
||||||
const location_hr = e.features[0].properties.location_hr;
|
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
|
// Ensure that if the map is zoomed out such that
|
||||||
// multiple copies of the feature are visible, the
|
// multiple copies of the feature are visible, the
|
||||||
@@ -153,21 +155,30 @@
|
|||||||
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
|
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
|
||||||
}
|
}
|
||||||
|
|
||||||
new maplibregl.Popup()
|
const description_column = `
|
||||||
.setLngLat(coordinates)
|
|
||||||
.setHTML(
|
|
||||||
`<div class="popup-content is-size-7">
|
|
||||||
<div class="columns is-centered-mobile">
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<strong><a class="is-size-7" href="${url}">${title}</a></strong><br>
|
<strong><a class="is-size-7" href="${url}">${title}</a></strong><br>
|
||||||
<span><strong>{% translate 'Ort' %}</strong>: ${location_hr}</span><br>
|
<span><strong>{% translate 'Ort' %}</strong>: ${location_hr}</span><br>
|
||||||
<p class="is-size-7">${truncate(description, 80, url)}</p>
|
<p class="is-size-7">${truncate(description, 80, url)}</p>
|
||||||
</div>
|
</div>
|
||||||
|
`;
|
||||||
|
let picture_column = '';
|
||||||
|
if (image_url) {
|
||||||
|
picture_column = `
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<figure class="image is-128x128">
|
<figure class="image is-128x128">
|
||||||
<img src="https://bulma.io/assets/images/placeholders/128x128.png" />
|
<img src="${image_url}" alt="${image_alt}"/>
|
||||||
</figure>
|
</figure>
|
||||||
</div>
|
</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
new maplibregl.Popup()
|
||||||
|
.setLngLat(coordinates)
|
||||||
|
.setHTML(`
|
||||||
|
<div class="popup-content is-size-7">
|
||||||
|
<div class="columns">
|
||||||
|
${description_column}
|
||||||
|
${picture_column}
|
||||||
</div>
|
</div>
|
||||||
</div>`
|
</div>`
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user