filtering product searches with empty string working now

This commit is contained in:
Sam
2021-03-17 11:30:11 +00:00
parent 696e2f49d1
commit d2ee4273b9
2 changed files with 48 additions and 12 deletions

View File

@@ -757,19 +757,22 @@ 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_search_empty_string(self): def test_anon_user_can_search_empty_string_with_tags(self):
expected_instances = [ expected_instances = [
self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes", discount=None), self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes", discount=None),
self.factory(tags="colores/rojos, tono/brillante", discount=100.00), self.factory(tags="colores/rojos, tono/brillante", discount=100.00),
self.factory(tags="lunares/azules", description="zapatos rojos", discount=12.00),
self.factory(tags="lunares/rojos", description="zapatos", discount=0.00), self.factory(tags="lunares/rojos", description="zapatos", discount=0.00),
]
unexpected_instances = [
self.factory(tags="lunares/azules", description="zapatos rojos", discount=12.00),
self.factory(attributes='"zapatos de campo", tono/oscuro', discount=9.00), self.factory(attributes='"zapatos de campo", tono/oscuro', discount=9.00),
self.factory(description="chanclas"), self.factory(description="chanclas"),
self.factory(tags="azules"), self.factory(tags="azules"),
] ]
# discount=true # discount=true
url = f"{self.endpoint}?q=" url = f"{self.endpoint}?tags=rojos"
# send in request # send in request
response = self.client.get(url) response = self.client.get(url)
# check response # check response

View File

@@ -9,6 +9,7 @@ from django.core.validators import validate_email
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from django.db.models import Max, Min
# Create your views here. # Create your views here.
from rest_framework import status from rest_framework import status
@@ -168,9 +169,6 @@ def product_search(request):
price_max = request.GET.get('price_max', None) price_max = request.GET.get('price_max', None)
order = request.GET.get('order', '') order = request.GET.get('order', '')
if q is None:
return Response({"errors": {"details": "No query string to parse"}})
try: try:
# we collect our results here # we collect our results here
result_set = set() result_set = set()
@@ -180,13 +178,48 @@ def product_search(request):
'max': None, 'max': None,
} }
if q == '': if not q:
# filter entire queryset # filter entire queryset
products_qs = Product.objects.filter(active=True) products_qs = Product.objects.filter(active=True)
if tags: # filter by category
products_qs = Product.objects.filter(tags=tags) if categories is not None:
if categories: products_qs = products_qs.filter(category__name__in=categories)
products_qs = Product.objects.filter(category__name__in=categories)
# filter by tags
if tags is not None:
products_qs = products_qs.filter(tags__name__icontains=tags)
# filter by shipping cost
if shipping_cost is True:
# only instances with shipping costs
products_qs = products_qs.filter(
Q(shipping_cost__isnull=False)&
Q(shipping_cost__gte=1)
)
elif shipping_cost is False:
# only intances without shpping costs
products_qs = products_qs.filter(Q(shipping_cost=None)|Q(shipping_cost=0.00))
# filter by discount
if discount is True:
# only instances with shipping costs
products_qs = products_qs.filter(
Q(discount__isnull=False)&
Q(discount__gte=1)
)
elif discount is False:
# only intances without shpping costs
products_qs = products_qs.filter(Q(discount=None)|Q(discount=0.00))
# filter by price
if price_min is not None:
products_qs = products_qs.filter(price__gte=price_min)
if price_max is not None:
products_qs = products_qs.filter(price__lte=price_max)
# get min_price and max_price
prices['min'] = products_qs.aggregate(Min('price'))
prices['max'] = products_qs.aggregate(Max('price'))
# serialize and list data # serialize and list data
serializer = ProductSerializer(products_qs, many=True) serializer = ProductSerializer(products_qs, many=True)
result_list = [dict(i) for i in serializer.data] result_list = [dict(i) for i in serializer.data]
@@ -216,7 +249,7 @@ def product_search(request):
elif order == 'oldest': elif order == 'oldest':
# order results by created # order results by created
result_list = sorted(result_list, key= lambda x:x['created'], reverse=False) result_list = sorted(result_list, key= lambda x:x['created'], reverse=False)
elif q != '': elif q:
# order results by RANK # order results by RANK
result_list = sorted(result_list, key= lambda x:x['rank'], reverse=True) result_list = sorted(result_list, key= lambda x:x['rank'], reverse=True)