From 5943839edacf569a147f1064fbe4f396240aeafb Mon Sep 17 00:00:00 2001 From: Semyon Date: Thu, 12 Sep 2019 14:24:48 +0300 Subject: [PATCH 1/2] Added view for nearest establishments --- apps/establishment/models.py | 14 ++++++++++++++ apps/establishment/views.py | 25 ++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index efe665ef..a0c9c44c 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -102,6 +102,20 @@ class EstablishmentQuerySet(models.QuerySet): default=False, output_field=models.BooleanField(default=False))) + def by_distance_from_point(self, center, radius, unit='m'): + """ + Returns nearest establishments + + :param center: point from which to find nearby establishments + :param radius: the maximum distance within the radius of which to look for establishments + :return: all establishments within the specified radius of specified point + :param unit: length unit e.g. m, km. Default is 'm'. + """ + + from django.contrib.gis.measure import Distance + kwargs = {unit: radius} + return self.filter(address__coordinates__distance_lte=(center, Distance(**kwargs))) + class Establishment(ProjectBaseMixin, ImageMixin, TranslatedFieldsMixin): """Establishment model.""" diff --git a/apps/establishment/views.py b/apps/establishment/views.py index 528d2377..7745af2d 100644 --- a/apps/establishment/views.py +++ b/apps/establishment/views.py @@ -111,9 +111,28 @@ class EstablishmentFavoritesCreateDestroyView(generics.CreateAPIView, generics.D obj = get_object_or_404( self.request.user.favorites.by_user(user=self.request.user) - .by_content_type(app_label='establishment', - model='establishment') - .by_object_id(object_id=self.kwargs['pk'])) + .by_content_type(app_label='establishment', + model='establishment') + .by_object_id(object_id=self.kwargs['pk'])) # May raise a permission denied self.check_object_permissions(self.request, obj) return obj + + +class EstablishmentNearestRetrieveView(EstablishmentMixin, JWTGenericViewMixin, generics.ListAPIView): + """Resource for getting list of nearest establishments.""" + serializer_class = serializers.EstablishmentListSerializer + filter_class = filters.EstablishmentFilter + + def get_queryset(self): + """Overrided method 'get_queryset'.""" + from django.contrib.gis.geos import Point + + center = Point(float(self.request.query_params["lat"]), float(self.request.query_params["lon"])) + radius = float(self.request.query_params["radius"]) + unit = self.request.query_params.get("unit", None) + by_distance_from_point_kwargs = {"center": center, "radius": radius, "unit": unit} + return super(EstablishmentNearestRetrieveView, self).get_queryset() \ + .by_distance_from_point(**{k: v for k, v in by_distance_from_point_kwargs.items() if v is not None}) \ + .by_country_code(code=self.request.country_code) \ + .annotate_in_favorites(user=self.request.user) From bdc2c327f6fe4d3fe4d0ae827e4d063be076892b Mon Sep 17 00:00:00 2001 From: Semyon Date: Thu, 12 Sep 2019 14:25:03 +0300 Subject: [PATCH 2/2] Registered path for nearest establishments --- apps/establishment/urls/common.py | 5 ++--- apps/establishment/urls/mobile.py | 11 +++++++++++ project/urls/mobile.py | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 apps/establishment/urls/mobile.py diff --git a/apps/establishment/urls/common.py b/apps/establishment/urls/common.py index 0f2958eb..ff6af5c8 100644 --- a/apps/establishment/urls/common.py +++ b/apps/establishment/urls/common.py @@ -5,7 +5,6 @@ from establishment import views app_name = 'establishment' - urlpatterns = [ path('', views.EstablishmentListView.as_view(), name='list'), path('/', views.EstablishmentRetrieveView.as_view(), name='detail'), @@ -15,5 +14,5 @@ urlpatterns = [ path('/comments//', views.EstablishmentCommentRUDView.as_view(), name='rud-comment'), path('/favorites/', views.EstablishmentFavoritesCreateDestroyView.as_view(), - name='add-favorites'), -] \ No newline at end of file + name='add-favorites') +] diff --git a/apps/establishment/urls/mobile.py b/apps/establishment/urls/mobile.py new file mode 100644 index 00000000..2803be18 --- /dev/null +++ b/apps/establishment/urls/mobile.py @@ -0,0 +1,11 @@ +"""Establishment url patterns.""" +from django.urls import path + +from establishment import views +from establishment.urls.common import urlpatterns as common_urlpatterns + +urlpatterns = [ + path('geo/', views.EstablishmentNearestRetrieveView.as_view(), name='nearest-establishments-list') +] + +urlpatterns.extend(common_urlpatterns) diff --git a/project/urls/mobile.py b/project/urls/mobile.py index c007e260..0bcbd31c 100644 --- a/project/urls/mobile.py +++ b/project/urls/mobile.py @@ -3,10 +3,11 @@ from django.urls import path, include app_name = 'mobile' urlpatterns = [ + path('establishments/', include('establishment.urls.mobile')), # path('account/', include('account.urls.web')), # path('advertisement/', include('advertisement.urls.web')), # path('collection/', include('collection.urls.web')), # path('establishments/', include('establishment.urls.web')), # path('news/', include('news.urls.web')), # path('partner/', include('partner.urls.web')), -] \ No newline at end of file +]