implemented basic filter creation during product search
This commit is contained in:
@@ -2,8 +2,7 @@ from rest_framework import serializers
|
|||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
|
|
||||||
from utils.tag_serializers import TagListSerializerField, TaggitSerializer, SingleTagSerializerField
|
from utils.tag_serializers import TagListSerializerField, TaggitSerializer, SingleTagSerializerField, SingleTag, TagList
|
||||||
|
|
||||||
|
|
||||||
class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
|
||||||
@@ -14,3 +13,11 @@ class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
exclude = ['created', 'updated', 'creator']
|
exclude = ['created', 'updated', 'creator']
|
||||||
|
|
||||||
|
|
||||||
|
class TagFilterSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
tags = TagListSerializerField(required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = TagList
|
||||||
|
fields = '__all__'
|
||||||
|
|||||||
@@ -390,11 +390,11 @@ class ProductSearchTest(TestCase):
|
|||||||
url = f"{self.endpoint}?query_string={query_string}"
|
url = f"{self.endpoint}?query_string={query_string}"
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
# import ipdb; ipdb.set_trace()
|
import ipdb; ipdb.set_trace()
|
||||||
# check re sponse
|
# check re sponse
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
# check for object creation
|
# check for object creation
|
||||||
self.assertEquals(len(response.data), len(expected_instances))
|
self.assertEquals(len(response.data['products']), len(expected_instances))
|
||||||
|
|
||||||
|
|
||||||
class MyProductsViewTest(APITestCase):
|
class MyProductsViewTest(APITestCase):
|
||||||
|
|||||||
9
products/utils.py
Normal file
9
products/utils.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
def extract_search_filters(result_set):
|
||||||
|
filters = set()
|
||||||
|
|
||||||
|
for item in result_set:
|
||||||
|
tags = item.tags.all()
|
||||||
|
for tag in tags:
|
||||||
|
filters.add(tag.name)
|
||||||
|
return list(filters)
|
||||||
@@ -17,12 +17,13 @@ from rest_framework.decorators import api_view, permission_classes
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
from products.serializers import ProductSerializer
|
from products.serializers import ProductSerializer, TagFilterSerializer
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
from history.models import HistorySync
|
from history.models import HistorySync
|
||||||
|
|
||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
|
from .utils import extract_search_filters
|
||||||
|
from utils.tag_serializers import TaggitSerializer
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename='logs/product-load.log',
|
filename='logs/product-load.log',
|
||||||
@@ -142,7 +143,6 @@ def product_search(request):
|
|||||||
result_set = set()
|
result_set = set()
|
||||||
# split query string into single words
|
# split query string into single words
|
||||||
chunks = query_string.split(' ')
|
chunks = query_string.split(' ')
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
for chunk in chunks:
|
for chunk in chunks:
|
||||||
# search inside name and description
|
# search inside name and description
|
||||||
@@ -162,8 +162,10 @@ def product_search(request):
|
|||||||
products = Product.objects.filter(attributes=chunk)
|
products = Product.objects.filter(attributes=chunk)
|
||||||
for item in products:
|
for item in products:
|
||||||
result_set.add(item)
|
result_set.add(item)
|
||||||
|
# extract filters from result_set
|
||||||
|
filters = extract_search_filters(result_set)
|
||||||
# serialize and respond
|
# serialize and respond
|
||||||
product_serializer = ProductSerializer(result_set, many=True)
|
product_serializer = ProductSerializer(result_set, many=True)
|
||||||
return Response(data=product_serializer.data)
|
return Response(data={"filters": filters, "products": product_serializer.data})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"errors": {"details": str(type(e))}})
|
return Response({"errors": {"details": str(type(e))}})
|
||||||
|
|||||||
Reference in New Issue
Block a user