From a7740dc1a2592b3a61de08372bf7b457726e3b16 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 23 Feb 2021 12:44:54 +0000 Subject: [PATCH] added search filter by tags --- products/tests.py | 22 ++++++++++++++++++++++ products/utils.py | 6 +++++- products/views.py | 6 +++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/products/tests.py b/products/tests.py index fd40f26..92a95a3 100644 --- a/products/tests.py +++ b/products/tests.py @@ -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 diff --git a/products/utils.py b/products/utils.py index 0cfc28c..a294df2 100644 --- a/products/utils.py +++ b/products/utils.py @@ -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 diff --git a/products/views.py b/products/views.py index 96d34bd..d23416b 100644 --- a/products/views.py +++ b/products/views.py @@ -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 ???