backend/passfinder/recomendations/api/views.py

197 lines
7.3 KiB
Python
Raw Normal View History

2023-05-27 11:13:41 +03:00
from typing import Any
from rest_framework import viewsets, mixins
from rest_framework.request import Request
from rest_framework.response import Response
from passfinder.events.models import Event
from passfinder.events.api.serializers import EventSerializer, HotelSerializer
from random import choice
from rest_framework.decorators import action
from rest_framework.response import Response
from .serializers import *
from passfinder.recomendations.models import UserPreferences
from ..service.service import *
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Count
from random import sample
2023-05-28 16:52:03 +03:00
from passfinder.events.api.serializers import RouteInputSerializer
from passfinder.events.api.consts import city_in_hotels
2023-05-27 11:13:41 +03:00
class TinderView(viewsets.GenericViewSet):
serializer_class = EventSerializer
model = Event
queryset = Event.objects.all()
2023-05-28 00:11:44 +03:00
@action(methods=["GET"], detail=False, serializer_class=EventSerializer)
2023-05-27 11:13:41 +03:00
def start(self, request: Request, *args: Any, **kwargs: Any):
UserPreferences.objects.get_or_create(user=request.user)
event = EventSerializer(choice(Event.objects.all()))
return Response(data=event.data, status=200)
2023-05-28 00:11:44 +03:00
2023-05-27 11:13:41 +03:00
@csrf_exempt
2023-05-28 00:11:44 +03:00
@action(methods=["POST"], detail=True, serializer_class=TinderProceedSerializer)
2023-05-27 11:13:41 +03:00
def proceed(self, request: Request, pk):
2023-05-28 00:11:44 +03:00
update_preferences_state(
request.user, Event.objects.get(oid=pk), request.data["action"]
)
event = get_next_tinder(
request.user, Event.objects.get(oid=pk), request.data["action"]
)
2023-05-27 11:13:41 +03:00
if event is None:
return Response(data={}, status=404)
2023-05-28 00:11:44 +03:00
return Response(data={"event": EventSerializer(event).data}, status=200)
2023-05-27 11:13:41 +03:00
2023-05-28 00:11:44 +03:00
@action(
methods=["POST"], detail=False, serializer_class=TinderGetEventFilterSerializer
)
2023-05-27 11:13:41 +03:00
def get_event(self, request: Request):
# отдавать под пользователя
2023-05-28 00:11:44 +03:00
events = Event.objects.filter(type__in=request.data["type"])
return Response(
data={"event": EventSerializer(choice(events)).data}, status=200
)
2023-05-27 11:13:41 +03:00
class PersonalRecommendation(viewsets.GenericViewSet):
serializer_class = EventSerializer
model = Event
queryset = Event.objects.all()
2023-05-28 00:11:44 +03:00
@action(methods=["GET"], detail=False, serializer_class=SelfRecomendationSerializer)
def recommendations(self, request, *args, **kwargs):
return Response(data=get_personal_recomendations(request.user), status=200)
2023-05-24 17:58:51 +03:00
2023-05-28 00:11:44 +03:00
@action(methods=["GET"], detail=True)
2023-05-27 11:13:41 +03:00
def get_nearest_user_distance(self, request, pk, *args, **kwargs):
request_point = pk
if len(Event.objects.filter(oid=pk)):
request_point = Event.objects.get(oid=pk)
elif len(Restaurant.objects.filter(oid=pk)):
request_point = Restaurant.objects.get(oid=pk)
else:
request_point = Hotel.objects.get(oid=pk)
res = nearest_distance_points(request_point, request.user)
return Response(
2023-05-28 00:11:44 +03:00
data=list(map(lambda event: EventSerializer(event).data, res)), status=200
2023-05-27 11:13:41 +03:00
)
2023-05-28 00:11:44 +03:00
@action(
methods=["GET"], detail=False, serializer_class=DailySelectionSerializerInput()
)
2023-05-27 11:13:41 +03:00
def get_daily_selection(self, request, *args, **kwargs):
2023-05-28 00:11:44 +03:00
city = choice(
City.objects.annotate(points_count=Count("points")).filter(
points_count__gt=200
)
)
2023-05-27 11:13:41 +03:00
events = sample(list(Event.objects.filter(city=city)), 10)
return Response(
data={
2023-05-28 00:11:44 +03:00
"city": city.title,
"events": list(map(lambda event: EventSerializer(event).data, events)),
2023-05-27 11:13:41 +03:00
}
)
2023-05-28 00:11:44 +03:00
@action(methods=["POST"], detail=False, serializer_class=DailySelectionSerializer)
2023-05-27 11:13:41 +03:00
def generate_daily_selection(self, request, *args, **kwargs):
points = []
2023-05-28 16:52:03 +03:00
print(request.data['nodes'])
2023-05-28 00:11:44 +03:00
for point in request.data["nodes"]:
if point["action"] == "right":
points.append(Event.objects.get(oid=point["oid"]))
2023-05-27 11:13:41 +03:00
path = generate_points_path(request.user, points, 3)
2023-05-28 00:11:44 +03:00
return Response(data={"path": path})
2023-05-28 16:52:03 +03:00
@action(methods=['POST'], detail=False, serializer_class=RouteInputSerializer)
def build_events(self, request, *args, **kwargs):
serializer = RouteInputSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data =serializer.data
what_to_see = data["what_to_see"]
if what_to_see is None:
what_to_see = [
"attractions",
"museum",
"movie",
"concert",
"artwork",
"plays",
"shop",
"gallery",
"theme_park",
"viewpoint",
"zoo",
]
city_id = data["city"]
allowed_regions = []
if city_id:
allowed_regions = [City.objects.get(oid=city_id)]
else:
allowed_regions = sample(
list(City.objects.annotate(points_count=Count("points"))
.filter(title__in=city_in_hotels)
.filter(points_count__gt=400)), 5)
return Response(data=get_events(request.user, allowed_regions, what_to_see))
2023-05-28 00:11:44 +03:00
2023-05-27 11:13:41 +03:00
class OnboardingViewset(viewsets.GenericViewSet):
serializer_class = EventSerializer
model = Event
queryset = Event.objects.all()
2023-05-28 00:11:44 +03:00
@action(methods=["POST"], detail=False, serializer_class=HotelOnboardingRetrieve)
2023-05-27 11:13:41 +03:00
def hotels(self, reqeust, *args, **kwargs):
2023-05-28 00:11:44 +03:00
hotels = get_onboarding_hotels(reqeust.data["stars"])
res = HotelOnboardingRetrieve({"hotels": hotels}).data
2023-05-27 11:13:41 +03:00
return Response(res, 200)
2023-05-28 00:11:44 +03:00
@action(methods=["POST"], detail=False, serializer_class=EventOnboardingRetrieve)
2023-05-27 11:13:41 +03:00
def event(self, request, *args, **kwargs):
events = get_onboarding_attractions()
2023-05-28 00:11:44 +03:00
res = EventOnboardingRetrieve({"events": events}).data
2023-05-27 11:13:41 +03:00
return Response(res, 200)
2023-05-28 00:11:44 +03:00
@action(methods=["GET"], detail=True)
2023-05-27 11:13:41 +03:00
def add_to_favorites(self, request, pk, *args, **kwargs):
pref, _ = UserPreferences.objects.get_or_create(user=request.user)
event = Event.objects.get(oid=pk)
2023-05-28 00:11:44 +03:00
if event.type == "attraction":
2023-05-27 11:13:41 +03:00
pref.prefferred_attractions.add(event)
2023-05-28 00:11:44 +03:00
elif event.type == "museum":
2023-05-27 11:13:41 +03:00
pref.prefferred_museums.add(event)
2023-05-28 00:11:44 +03:00
elif event.type == "movie":
2023-05-27 11:13:41 +03:00
pref.preffered_movies.add(event)
2023-05-28 00:11:44 +03:00
elif event.type == "play":
2023-05-27 11:13:41 +03:00
pref.preffered_plays.add(event)
2023-05-28 00:11:44 +03:00
elif event.type == "concert":
2023-05-27 11:13:41 +03:00
pref.preferred_concerts.add(event)
2023-05-28 00:11:44 +03:00
2023-05-27 11:13:41 +03:00
pref.save()
2023-05-28 00:11:44 +03:00
2023-05-27 11:13:41 +03:00
return Response(status=200)
2023-05-28 00:11:44 +03:00
@action(methods=["POST"], detail=False, serializer_class=StarSelectionSerializer)
2023-05-27 22:32:52 +03:00
def set_hotel_stars(self, request, *args, **kwargs):
up, _ = UserPreferences.objects.get_or_create(user=request.user)
2023-05-28 00:11:44 +03:00
up.preferred_stars = request.data["stars"]
2023-05-27 22:32:52 +03:00
up.save()
return Response(status=200)
2023-05-28 00:11:44 +03:00
@action(
methods=["POST"], detail=False, serializer_class=CategorySelectionSerializer
)
2023-05-27 22:32:52 +03:00
def set_categories(self, request, *args, **kwargs):
up, _ = UserPreferences.objects.get_or_create(user=request.user)
2023-05-28 00:11:44 +03:00
up.preferred_categories = request.data["categories"]
2023-05-27 22:32:52 +03:00
up.save()
return Response(status=200)