From 0e0d6e318e1593716203273a559d1529b8fa5749 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 26 Sep 2019 14:12:49 +0300 Subject: [PATCH] GM-127: finished --- apps/establishment/models.py | 19 +++++++++---------- apps/establishment/views/web.py | 10 ++++++---- apps/main/methods.py | 16 +++++++++++++++- apps/utils/middleware.py | 11 ----------- apps/utils/pagination.py | 6 ++++-- project/settings/base.py | 11 ++++++++++- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 666b7194..aed78d9f 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -114,7 +114,7 @@ class EstablishmentQuerySet(models.QuerySet): models.When( collections__collection_type=Collection.POP, public_mark__isnull=True, - then=10 + then=settings.DEFAULT_ESTABLISHMENT_PUBLIC_MARK ), default='public_mark', output_field=models.FloatField())) @@ -135,23 +135,22 @@ class EstablishmentQuerySet(models.QuerySet): Return QuerySet with objects that similar to Establishment. :param establishment_slug: str Establishment slug """ - establishment_qs = Establishment.objects.filter(slug=establishment_slug, - public_mark__isnull=False) + establishment_qs = self.filter(slug=establishment_slug, + public_mark__isnull=False) if establishment_qs.exists(): establishment = establishment_qs.first() subquery_filter_by_distance = Subquery( self.exclude(slug=establishment_slug) .filter(image_url__isnull=False, public_mark__gte=10) - .published() .has_published_reviews() - .annotate_distance(point=establishment.address.coordinates) + .annotate_distance(point=establishment.location) .order_by('distance')[:settings.LIMITING_QUERY_NUMBER] .values('id') ) - return Establishment.objects.filter(id__in=subquery_filter_by_distance) \ - .annotate_intermediate_public_mark() \ - .annotate_mark_similarity(mark=establishment.public_mark) \ - .order_by('mark_similarity') + return self.filter(id__in=subquery_filter_by_distance) \ + .annotate_intermediate_public_mark() \ + .annotate_mark_similarity(mark=establishment.public_mark) \ + .order_by('mark_similarity') else: return self.none() @@ -168,7 +167,7 @@ class EstablishmentQuerySet(models.QuerySet): favorite_establishments = [] if user.is_authenticated: favorite_establishments = user.favorites.by_content_type(app_label='establishment', - model='establishment')\ + model='establishment') \ .values_list('object_id', flat=True) return self.annotate(in_favorites=models.Case( models.When( diff --git a/apps/establishment/views/web.py b/apps/establishment/views/web.py index 5a69bd87..62b12016 100644 --- a/apps/establishment/views/web.py +++ b/apps/establishment/views/web.py @@ -1,5 +1,6 @@ """Establishment app views.""" +from django.conf import settings from django.contrib.gis.geos import Point from django.shortcuts import get_object_or_404 from rest_framework import generics, permissions @@ -8,9 +9,10 @@ from comment import models as comment_models from establishment import filters from establishment import models, serializers from establishment.views import EstablishmentMixin +from main import methods from main.models import MetaDataContent -from utils.pagination import EstablishmentPagination from timetable.serialziers import ScheduleRUDSerializer, ScheduleCreateSerializer +from utils.pagination import EstablishmentPortionPagination class EstablishmentListView(EstablishmentMixin, generics.ListAPIView): @@ -28,12 +30,12 @@ class EstablishmentListView(EstablishmentMixin, generics.ListAPIView): class EstablishmentSimilarListView(EstablishmentListView): """Resource for getting a list of establishments.""" serializer_class = serializers.EstablishmentListSerializer - pagination_class = EstablishmentPagination + pagination_class = EstablishmentPortionPagination def get_queryset(self): """Override get_queryset method""" - return super().get_queryset() \ - .similar(establishment_slug=self.kwargs.get('slug')) + qs = super().get_queryset() + return qs.similar(establishment_slug=self.kwargs.get('slug')) class EstablishmentRetrieveView(EstablishmentMixin, generics.RetrieveAPIView): diff --git a/apps/main/methods.py b/apps/main/methods.py index e9ec780c..67da3480 100644 --- a/apps/main/methods.py +++ b/apps/main/methods.py @@ -1,9 +1,10 @@ """Main app methods.""" import logging + from django.conf import settings from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception -from main import models +from main import models logger = logging.getLogger(__name__) @@ -38,6 +39,19 @@ def determine_country_code(ip_addr): return country_code +def determine_coordinates(ip_addr): + longitude, latitude = None, None + if ip_addr: + try: + geoip = GeoIP2() + longitude, latitude = geoip.coords(ip_addr) + except GeoIP2Exception as ex: + logger.info(f'GEOIP Exception: {ex}. ip: {ip_addr}') + except Exception as ex: + logger.error(f'GEOIP Base exception: {ex}') + return longitude, latitude + + def determine_user_site_url(country_code): """Determine user's site url.""" try: diff --git a/apps/utils/middleware.py b/apps/utils/middleware.py index 7203127c..096f8474 100644 --- a/apps/utils/middleware.py +++ b/apps/utils/middleware.py @@ -31,14 +31,3 @@ def parse_cookies(get_response): response = get_response(request) return response return middleware - - -class CORSMiddleware: - """Added parameter {Access-Control-Allow-Origin: *} to response""" - def __init__(self, get_response): - self.get_response = get_response - - def __call__(self, request): - response = self.get_response(request) - response["Access-Control-Allow-Origin"] = '*' - return response diff --git a/apps/utils/pagination.py b/apps/utils/pagination.py index efaf329e..2c9e92e5 100644 --- a/apps/utils/pagination.py +++ b/apps/utils/pagination.py @@ -1,6 +1,8 @@ """Pagination settings.""" from base64 import b64encode from urllib import parse as urlparse + +from django.conf import settings from rest_framework.pagination import PageNumberPagination, CursorPagination @@ -46,8 +48,8 @@ class ProjectMobilePagination(ProjectPageNumberPagination): return self.page.previous_page_number() -class EstablishmentPagination(ProjectMobilePagination): +class EstablishmentPortionPagination(ProjectMobilePagination): """ Pagination for app establishments with limit page size equal to 12 """ - page_size = 12 + page_size = settings.LIMITING_OUTPUT_OBJECTS diff --git a/project/settings/base.py b/project/settings/base.py index 2007d908..10eb3544 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -413,4 +413,13 @@ SITE_REDIRECT_URL_UNSUBSCRIBE = '/unsubscribe/' SITE_NAME = 'Gault & Millau' -LIMITING_QUERY_NUMBER = 36 # Need to restrict objects to sort (used in establishments) +# Used in annotations for establishments. +DEFAULT_ESTABLISHMENT_PUBLIC_MARK = 10 +# Limit output objects (see in pagination classes). +LIMITING_OUTPUT_OBJECTS = 12 +# Need to restrict objects to sort (3 times more then expected). +LIMITING_QUERY_NUMBER = LIMITING_OUTPUT_OBJECTS * 3 + +# GEO +# A Spatial Reference System Identifier +GEO_DEFAULT_SRID = 4326