unified the db queries in product search, one per chunk
This commit is contained in:
@@ -462,7 +462,9 @@ class ProductSearchTest(TestCase):
|
||||
def test_anon_user_can_search(self):
|
||||
expected_instances = [
|
||||
self.factory(description="zapatos verdes"),
|
||||
self.factory(tags="colores/rojos"),
|
||||
self.factory(tags="colores/rojos, "),
|
||||
self.factory(description="zapatos rojos"),
|
||||
self.factory(attributes='"lunares rojos", '),
|
||||
]
|
||||
unexpected_instances = [
|
||||
self.factory(description="chanclas"),
|
||||
@@ -476,10 +478,11 @@ class ProductSearchTest(TestCase):
|
||||
url = f"{self.endpoint}?query_string={query_string}"
|
||||
# send in request
|
||||
response = self.client.get(url)
|
||||
payload = response.json()
|
||||
# check response
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# check for object creation
|
||||
self.assertEquals(len(response.data['products']), len(expected_instances))
|
||||
self.assertEquals(len(payload['products']), len(expected_instances))
|
||||
|
||||
|
||||
class MyProductsViewTest(APITestCase):
|
||||
|
||||
@@ -155,31 +155,27 @@ def product_search(request):
|
||||
chunks = query_string.split(' ')
|
||||
|
||||
for chunk in chunks:
|
||||
# search inside name and description
|
||||
products = Product.objects.filter(Q(name__icontains=chunk) | Q(description__icontains=chunk))
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
|
||||
# search in tags
|
||||
tags = Product.tags.tag_model.objects.filter(name__icontains=chunk)
|
||||
products = Product.objects.filter(tags__in=tags)
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
# search in category
|
||||
products = Product.objects.filter(category=chunk)
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
categories = Product.category.tag_model.objects.filter(name__icontains=chunk)
|
||||
# search in attributes
|
||||
attributes = Product.attributes.tag_model.objects.filter(name__icontains=chunk)
|
||||
products = Product.objects.filter(attributes__in=attributes)
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
# unified tag search
|
||||
products_qs = Product.objects.filter(
|
||||
Q(name__icontains=chunk)|
|
||||
Q(description__icontains=chunk)|
|
||||
Q(tags__in=tags)|
|
||||
Q(category__in=categories)|
|
||||
Q(attributes__in=attributes)
|
||||
)
|
||||
for instance in products_qs:
|
||||
result_set.add(instance)
|
||||
|
||||
# extract filters from result_set
|
||||
filters = extract_search_filters(result_set)
|
||||
# filters = {}
|
||||
# serialize and respond
|
||||
product_serializer = ProductSerializer(result_set, many=True)
|
||||
return Response(data={"filters": filters, "products": product_serializer.data})
|
||||
except Exception as e:
|
||||
import ipdb; ipdb.set_trace()
|
||||
return Response({"errors": {"details": str(type(e))}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
Reference in New Issue
Block a user