GM-127: finished
This commit is contained in:
parent
207e35feb1
commit
0e0d6e318e
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user