diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py index 5074f8a..f2b4b25 100644 --- a/src/fellchensammlung/api/views.py +++ b/src/fellchensammlung/api/views.py @@ -14,11 +14,22 @@ from .serializers import ( SpeciesSerializer, RescueOrgSerializer, ) from fellchensammlung.models import Animal, RescueOrganization, AdoptionNotice, Species, Image - +from drf_spectacular.utils import extend_schema class AdoptionNoticeApiView(APIView): permission_classes = [IsAuthenticated] + @extend_schema( + parameters=[ + { + 'name': 'id', + 'required': False, + 'description': 'ID of the adoption notice to retrieve.', + 'type': int + }, + ], + responses={200: AdoptionNoticeSerializer(many=True)} + ) def get(self, request, *args, **kwargs): """ Retrieve adoption notices with their related animals and images. @@ -36,9 +47,13 @@ class AdoptionNoticeApiView(APIView): return Response(serializer.data, status=status.HTTP_200_OK) @transaction.atomic + @extend_schema( + request=AdoptionNoticeSerializer, + responses={201: 'Adoption notice created successfully!'} + ) def post(self, request, *args, **kwargs): """ - API view to add an adoption notice.b + API view to add an adoption notice. """ serializer = AdoptionNoticeSerializer(data=request.data, context={'request': request}) if not serializer.is_valid(): @@ -69,6 +84,7 @@ class AdoptionNoticeApiView(APIView): ) + class AnimalApiView(APIView): permission_classes = [IsAuthenticated] @@ -102,15 +118,25 @@ class AnimalApiView(APIView): ) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - class RescueOrganizationApiView(APIView): permission_classes = [IsAuthenticated] + @extend_schema( + parameters=[ + { + 'name': 'id', + 'required': False, + 'description': 'ID of the rescue organization to retrieve.', + 'type': int + }, + ], + responses={200: RescueOrganizationSerializer(many=True)} + ) def get(self, request, *args, **kwargs): """ Get list of rescue organizations or a specific organization by ID. """ - org_id = kwargs .get("id") + org_id = kwargs.get("id") if org_id: try: organization = RescueOrganization.objects.get(pk=org_id) @@ -123,6 +149,10 @@ class RescueOrganizationApiView(APIView): return Response(serializer.data, status=status.HTTP_200_OK) @transaction.atomic + @extend_schema( + request=RescueOrgSerializer, # Document the request body + responses={201: 'Rescue organization created/updated successfully!'} + ) def post(self, request, *args, **kwargs): """ Create or update a rescue organization. @@ -131,7 +161,7 @@ class RescueOrganizationApiView(APIView): if serializer.is_valid(): rescue_org = serializer.save(owner=request.user) return Response( - {"message": "Rescue organization created/updated successfully successfully!", "id": rescue_org.id}, + {"message": "Rescue organization created/updated successfully!", "id": rescue_org.id}, status=status.HTTP_201_CREATED, ) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @@ -140,6 +170,10 @@ class AddImageApiView(APIView): permission_classes = [IsAuthenticated] @transaction.atomic + @extend_schema( + request=ImageCreateSerializer, + responses={201: 'Image added successfully!'} + ) def post(self, request, *args, **kwargs): """ Add an image to an animal or adoption notice. @@ -148,7 +182,7 @@ class AddImageApiView(APIView): if serializer.is_valid(): if serializer.validated_data["attach_to_type"] == "animal": object_to_attach_to = Animal.objects.get(id=serializer.validated_data["attach_to"]) - elif serializer.fields["attach_to_type"] == "adoption_notice": + elif serializer.validated_data["attach_to_type"] == "adoption_notice": object_to_attach_to = AdoptionNotice.objects.get(id=serializer.validated_data["attach_to"]) else: raise ValueError("Unknown attach_to_type given, should not happen. Check serializer") @@ -166,6 +200,9 @@ class AddImageApiView(APIView): class SpeciesApiView(APIView): permission_classes = [IsAuthenticated] + @extend_schema( + responses={200: SpeciesSerializer(many=True)} + ) def get(self, request, *args, **kwargs): """ Retrieve a list of species.