filtering product searches with empty string working now
This commit is contained in:
@@ -757,19 +757,22 @@ class ProductSearchTest(TestCase):
|
||||
payload = response.json()
|
||||
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 = [
|
||||
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="lunares/azules", description="zapatos rojos", discount=12.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(description="chanclas"),
|
||||
self.factory(tags="azules"),
|
||||
]
|
||||
|
||||
# discount=true
|
||||
url = f"{self.endpoint}?q="
|
||||
url = f"{self.endpoint}?tags=rojos"
|
||||
# send in request
|
||||
response = self.client.get(url)
|
||||
# check response
|
||||
|
||||
@@ -9,6 +9,7 @@ from django.core.validators import validate_email
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.template.loader import render_to_string
|
||||
from django.core.mail import EmailMessage
|
||||
from django.db.models import Max, Min
|
||||
|
||||
# Create your views here.
|
||||
from rest_framework import status
|
||||
@@ -168,9 +169,6 @@ def product_search(request):
|
||||
price_max = request.GET.get('price_max', None)
|
||||
order = request.GET.get('order', '')
|
||||
|
||||
if q is None:
|
||||
return Response({"errors": {"details": "No query string to parse"}})
|
||||
|
||||
try:
|
||||
# we collect our results here
|
||||
result_set = set()
|
||||
@@ -180,13 +178,48 @@ def product_search(request):
|
||||
'max': None,
|
||||
}
|
||||
|
||||
if q == '':
|
||||
if not q:
|
||||
# filter entire queryset
|
||||
products_qs = Product.objects.filter(active=True)
|
||||
if tags:
|
||||
products_qs = Product.objects.filter(tags=tags)
|
||||
if categories:
|
||||
products_qs = Product.objects.filter(category__name__in=categories)
|
||||
# filter by category
|
||||
if categories is not None:
|
||||
products_qs = products_qs.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
|
||||
serializer = ProductSerializer(products_qs, many=True)
|
||||
result_list = [dict(i) for i in serializer.data]
|
||||
@@ -216,7 +249,7 @@ def product_search(request):
|
||||
elif order == 'oldest':
|
||||
# order results by created
|
||||
result_list = sorted(result_list, key= lambda x:x['created'], reverse=False)
|
||||
elif q != '':
|
||||
elif q:
|
||||
# order results by RANK
|
||||
result_list = sorted(result_list, key= lambda x:x['rank'], reverse=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user