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-25 17:12:39 +03:00
|
|
|
from django.db.models import Count
|
|
|
|
from random import choice
|
|
|
|
from passfinder.recomendations.service.service import generate_tour
|
|
|
|
from datetime import timedelta, datetime
|
|
|
|
from .consts import *
|
|
|
|
|
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 23:36:45 +03:00
|
|
|
CitySerializer,
|
|
|
|
InputRouteSerializer,
|
|
|
|
)
|
|
|
|
from passfinder.events.models import (
|
|
|
|
BasePoint,
|
|
|
|
Region,
|
|
|
|
City,
|
|
|
|
UserRoute,
|
|
|
|
UserRoutePoint,
|
|
|
|
UserRouteTransaction,
|
2023-05-23 00:33:04 +03:00
|
|
|
)
|
2023-05-21 20:46:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BuildRouteApiView(GenericAPIView):
|
2023-05-22 22:29:46 +03:00
|
|
|
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):
|
2023-05-22 22:29:46 +03:00
|
|
|
routes = []
|
|
|
|
for _ in range(10):
|
|
|
|
routes.append(
|
|
|
|
{
|
|
|
|
"name": "bebra",
|
2023-05-23 23:36:45 +03:00
|
|
|
"date": None,
|
2023-05-22 22:29:46 +03:00
|
|
|
"description": "bebra bebra bebra",
|
|
|
|
"points": PointSerializer(many=True).to_representation(
|
|
|
|
BasePoint.objects.order_by("?")[:10]
|
|
|
|
),
|
|
|
|
}
|
2023-05-21 20:46:07 +03:00
|
|
|
)
|
2023-05-22 22:29:46 +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)
|
2023-05-23 23:36:45 +03:00
|
|
|
data = serializer.data
|
2023-05-25 17:12:39 +03:00
|
|
|
city_id = data["city"]
|
2023-05-26 01:15:02 +03:00
|
|
|
start_date = datetime.strptime(data["date_from"], "%Y-%m-%d")
|
|
|
|
end_date = datetime.strptime(data["date_to"], "%Y-%m-%d")
|
2023-05-25 17:12:39 +03:00
|
|
|
|
|
|
|
if city_id:
|
|
|
|
region = get_object_or_404(City, oid=city_id)
|
2023-05-23 18:14:06 +03:00
|
|
|
else:
|
2023-05-26 01:15:02 +03:00
|
|
|
region = choice(
|
|
|
|
City.objects.annotate(points_count=Count("points")).filter(
|
|
|
|
title__in=city_in_hotels
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-05-25 17:12:39 +03:00
|
|
|
if not start_date and end_date:
|
|
|
|
tour_length = choice([timedelta(days=i) for i in range(1, 4)])
|
|
|
|
start_date = end_date - tour_length
|
|
|
|
if not end_date and start_date:
|
|
|
|
tour_length = choice([timedelta(days=i) for i in range(1, 4)])
|
|
|
|
end_date = end_date + tour_length
|
|
|
|
if not end_date and not start_date:
|
|
|
|
max_date = datetime.now() + timedelta(days=15)
|
|
|
|
start_date = choice([max_date - timedelta(days=i) for i in range(1, 5)])
|
|
|
|
tour_length = choice([timedelta(days=i) for i in range(1, 4)])
|
|
|
|
end_date = start_date + tour_length
|
|
|
|
|
|
|
|
print(request.user, region, start_date, end_date)
|
|
|
|
|
|
|
|
tour = generate_tour(request.user, region, start_date, end_date)
|
|
|
|
print(len(tour[1]))
|
2023-05-26 01:15:02 +03:00
|
|
|
|
2023-05-25 17:12:39 +03:00
|
|
|
return Response(data=tour[0])
|
2023-05-23 18:14:06 +03:00
|
|
|
|
2023-05-23 00:33:04 +03:00
|
|
|
|
|
|
|
class ListRegionApiView(ListAPIView):
|
|
|
|
serializer_class = RegionSerializer
|
|
|
|
queryset = Region.objects.all()
|
2023-05-23 23:36:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ListCityApiView(ListAPIView):
|
|
|
|
serializer_class = CitySerializer
|
2023-05-26 01:15:02 +03:00
|
|
|
queryset = (
|
|
|
|
City.objects.annotate(points_num=Count("points"))
|
|
|
|
.filter(points_num__gte=100)
|
|
|
|
.order_by("title")
|
|
|
|
)
|
2023-05-23 23:36:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class SaveRouteSerializer(GenericAPIView):
|
|
|
|
serializer_class = InputRouteSerializer
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = InputRouteSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
data = serializer.data
|
|
|
|
route = UserRoute.objects.create(user=self.request.user)
|
|
|
|
for point in data["points"]:
|
|
|
|
if point["type"] == "point":
|
|
|
|
UserRoutePoint.objects.create(
|
|
|
|
route=route, point=BasePoint.objects.get(oid=point["point"])
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
UserRouteTransaction.objects.create(
|
|
|
|
route=route,
|
|
|
|
point_from=BasePoint.objects.get(oid=point["point_from"]),
|
|
|
|
point_to=BasePoint.objects.get(oid=point["point_to"]),
|
|
|
|
)
|
|
|
|
|
|
|
|
return Response(data=data)
|