feat: Add annotation for API
This commit is contained in:
parent
aefeffd63a
commit
4ef9da953c
@ -14,11 +14,22 @@ from .serializers import (
|
|||||||
SpeciesSerializer, RescueOrgSerializer,
|
SpeciesSerializer, RescueOrgSerializer,
|
||||||
)
|
)
|
||||||
from fellchensammlung.models import Animal, RescueOrganization, AdoptionNotice, Species, Image
|
from fellchensammlung.models import Animal, RescueOrganization, AdoptionNotice, Species, Image
|
||||||
|
from drf_spectacular.utils import extend_schema
|
||||||
|
|
||||||
class AdoptionNoticeApiView(APIView):
|
class AdoptionNoticeApiView(APIView):
|
||||||
permission_classes = [IsAuthenticated]
|
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):
|
def get(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Retrieve adoption notices with their related animals and images.
|
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)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
@extend_schema(
|
||||||
|
request=AdoptionNoticeSerializer,
|
||||||
|
responses={201: 'Adoption notice created successfully!'}
|
||||||
|
)
|
||||||
def post(self, request, *args, **kwargs):
|
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})
|
serializer = AdoptionNoticeSerializer(data=request.data, context={'request': request})
|
||||||
if not serializer.is_valid():
|
if not serializer.is_valid():
|
||||||
@ -69,6 +84,7 @@ class AdoptionNoticeApiView(APIView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AnimalApiView(APIView):
|
class AnimalApiView(APIView):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
@ -102,15 +118,25 @@ class AnimalApiView(APIView):
|
|||||||
)
|
)
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
class RescueOrganizationApiView(APIView):
|
class RescueOrganizationApiView(APIView):
|
||||||
permission_classes = [IsAuthenticated]
|
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):
|
def get(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Get list of rescue organizations or a specific organization by ID.
|
Get list of rescue organizations or a specific organization by ID.
|
||||||
"""
|
"""
|
||||||
org_id = kwargs .get("id")
|
org_id = kwargs.get("id")
|
||||||
if org_id:
|
if org_id:
|
||||||
try:
|
try:
|
||||||
organization = RescueOrganization.objects.get(pk=org_id)
|
organization = RescueOrganization.objects.get(pk=org_id)
|
||||||
@ -123,6 +149,10 @@ class RescueOrganizationApiView(APIView):
|
|||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
@extend_schema(
|
||||||
|
request=RescueOrgSerializer, # Document the request body
|
||||||
|
responses={201: 'Rescue organization created/updated successfully!'}
|
||||||
|
)
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create or update a rescue organization.
|
Create or update a rescue organization.
|
||||||
@ -131,7 +161,7 @@ class RescueOrganizationApiView(APIView):
|
|||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
rescue_org = serializer.save(owner=request.user)
|
rescue_org = serializer.save(owner=request.user)
|
||||||
return Response(
|
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,
|
status=status.HTTP_201_CREATED,
|
||||||
)
|
)
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
@ -140,6 +170,10 @@ class AddImageApiView(APIView):
|
|||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
@extend_schema(
|
||||||
|
request=ImageCreateSerializer,
|
||||||
|
responses={201: 'Image added successfully!'}
|
||||||
|
)
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add an image to an animal or adoption notice.
|
Add an image to an animal or adoption notice.
|
||||||
@ -148,7 +182,7 @@ class AddImageApiView(APIView):
|
|||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
if serializer.validated_data["attach_to_type"] == "animal":
|
if serializer.validated_data["attach_to_type"] == "animal":
|
||||||
object_to_attach_to = Animal.objects.get(id=serializer.validated_data["attach_to"])
|
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"])
|
object_to_attach_to = AdoptionNotice.objects.get(id=serializer.validated_data["attach_to"])
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unknown attach_to_type given, should not happen. Check serializer")
|
raise ValueError("Unknown attach_to_type given, should not happen. Check serializer")
|
||||||
@ -166,6 +200,9 @@ class AddImageApiView(APIView):
|
|||||||
class SpeciesApiView(APIView):
|
class SpeciesApiView(APIView):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
responses={200: SpeciesSerializer(many=True)}
|
||||||
|
)
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Retrieve a list of species.
|
Retrieve a list of species.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user