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()