added search filter by tags
This commit is contained in:
@@ -658,6 +658,28 @@ class ProductSearchTest(TestCase):
|
||||
payload = response.json()
|
||||
self.assertEquals(len(payload['products']), len(expected_instances))
|
||||
|
||||
def test_anon_user_can_filter_by_tags(self):
|
||||
expected_instances = [
|
||||
self.factory(tags="lunares/rojos, deporte", description="zapatos verdes", discount=None),
|
||||
self.factory(tags="lunares/rojos, deporte", discount=0.00),
|
||||
self.factory(tags="deporte", attributes='"zapatos de campo", tono/oscuro', category="ropa", discount=9.00),
|
||||
]
|
||||
unexpected_instances = [
|
||||
self.factory(description="chanclas", tags='rojos'),
|
||||
self.factory(tags="zapatos/azules", category="deporte", description='rojos', discount=12.00),
|
||||
]
|
||||
|
||||
query_string = quote("zapatos rojos")
|
||||
# discount=true
|
||||
url = f"{self.endpoint}?query_string={query_string}&tags=deporte"
|
||||
# send in request
|
||||
response = self.client.get(url)
|
||||
# check response
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# load response data
|
||||
payload = response.json()
|
||||
self.assertEquals(len(payload['products']), len(expected_instances))
|
||||
|
||||
|
||||
class MyProductsViewTest(APITestCase):
|
||||
"""my_products tests
|
||||
|
||||
@@ -125,7 +125,7 @@ def find_related_products_v3(keyword):
|
||||
return set(products_qs)
|
||||
|
||||
|
||||
def find_related_products_v6(keyword, shipping_cost=None, discount=None, category=None):
|
||||
def find_related_products_v6(keyword, shipping_cost=None, discount=None, category=None, tags=None):
|
||||
"""
|
||||
Ranked product search
|
||||
|
||||
@@ -147,6 +147,10 @@ def find_related_products_v6(keyword, shipping_cost=None, discount=None, categor
|
||||
if category is not None:
|
||||
products_qs = products_qs.filter(category=category)
|
||||
|
||||
# filter by tags
|
||||
if tags is not None:
|
||||
products_qs = products_qs.filter(tags=tags)
|
||||
|
||||
# filter for shipping cost
|
||||
if shipping_cost is True:
|
||||
# only instances with shipping costs
|
||||
|
||||
@@ -151,6 +151,9 @@ def product_search(request):
|
||||
- limit: max number of returned instances [OPTIONAL]
|
||||
- offset: where to start counting results [OPTIONAL]
|
||||
- shipping_cost: true/false
|
||||
- discount: true/false
|
||||
- category: string
|
||||
- tags: string
|
||||
"""
|
||||
# capture query params
|
||||
query_string = request.GET.get('query_string', None)
|
||||
@@ -173,6 +176,7 @@ def product_search(request):
|
||||
else:
|
||||
discount = None
|
||||
category = request.GET.get('category', None)
|
||||
tags = request.GET.get('tags', None)
|
||||
|
||||
if query_string is None:
|
||||
return Response({"errors": {"details": "No query string to parse"}})
|
||||
@@ -189,7 +193,7 @@ def product_search(request):
|
||||
# split query string into single words
|
||||
chunks = query_string.split(' ')
|
||||
for chunk in chunks:
|
||||
product_set = find_related_products_v6(chunk, shipping_cost, discount, category)
|
||||
product_set = find_related_products_v6(chunk, shipping_cost, discount, category, tags)
|
||||
# add to result set
|
||||
result_set.update(product_set)
|
||||
# TODO: add search for entire phrase ???
|
||||
|
||||
Reference in New Issue
Block a user