backend/passfinder/recomendations/api/views.py

68 lines
2.6 KiB
Python
Raw Normal View History

2023-05-21 13:37:21 +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
from random import choice
from rest_framework.decorators import action
from rest_framework.response import Response
from .serializers import TinderProceedSerializer
2023-05-21 17:13:16 +03:00
from passfinder.recomendations.models import UserPreferences
from ..service.service import update_preferences_state, get_next_tinder, get_personal_concerts_recommendation, \
get_personal_plays_recommendation, get_personal_movies_recommendation
from django.views.decorators.csrf import csrf_exempt
2023-05-21 13:37:21 +03:00
class TinderView(viewsets.GenericViewSet):
serializer_class = EventSerializer
model = Event
queryset = Event.objects.all()
@action(methods=['GET'], detail=False, serializer_class=EventSerializer)
def start(self, request: Request, *args: Any, **kwargs: Any):
2023-05-21 17:13:16 +03:00
UserPreferences.objects.get_or_create(user=request.user)
2023-05-21 13:37:21 +03:00
event = EventSerializer(choice(Event.objects.all()))
return Response(data=event.data, status=200)
2023-05-21 17:13:16 +03:00
@csrf_exempt
2023-05-21 13:37:21 +03:00
@action(methods=['POST'], detail=True, serializer_class=TinderProceedSerializer)
2023-05-21 17:13:16 +03:00
def proceed(self, request: Request, pk):
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'])
if event is None:
return Response(data={}, status=404)
return Response(data={'event': EventSerializer(event).data}, status=200)
class PersonalRecommendation(viewsets.GenericViewSet):
serializer_class = EventSerializer
model = Event
queryset = Event.objects.all()
@action(methods=['GET'], detail=False)
def plays(self, request, *args, **kwargs):
recs = get_personal_plays_recommendation(request.user)
ans = []
for rec in recs:
ans.append(EventSerializer(rec[1]).data)
return Response(ans, 200)
@action(methods=['GET'], detail=False)
def concerts(self, request, *args, **kwargs):
recs = get_personal_concerts_recommendation(request.user)
ans = []
for rec in recs:
ans.append(EventSerializer(rec[1]).data)
return Response(ans, 200)
@action(methods=['GET'], detail=False)
def movies(self, request, *args, **kwargs):
recs = get_personal_movies_recommendation(request.user)
ans = []
for rec in recs:
ans.append(EventSerializer(rec[1]).data)
return Response(ans, 200)