feat: Add rescue orgs as geojson via API

This commit is contained in:
2025-06-17 22:43:14 +02:00
parent ef9ac58c0f
commit 671c6ec6f5
4 changed files with 42 additions and 3 deletions

View File

@@ -21,7 +21,7 @@ class GeoJSONRenderer(BaseRenderer):
"properties": {
k: v for k, v in item.items()
},
"id": f"adoptionnotice/{item['id']}"
"id": f"{item['id']}"
}
features.append(feature)

View File

@@ -83,6 +83,38 @@ class AdoptionNoticeGeoJSONSerializer(serializers.ModelSerializer):
return f"{obj.location.city}"
return None
class RescueOrgeGeoJSONSerializer(serializers.ModelSerializer):
name = serializers.CharField()
url = serializers.SerializerMethodField()
location_hr = serializers.SerializerMethodField()
coordinates = serializers.SerializerMethodField()
class Meta:
model = AdoptionNotice
fields = ('id', 'name', 'description', 'url', 'location_hr', 'coordinates')
def get_url(self, obj):
return obj.get_absolute_url()
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
points are always displayed at the same location (as if they were a seed for a random function).
It's not exactly a circle, because the earth is round.
"""
if obj.location:
return [obj.location.longitude, obj.location.latitude]
return None
def get_location_hr(self, obj):
if obj.location.city:
return f"{obj.location.city}"
elif obj.location:
return f"{obj.location}"
return None
class AnimalCreateSerializer(serializers.ModelSerializer):
class Meta:

View File

@@ -2,7 +2,7 @@ from django.urls import path
from .views import (
AdoptionNoticeApiView,
AnimalApiView, RescueOrganizationApiView, AddImageApiView, SpeciesApiView, LocationApiView,
AdoptionNoticeGeoJSONView
AdoptionNoticeGeoJSONView, RescueOrgGeoJSONView
)
urlpatterns = [
@@ -12,6 +12,7 @@ urlpatterns = [
path("animals/", AnimalApiView.as_view(), name="api-animal-list"),
path("animals/<int:id>/", AnimalApiView.as_view(), name="api-animal-detail"),
path("organizations/", RescueOrganizationApiView.as_view(), name="api-organization-list"),
path("organizations.geojson", RescueOrgGeoJSONView.as_view(), name="api-organization-list-geojson"),
path("organizations/<int:id>/", RescueOrganizationApiView.as_view(), name="api-organization-detail"),
path("images/", AddImageApiView.as_view(), name="api-add-image"),
path("species/", SpeciesApiView.as_view(), name="api-species-list"),

View File

@@ -14,7 +14,7 @@ from .renderers import GeoJSONRenderer
from .serializers import (
AnimalGetSerializer,
AnimalCreateSerializer,
RescueOrganizationSerializer,
RescueOrgeGeoJSONSerializer,
AdoptionNoticeSerializer,
ImageCreateSerializer,
SpeciesSerializer, RescueOrganizationSerializer,
@@ -364,3 +364,9 @@ class AdoptionNoticeGeoJSONView(ListAPIView):
adoptionnoticestatus__major_status=AdoptionNoticeStatus.ACTIVE)
serializer_class = AdoptionNoticeGeoJSONSerializer
renderer_classes = [GeoJSONRenderer]
class RescueOrgGeoJSONView(ListAPIView):
queryset = RescueOrganization.objects.select_related('location').filter(location__isnull=False)
serializer_class = RescueOrgeGeoJSONSerializer
renderer_classes = [GeoJSONRenderer]