From d2ee4273b95432d17fa70ff17cedc6ceea1cac37 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 17 Mar 2021 11:30:11 +0000 Subject: [PATCH] filtering product searches with empty string working now --- products/tests.py | 9 ++++++--- products/views.py | 51 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/products/tests.py b/products/tests.py index 7d1c462..e9944bf 100644 --- a/products/tests.py +++ b/products/tests.py @@ -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 diff --git a/products/views.py b/products/views.py index 75776be..612be20 100644 --- a/products/views.py +++ b/products/views.py @@ -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)