2023-05-03 11:56:13 +02:00
|
|
|
from rest_framework import viewsets
|
2023-05-07 11:48:53 +02:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
2023-04-10 09:34:57 +02:00
|
|
|
|
2023-05-01 12:39:34 +02:00
|
|
|
from .models import Apartament
|
2023-05-03 11:56:13 +02:00
|
|
|
from .serializer import (ApartamentListSerializer,
|
|
|
|
ApartamentDetailSerializer)
|
2023-05-01 12:39:34 +02:00
|
|
|
|
|
|
|
|
2023-05-03 11:56:13 +02:00
|
|
|
class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""Вывод списка квартир или отдельной квартиры"""
|
2023-05-01 12:39:34 +02:00
|
|
|
|
2023-05-03 11:56:13 +02:00
|
|
|
def get_queryset(self):
|
2023-05-01 12:39:34 +02:00
|
|
|
apartaments = Apartament.objects.all()
|
2023-05-03 11:56:13 +02:00
|
|
|
return apartaments
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'list':
|
|
|
|
return ApartamentListSerializer
|
|
|
|
elif self.action == "retrieve":
|
2023-05-07 11:48:53 +02:00
|
|
|
return ApartamentDetailSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ApartamentComparisonAPIView(APIView):
|
|
|
|
"""Вывод отдельных квартир для сравнения"""
|
|
|
|
def get(self, request):
|
|
|
|
apartaments_id = request.COOKIES["apartament_comparison_id"].split(',') # получение куки пример (123,453)
|
|
|
|
apartaments = []
|
|
|
|
for i in apartaments_id:
|
|
|
|
apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
|
|
|
|
return Response({"results": apartaments})
|