From 395cfa0e3b21211675a76e858aee383b2275d79a Mon Sep 17 00:00:00 2001 From: b1ek Date: Thu, 11 May 2023 09:52:32 +1000 Subject: [PATCH] add filters at back --- pairent_backend/pairent_app/urls.py | 1 + pairent_backend/pairent_app/views.py | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pairent_backend/pairent_app/urls.py b/pairent_backend/pairent_app/urls.py index e4d1450..4684834 100644 --- a/pairent_backend/pairent_app/urls.py +++ b/pairent_backend/pairent_app/urls.py @@ -8,4 +8,5 @@ urlpatterns = format_suffix_patterns([ path("apartament//", views.ApartamentViewSet.as_view({'get': 'retrieve'})), path("apartaments/comparison/", views.ApartamentGetManyViewSet.as_view({'get': 'retrieve'})), # пример: apartaments/comparison/?user_id=1 user_id - id пользователя path("apartaments/favorite/", views.ApartamentGetManyViewSet.as_view({'get': 'list'})), # пример: apartaments/favorite/?user_id=1 user_id - id пользователя + path("apartaments/filters/", views.ApartmentFilter.as_view({'post': 'list'})) ]) diff --git a/pairent_backend/pairent_app/views.py b/pairent_backend/pairent_app/views.py index 5cb75ce..6fb2521 100644 --- a/pairent_backend/pairent_app/views.py +++ b/pairent_backend/pairent_app/views.py @@ -1,11 +1,15 @@ from rest_framework import viewsets from rest_framework.response import Response -from rest_framework.views import APIView +from rest_framework.views import APIView, View +from rest_framework.request import Request + +from django.http import HttpResponseBadRequest from .models import Apartament, User from .serializer import (ApartamentListSerializer, ApartamentDetailSerializer) +import json class ApartamentViewSet(viewsets.ReadOnlyModelViewSet): """Вывод списка квартир или отдельной квартиры""" @@ -49,4 +53,23 @@ class ApartamentGetManyViewSet(viewsets.ReadOnlyModelViewSet): for i in queryset: apartaments.append(ApartamentListSerializer(i).data) return Response({'results': apartaments}) - \ No newline at end of file + +class ApartmentFilter(viewsets.ViewSet): + """Вывод списка квартир или отдельной квартиры""" + + def list(self, req: Request): + filters = dict(req.data); + unfiltered = ApartamentDetailSerializer(Apartament.objects.all()[:100], many=True).data; + filtered = []; + + for entry in unfiltered: + if (filters['price_range']['from'] >= entry['price'] and (filters['price_range']['to'] != -1 and filters['price_range']['to'] <= entry['price'])): + continue; + if (filters['area_range']['from'] >= entry['perimetrs'] and (filters['area_range']['to'] != -1 and filters['area_range']['to'] <= entry['perimetrs'])): + continue; + if (filters['rooms'] != -1 and entry['rooms'] != filters['rooms']): + continue; + + filtered.append(entry); + + return Response(filtered); \ No newline at end of file