added search filter by tags

This commit is contained in:
Sam
2021-02-23 12:44:54 +00:00
parent 28d61c75c0
commit a7740dc1a2
3 changed files with 32 additions and 2 deletions

View File

@@ -658,6 +658,28 @@ class ProductSearchTest(TestCase):
payload = response.json() payload = response.json()
self.assertEquals(len(payload['products']), len(expected_instances)) 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): class MyProductsViewTest(APITestCase):
"""my_products tests """my_products tests

View File

@@ -125,7 +125,7 @@ def find_related_products_v3(keyword):
return set(products_qs) 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 Ranked product search
@@ -147,6 +147,10 @@ def find_related_products_v6(keyword, shipping_cost=None, discount=None, categor
if category is not None: if category is not None:
products_qs = products_qs.filter(category=category) 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 # filter for shipping cost
if shipping_cost is True: if shipping_cost is True:
# only instances with shipping costs # only instances with shipping costs

View File

@@ -151,6 +151,9 @@ def product_search(request):
- limit: max number of returned instances [OPTIONAL] - limit: max number of returned instances [OPTIONAL]
- offset: where to start counting results [OPTIONAL] - offset: where to start counting results [OPTIONAL]
- shipping_cost: true/false - shipping_cost: true/false
- discount: true/false
- category: string
- tags: string
""" """
# capture query params # capture query params
query_string = request.GET.get('query_string', None) query_string = request.GET.get('query_string', None)
@@ -173,6 +176,7 @@ def product_search(request):
else: else:
discount = None discount = None
category = request.GET.get('category', None) category = request.GET.get('category', None)
tags = request.GET.get('tags', None)
if query_string is None: if query_string is None:
return Response({"errors": {"details": "No query string to parse"}}) return Response({"errors": {"details": "No query string to parse"}})
@@ -189,7 +193,7 @@ def product_search(request):
# split query string into single words # split query string into single words
chunks = query_string.split(' ') chunks = query_string.split(' ')
for chunk in chunks: 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 # add to result set
result_set.update(product_set) result_set.update(product_set)
# TODO: add search for entire phrase ??? # TODO: add search for entire phrase ???