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([ urlpatterns = format_suffix_patterns([
path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})), path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})),
path("apartament/<int:pk>/", views.ApartamentViewSet.as_view({'get': 'retrieve'})), 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 import viewsets
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Apartament from .models import Apartament
from .serializer import (ApartamentListSerializer, from .serializer import (ApartamentListSerializer,
@ -17,3 +19,13 @@ class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
return ApartamentListSerializer return ApartamentListSerializer
elif self.action == "retrieve": 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})