backend/passfinder/events/api/views.py

69 lines
2.3 KiB
Python
Raw Normal View History

2023-05-23 18:14:06 +03:00
from rest_framework.generics import GenericAPIView, ListAPIView, get_object_or_404
2023-05-21 20:46:07 +03:00
from rest_framework.response import Response
2023-05-23 18:14:06 +03:00
from drf_spectacular.utils import extend_schema
2023-05-21 20:46:07 +03:00
2023-05-23 00:33:04 +03:00
from passfinder.events.api.serializers import (
PointSerializer,
RouteSerializer,
RegionSerializer,
2023-05-23 18:14:06 +03:00
RouteInputSerializer,
2023-05-23 00:33:04 +03:00
)
from passfinder.events.models import BasePoint, Region
2023-05-21 20:46:07 +03:00
class BuildRouteApiView(GenericAPIView):
serializer_class = RouteSerializer
2023-05-21 20:46:07 +03:00
2023-05-23 18:14:06 +03:00
@extend_schema(responses={200: RouteSerializer(many=True)})
2023-05-21 20:46:07 +03:00
def get(self, request):
routes = []
for _ in range(10):
routes.append(
{
"name": "bebra",
"description": "bebra bebra bebra",
"points": PointSerializer(many=True).to_representation(
BasePoint.objects.order_by("?")[:10]
),
}
2023-05-21 20:46:07 +03:00
)
return Response(data=routes)
2023-05-23 00:33:04 +03:00
2023-05-23 18:14:06 +03:00
@extend_schema(
request=RouteInputSerializer, responses={200: RouteSerializer(many=True)}
)
def post(self, request):
serializer = RouteInputSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
region = serializer.data["region"]
routes = []
if region:
region = get_object_or_404(Region, oid=region)
for _ in range(2):
routes.append(
{
"name": "bebra",
"description": "bebra bebra bebra",
"points": PointSerializer(many=True).to_representation(
region.points.all().order_by("?")[:10]
),
}
)
else:
for _ in range(10):
routes.append(
{
"name": "bebra",
"description": "bebra bebra bebra",
"points": PointSerializer(many=True).to_representation(
BasePoint.objects.order_by("?")[:10]
),
}
)
return Response(data=routes)
2023-05-23 00:33:04 +03:00
class ListRegionApiView(ListAPIView):
serializer_class = RegionSerializer
queryset = Region.objects.all()