From 2a1d4178d7423e897ffabc78c9767ea702a3b31b Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 24 Apr 2025 22:35:38 +0200 Subject: [PATCH] feat: Allow creating locations via API --- src/fellchensammlung/api/serializers.py | 8 +++- src/fellchensammlung/api/urls.py | 3 +- src/fellchensammlung/api/views.py | 63 ++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/fellchensammlung/api/serializers.py b/src/fellchensammlung/api/serializers.py index 69eb38f..64304b9 100644 --- a/src/fellchensammlung/api/serializers.py +++ b/src/fellchensammlung/api/serializers.py @@ -1,4 +1,4 @@ -from ..models import Animal, RescueOrganization, AdoptionNotice, Species, Image +from ..models import Animal, RescueOrganization, AdoptionNotice, Species, Image, Location from rest_framework import serializers @@ -51,3 +51,9 @@ class SpeciesSerializer(serializers.ModelSerializer): class Meta: model = Species fields = "__all__" + + +class LocationSerializer(serializers.ModelSerializer): + class Meta: + model = Location + fields = "__all__" diff --git a/src/fellchensammlung/api/urls.py b/src/fellchensammlung/api/urls.py index cb66385..ae82b08 100644 --- a/src/fellchensammlung/api/urls.py +++ b/src/fellchensammlung/api/urls.py @@ -1,7 +1,7 @@ from django.urls import path from .views import ( AdoptionNoticeApiView, - AnimalApiView, RescueOrganizationApiView, AddImageApiView, SpeciesApiView + AnimalApiView, RescueOrganizationApiView, AddImageApiView, SpeciesApiView, LocationApiView ) urlpatterns = [ @@ -13,4 +13,5 @@ urlpatterns = [ path("organizations//", 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"), + path("locations/", LocationApiView.as_view(), name="api-locations-list"), ] diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py index df59d5d..2a50c64 100644 --- a/src/fellchensammlung/api/views.py +++ b/src/fellchensammlung/api/views.py @@ -1,7 +1,8 @@ +from fellchensammlung.api.serializers import LocationSerializer from rest_framework.views import APIView from rest_framework.response import Response from django.db import transaction -from fellchensammlung.models import AdoptionNotice, Animal, Log, TrustLevel +from fellchensammlung.models import AdoptionNotice, Animal, Log, TrustLevel, Location from fellchensammlung.tasks import post_adoption_notice_save, post_rescue_org_save from rest_framework import status from rest_framework.permissions import IsAuthenticated @@ -274,3 +275,63 @@ class SpeciesApiView(APIView): species = Species.objects.all() serializer = SpeciesSerializer(species, many=True, context={"request": request}) return Response(serializer.data, status=status.HTTP_200_OK) + +class LocationApiView(APIView): + permission_classes = [IsAuthenticated] + + @extend_schema( + parameters=[ + { + 'name': 'id', + 'required': False, + 'description': 'ID of the location to retrieve.', + 'type': int + }, + ], + responses={200: LocationSerializer(many=True)} + ) + def get(self, request, *args, **kwargs): + """ + Retrieve a location + """ + location_id = kwargs.get("id") + if location_id: + try: + location = Location.objects.get(pk=location_id) + serializer = LocationSerializer(location, context={"request": request}) + return Response(serializer.data, status=status.HTTP_200_OK) + except Location.DoesNotExist: + return Response({"error": "Location not found."}, status=status.HTTP_404_NOT_FOUND) + locations = Location.objects.all() + serializer = LocationSerializer(locations, many=True, context={"request": request}) + return Response(serializer.data, status=status.HTTP_200_OK) + + @transaction.atomic + @extend_schema( + request=LocationSerializer, + responses={201: 'Location created successfully!'} + ) + def post(self, request, *args, **kwargs): + """ + API view to add a location + """ + serializer = LocationSerializer(data=request.data, context={'request': request}) + if not serializer.is_valid(): + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + location = serializer.save() + + # Log the action + Log.objects.create( + user=request.user, + action="add_location", + text=f"{request.user} added adoption notice {location.pk} via API", + ) + + # Return success response with new adoption notice details + return Response( + {"message": "Location created successfully!", "id": location.pk}, + status=status.HTTP_201_CREATED, + ) + +