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-09 10:32:18 +02:00
|
|
|
from .models import Apartament, User
|
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-07 12:42:06 +02:00
|
|
|
print(apartaments)
|
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
|
|
|
|
|
2023-05-09 11:05:47 +02:00
|
|
|
class ApartamentGetManyViewSet(viewsets.ModelViewSet):
|
2023-05-07 11:48:53 +02:00
|
|
|
"""Вывод отдельных квартир для сравнения"""
|
2023-05-09 13:03:37 +02:00
|
|
|
def get_queryset(self): # получение id квартир для избранного или сравнения
|
2023-05-09 10:32:18 +02:00
|
|
|
pk = self.request.query_params.get("pk", None) # получение id пользователя
|
|
|
|
|
|
|
|
if self.action == "retrieve": # если для страницы сравнения
|
|
|
|
apartaments_id = User.objects.get(pk=pk).apartaments_for_comparison.split(',') # получение id квартир
|
|
|
|
elif self.action == "list": # если для страницы избранного
|
|
|
|
apartaments_id = User.objects.get(pk=pk).favorites_apartaments.split(',') # получение id квартир
|
|
|
|
|
2023-05-09 13:03:37 +02:00
|
|
|
queryset = []
|
|
|
|
for i in apartaments_id:
|
|
|
|
queryset.append(Apartament.objects.get(pk=i))
|
|
|
|
|
|
|
|
return queryset
|
2023-05-09 10:32:18 +02:00
|
|
|
|
2023-05-09 11:05:47 +02:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
2023-05-09 13:03:37 +02:00
|
|
|
queryset = self.get_queryset()
|
2023-05-09 11:05:47 +02:00
|
|
|
apartaments = []
|
2023-05-09 13:03:37 +02:00
|
|
|
for i in queryset:
|
|
|
|
apartaments.append(ApartamentDetailSerializer(i).data)
|
|
|
|
return Response({'results': apartaments})
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
apartaments = []
|
|
|
|
for i in queryset:
|
|
|
|
apartaments.append(ApartamentListSerializer(i).data)
|
2023-05-09 11:05:47 +02:00
|
|
|
return Response({'results': apartaments})
|
|
|
|
|