made API for comparison

This commit is contained in:
Денис Сарапулов 2023-05-07 19:48:53 +10:00
parent c6a4784cd2
commit 76321d4306
2 changed files with 14 additions and 1 deletions

View File

@ -6,4 +6,5 @@ from . import views
urlpatterns = format_suffix_patterns([
path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})),
path("apartament/<int:pk>/", views.ApartamentViewSet.as_view({'get': 'retrieve'})),
path("comparison/", views.ApartamentComparisonAPIView.as_view())
])

View File

@ -1,4 +1,6 @@
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Apartament
from .serializer import (ApartamentListSerializer,
@ -16,4 +18,14 @@ class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
if self.action == 'list':
return ApartamentListSerializer
elif self.action == "retrieve":
return ApartamentDetailSerializer
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})