added filter by shipping cost to product search
This commit is contained in:
@@ -24,7 +24,7 @@ from companies.models import Company
|
||||
from history.models import HistorySync
|
||||
|
||||
from back_latienda.permissions import IsCreator
|
||||
from .utils import extract_search_filters, find_related_products_v3
|
||||
from .utils import extract_search_filters, find_related_products_v3, find_related_products_v6
|
||||
from utils.tag_serializers import TaggitSerializer
|
||||
from utils.tag_filters import ProductTagFilter
|
||||
|
||||
@@ -150,35 +150,46 @@ def product_search(request):
|
||||
- query_string: used for search [MANDATORY]
|
||||
- limit: max number of returned instances [OPTIONAL]
|
||||
- offset: where to start counting results [OPTIONAL]
|
||||
- shipping_cost: true/false
|
||||
"""
|
||||
query_string = request.GET.get('query_string', None)
|
||||
|
||||
limit = request.GET.get('limit', None)
|
||||
offset = request.GET.get('offset', None)
|
||||
shipping_cost = request.GET.get('shipping_cost', None)
|
||||
if shipping_cost is not None:
|
||||
if shipping_cost == 'true':
|
||||
shipping_cost = True
|
||||
elif shipping_cost == 'false':
|
||||
shipping_cost = False
|
||||
else:
|
||||
shipping_cost = None
|
||||
if query_string is None:
|
||||
return Response({"errors": {"details": "No query string to parse"}})
|
||||
elif query_string is '':
|
||||
# return everything
|
||||
pass
|
||||
try:
|
||||
# save results
|
||||
# we collect our results here
|
||||
result_set = set()
|
||||
|
||||
# split query string into single words
|
||||
chunks = query_string.split(' ')
|
||||
|
||||
for chunk in chunks:
|
||||
product_set = find_related_products_v3(chunk)
|
||||
product_set = find_related_products_v6(chunk, shipping_cost)
|
||||
# add to result set
|
||||
result_set.update(product_set)
|
||||
# TODO: add search for entire phrase
|
||||
# TODO: add search for entire phrase ???
|
||||
|
||||
# extract filters from result_set
|
||||
filters = extract_search_filters(result_set)
|
||||
# order results and respond
|
||||
# order results by RANK
|
||||
result_list = list(result_set)
|
||||
ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True)
|
||||
serializer = SearchResultSerializer(ranked_products, many=True)
|
||||
product_results = [dict(i) for i in serializer.data]
|
||||
total_results = len(product_results)
|
||||
|
||||
# check for pagination
|
||||
limit = request.GET.get('limit', None)
|
||||
offset = request.GET.get('offset', None)
|
||||
# RESULTS PAGINATION
|
||||
if limit is not None and offset is not None:
|
||||
limit = int(limit)
|
||||
offset = int(offset)
|
||||
@@ -187,6 +198,6 @@ def product_search(request):
|
||||
limit = int(limit)
|
||||
product_results = product_results[:limit]
|
||||
|
||||
return Response(data={"filters": filters, "total_results": total_results, "products": product_results})
|
||||
return Response(data={"filters": filters, "count": total_results, "products": product_results})
|
||||
except Exception as e:
|
||||
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
Reference in New Issue
Block a user