fixing changes to product search

This commit is contained in:
Sam
2021-03-17 10:31:39 +00:00
parent 62c6b76e36
commit 6a3bd7acea
3 changed files with 12 additions and 12 deletions

View File

@@ -780,9 +780,9 @@ class ProductSearchTest(TestCase):
def test_anon_user_can_filter_by_category(self):
expected_instances = [
self.factory(tags="lunares/rojos", category='ropa/nueva', description="zapatos verdes", discount=None),
self.factory(tags="lunares/rojos", category="ropa/nueva", discount=0.00),
self.factory(attributes='"zapatos de campo", tono/rojo', category="ropa/nueva", discount=9.00),
self.factory(tags="lunares/rojos", category='nueva', description="zapatos verdes", discount=None),
self.factory(tags="lunares/rojos", category="ropa", discount=0.00),
self.factory(attributes='"zapatos de campo", tono/rojo', category="ropa", discount=9.00),
]
unexpected_instances = [
self.factory(description="chanclas", tags='rojos'),
@@ -790,7 +790,7 @@ class ProductSearchTest(TestCase):
]
q = quote("zapatos rojos")
url = f"{self.endpoint}?q={q}&category=ropa/nueva"
url = f"{self.endpoint}?q={q}&category=ropa&category=nueva"
# send in request
response = self.client.get(url)
# check response

View File

@@ -124,7 +124,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, categories=None, tags=None, price_min=None,price_max=None):
"""
Ranked product search
@@ -135,7 +135,7 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
allow filtering by:
- shipping cost
"""
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name')
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__name') + SearchVector('company__company_name')
query = SearchQuery(keyword)
products_qs = Product.objects.annotate(
@@ -143,8 +143,8 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
).filter(rank__gt=0.05, active=True)
# filter by category
if category is not None:
products_qs = products_qs.filter(category__name__in=category)
if categories is not None:
products_qs = products_qs.filter(category__name__in=categories)
# filter by tags
if tags is not None:

View File

@@ -162,7 +162,7 @@ def product_search(request):
else:
discount = None
# category = request.GET.get('category', None)
category = request.query_params.getlist('category[]') or None
categories = request.query_params.getlist('category') or None
tags = request.GET.get('tags', None)
price_min = request.GET.get('price_min', None)
price_max = request.GET.get('price_max', None)
@@ -185,8 +185,8 @@ def product_search(request):
products_qs = Product.objects.filter(active=True)
if tags:
products_qs = Product.objects.filter(tags=tags)
if category:
products_qs = Product.objects.filter(category__name__in=category)
if categories:
products_qs = Product.objects.filter(category__name__in=categories)
# serialize and list data
serializer = ProductSerializer(products_qs, many=True)
result_list = [dict(i) for i in serializer.data]
@@ -194,7 +194,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 = ranked_product_search(chunk, shipping_cost, discount, categories, tags, price_min, price_max)
# update price values
if product_set:
if prices['min'] is None or min_price['price__min'] < prices['min']: