From e08bcca6069a33a9f7d52e431019e4b9bfedbe91 Mon Sep 17 00:00:00 2001 From: through-your-tears Date: Sun, 7 Apr 2024 01:08:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D1=8B=D0=B4=D0=B0=D1=87=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=82=D0=BE=D1=87=D0=BA=D0=B0=D0=BC=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=80=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/organizations/repositories.py | 11 +++++++++++ src/organizations/views.py | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/organizations/repositories.py b/src/organizations/repositories.py index eab2762..72928c5 100644 --- a/src/organizations/repositories.py +++ b/src/organizations/repositories.py @@ -1,3 +1,5 @@ +from django.contrib.gis.geos import Polygon + from .models import Category, Location, Organization, OrganizationImage, Region from core.repositories import BaseRepository, ObjectDoesNotExist @@ -38,6 +40,15 @@ class OrganizationRepository(BaseRepository): def get_by_category(cls, category_id): return cls.model.objects.filter(category__pk=category_id) + @classmethod + def get_by_categories(cls, categories): + return cls.model.objects.filter(category__pk__in=categories) + + @classmethod + def get_for_map(cls, categories, coords1, coords2): + rectangle = Polygon.from_bbox((*coords1, *coords2)) + return cls.get_by_categories(categories).filter(location__within=rectangle) + class OrganizationImagesRepository(BaseRepository): model = OrganizationImage diff --git a/src/organizations/views.py b/src/organizations/views.py index b38ea36..6909e5a 100644 --- a/src/organizations/views.py +++ b/src/organizations/views.py @@ -61,6 +61,19 @@ class OrganizationSearchListAPIView(ListAPIView): return super().list(request, args, kwargs) +class OrganizationMapFilter(ListAPIView): + serializer_class = OrganizationListSerializer + + def get_queryset(self): + ids = list(map(int, self.request.query_params.get('id'))) + coords1 = (float(self.request.query_params.get('lat1')), float(self.request.query_params.get('lon1'))) + coords2 = (float(self.request.query_params.get('lat2')), float(self.request.query_params.get('lon2'))) + if ids and coords1 and coords2: + return OrganizationRepository.get_for_map(ids, coords1, coords2) + else: + return OrganizationRepository.all()[:50] + + class CategoryListAPIView(ListAPIView): pagination_class = PageNumberPagination queryset = CategoryRepository.all()