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):
|
||||
|
||||
Reference in New Issue
Block a user