From 07644dfc52f641859c43ed754dba6c252199838e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 25 Feb 2021 11:51:07 +0000 Subject: [PATCH] producing separate filters for tags and attributes in product search --- products/tests.py | 4 ++-- products/utils.py | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/products/tests.py b/products/tests.py index 5929277..ebdf061 100644 --- a/products/tests.py +++ b/products/tests.py @@ -549,8 +549,8 @@ class ProductSearchTest(TestCase): self.assertTrue(payload['products'][i]['rank'] <= current ) current = payload['products'][i]['rank'] # check for filters - self.assertNotEquals([], payload['filters']['singles']) - self.assertTrue(len(payload['filters']) >= 2 ) + self.assertNotEquals([], payload['filters']['tags']['singles']) + self.assertTrue(len(payload['filters']['tags']) >= 2 ) def test_anon_user_can_paginate_search(self): expected_instances = [ diff --git a/products/utils.py b/products/utils.py index a647c6c..0c6094e 100644 --- a/products/utils.py +++ b/products/utils.py @@ -12,13 +12,17 @@ def extract_search_filters(result_set): Returned object should look something like: { - "singles": [], # non tree tags - "entry_1": [ 'tag1', 'tag2' ], - "entry_2": [ 'tag1', 'tag2' ], + "tags": [], + "attributes": [], } """ filter_dict = { - 'singles': set(), + "tags": { + 'singles': set(), + }, + "attributes": { + 'singles': set(), + } } for item in result_set: try: @@ -26,32 +30,32 @@ def extract_search_filters(result_set): tags = item.tags.all() for tag in tags: if len(tag.name.split('/')) == 1: - filter_dict['singles'].add(tag.name) + filter_dict['tags']['singles'].add(tag.name) else: # set penultimate tag as header chunks = tag.name.split('/') header = chunks[-2] name = chunks[-1] # check if - entry = filter_dict.get(header) + entry = filter_dict['tags'].get(header) if entry is None: - filter_dict[header] = set() - filter_dict[header].add(name) + filter_dict['tags'][header] = set() + filter_dict['tags'][header].add(name) # extract attributes attributes = item.attributes.all() for tag in attributes: if len(tag.name.split('/')) == 1: - filter_dict['singles'].add(tag.name) + filter_dict['attributes']['singles'].add(tag.name) else: # set penultimate tag as header chunks = tag.name.split('/') header = chunks[-2] name = chunks[-1] # check if - entry = filter_dict.get(header) + entry = filter_dict['attributes'].get(header) if entry is None: - filter_dict[header] = set() - filter_dict[header].add(name) + filter_dict['attributes'][header] = set() + filter_dict['attributes'][header].add(name) except Exception as e: logging.error(f'Extacting filters for {item}') return filter_dict