new way of handling category searches

This commit is contained in:
Sam
2021-03-19 10:53:49 +00:00
parent 2673aa51aa
commit 23109cfb39
3 changed files with 19 additions and 5 deletions

View File

@@ -793,7 +793,7 @@ class ProductSearchTest(TestCase):
]
q = quote("zapatos rojos")
url = f"{self.endpoint}?q={q}&category=ropa&category=nueva"
url = f"{self.endpoint}?q={q}&category[]=ropa&category[]=nueva"
# send in request
response = self.client.get(url)
# check response

View File

@@ -10,7 +10,7 @@ from django.utils import timezone
import requests
from products.models import Product
from products.models import Product, CategoryTag
from companies.models import Company
from history.models import HistorySync
from PIL import Image
@@ -144,7 +144,15 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, categories
# filter by category
if categories is not None:
products_qs = products_qs.filter(category__name__in=categories)
# products_qs = products_qs.filter(category__name__in=categories)
descendants = []
for entry in categories:
cat = CategoryTag.objects.filter(label__iexact=entry).first()
# append category tag, and children
descendants.append(cat)
descendants.extend(cat.children.all())
products_qs = products_qs.filter(category__in=descendants)
# filter by tags
if tags is not None:

View File

@@ -163,7 +163,7 @@ def product_search(request):
else:
discount = None
# category = request.GET.get('category', None)
categories = request.query_params.getlist('category') or None
categories = request.query_params.getlist('category[]') or None
tags = request.GET.get('tags', None)
price_min = request.GET.get('price_min', None)
price_max = request.GET.get('price_max', None)
@@ -183,8 +183,14 @@ def product_search(request):
products_qs = Product.objects.filter(active=True)
# filter by category
if categories is not None:
products_qs = products_qs.filter(category__name__in=categories)
descendants = []
for entry in categories:
cat = CategoryTag.objects.filter(label__iexact=entry).first()
# append category tag, and children
descendants.append(cat)
descendants.extend(cat.children.all())
products_qs = products_qs.filter(category__in=descendants)
# filter by tags
if tags is not None:
products_qs = products_qs.filter(tags__name__icontains=tags)