search result pagination working
This commit is contained in:
@@ -145,8 +145,14 @@ def load_coop_products(request):
|
||||
def product_search(request):
|
||||
"""
|
||||
Takes a string of data, return relevant products
|
||||
|
||||
Params:
|
||||
- query_string: used for search [MANDATORY]
|
||||
- limit: max number of returned instances [OPTIONAL]
|
||||
- offset: where to start counting results [OPTIONAL]
|
||||
"""
|
||||
query_string = request.GET.get('query_string', None)
|
||||
|
||||
if query_string is None:
|
||||
return Response({"errors": {"details": "No query string to parse"}})
|
||||
try:
|
||||
@@ -166,8 +172,19 @@ def product_search(request):
|
||||
# order results and respond
|
||||
result_list = list(result_set)
|
||||
ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True)
|
||||
# TODO: slice ranked_products as per pagination
|
||||
serializer = SearchResultSerializer(ranked_products, many=True)
|
||||
return Response(data={"filters": filters, "products": [dict(i) for i in serializer.data]})
|
||||
product_results = [dict(i) for i in serializer.data]
|
||||
# check for pagination
|
||||
limit = request.GET.get('limit', None)
|
||||
offset = request.GET.get('offset', None)
|
||||
if limit is not None and offset is not None:
|
||||
limit = int(limit)
|
||||
offset = int(offset)
|
||||
product_results = product_results[offset:(limit+offset)]
|
||||
elif limit is not None:
|
||||
limit = int(limit)
|
||||
product_results = product_results[:limit]
|
||||
|
||||
return Response(data={"filters": filters, "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