add filters at back

This commit is contained in:
b1ek 2023-05-11 09:52:32 +10:00
parent 9966915e7f
commit 395cfa0e3b
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 26 additions and 2 deletions

View File

@ -8,4 +8,5 @@ urlpatterns = format_suffix_patterns([
path("apartament/<int:pk>/", 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'}))
])

View File

@ -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})
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);