first steps for georestricted search results
This commit is contained in:
@@ -7,6 +7,8 @@ from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
|
||||
from django.db.models import Max, Min
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.contrib.gis.measure import D
|
||||
|
||||
import requests
|
||||
|
||||
@@ -124,7 +126,7 @@ def get_related_products(product):
|
||||
return total_results[:10]
|
||||
|
||||
|
||||
def ranked_product_search(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None):
|
||||
def ranked_product_search(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None, coordinates=None):
|
||||
"""
|
||||
Ranked product search
|
||||
|
||||
@@ -134,6 +136,12 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
|
||||
|
||||
allow filtering by:
|
||||
- shipping cost
|
||||
|
||||
Response includes:
|
||||
- result_set
|
||||
- min_price
|
||||
- max_price
|
||||
- georesult
|
||||
"""
|
||||
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name')
|
||||
query = SearchQuery(keyword)
|
||||
@@ -142,6 +150,24 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
|
||||
rank=SearchRank(vector, query)
|
||||
).filter(rank__gt=0.05, active=True)
|
||||
|
||||
# geolocation filtering
|
||||
if coordinates is not None:
|
||||
point = Point(coordinates)
|
||||
filtered_qs = products_qs.filter(geo__distance_lte=(point, D(km=10)))
|
||||
georesult = '10k'
|
||||
if filtered_qs.count() <= 10:
|
||||
products_qs = products_qs.filter(geo__distance_lte=(point, D(km=50)))
|
||||
georesult = '50k'
|
||||
if filtered_qs.count() <= 10:
|
||||
products_qs = products_qs.filter(geo__distance_lte=(point, D(km=200)))
|
||||
georesult = '200k'
|
||||
if filtered_qs.count > 10:
|
||||
products_qs = filtered_qs
|
||||
else:
|
||||
georesult = None
|
||||
else:
|
||||
georesult = None
|
||||
|
||||
# filter by category
|
||||
if category is not None:
|
||||
products_qs = products_qs.filter(category=category)
|
||||
@@ -183,7 +209,7 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
|
||||
max_price = products_qs.aggregate(Max('price'))
|
||||
|
||||
|
||||
return set(products_qs), min_price, max_price
|
||||
return set(products_qs), min_price, max_price, georesult
|
||||
|
||||
|
||||
def product_loader(csv_reader, user, company=None):
|
||||
|
||||
@@ -19,6 +19,7 @@ from rest_framework.decorators import api_view, permission_classes, action
|
||||
from rest_framework.filters import OrderingFilter
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
|
||||
import requests
|
||||
|
||||
from history.models import HistorySync
|
||||
@@ -28,7 +29,7 @@ from products.models import Product, CategoryTag
|
||||
from products.serializers import ProductSerializer, TagFilterSerializer, SearchResultSerializer
|
||||
from companies.models import Company
|
||||
from stats.models import StatsLog
|
||||
from back_latienda.permissions import IsCreator, IsSiteAdmin, ReadOnly
|
||||
from back_latienda.permissions import IsCreator, IsSiteAdmin
|
||||
from .utils import extract_search_filters, ranked_product_search, product_loader, get_related_products
|
||||
from utils.tag_serializers import TaggitSerializer
|
||||
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
||||
@@ -125,8 +126,8 @@ def product_search(request):
|
||||
|
||||
Params:
|
||||
- q: used for search [MANDATORY]
|
||||
- limit: max number of returned instances [OPTIONAL]
|
||||
- offset: where to start counting results [OPTIONAL]
|
||||
- limit: max number of returned instances
|
||||
- offset: where to start counting results
|
||||
- shipping_cost: true/false
|
||||
- discount: true/false
|
||||
- category: string
|
||||
@@ -134,6 +135,7 @@ def product_search(request):
|
||||
- order: string (newest/oldest)
|
||||
- price_min: int
|
||||
- price_max: int
|
||||
- geo: {'longitude': 23.23, 'latitude': 23432.23423}
|
||||
|
||||
In the response:
|
||||
- filters
|
||||
@@ -158,11 +160,16 @@ def product_search(request):
|
||||
discount = request.GET.get('discount', None)
|
||||
if discount is not None:
|
||||
if discount == 'true':
|
||||
discount = True
|
||||
discount = True
|
||||
elif discount == 'false':
|
||||
discount = False
|
||||
discount = False
|
||||
else:
|
||||
discount = None
|
||||
discount = None
|
||||
geo = request.GET.get('geo', None)
|
||||
if geo is not None:
|
||||
coordinates = (geo.get('longitude'), geo.get('latitude'))
|
||||
else:
|
||||
coordinates = None
|
||||
category = request.GET.get('category', None)
|
||||
tags = request.GET.get('tags', None)
|
||||
price_min = request.GET.get('price_min', None)
|
||||
@@ -195,7 +202,7 @@ def product_search(request):
|
||||
# split query string into single words
|
||||
chunks = q.split(' ')
|
||||
for chunk in chunks:
|
||||
product_set, min_price, max_price = ranked_product_search(chunk, shipping_cost, discount, category, tags, price_min, price_max)
|
||||
product_set, min_price, max_price, georesult = ranked_product_search(chunk, shipping_cost, discount, category, tags, price_min, price_max, coordinates)
|
||||
# update price values
|
||||
if product_set:
|
||||
if prices['min'] is None or min_price['price__min'] < prices['min']:
|
||||
|
||||
Reference in New Issue
Block a user