adde total_results to search response, for better frontend pagination

This commit is contained in:
Sam
2021-02-23 10:58:23 +00:00
parent dc9bfd1219
commit 1c38d81a5f
2 changed files with 6 additions and 2 deletions

View File

@@ -478,6 +478,7 @@ 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()
# check response # check response
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@@ -521,8 +522,9 @@ class ProductSearchTest(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# load response data # load response data
payload = response.json() payload = response.json()
# check for object creation
self.assertEquals(len(payload['products']), limit) self.assertEquals(len(payload['products']), limit)
self.assertEquals(payload['total_results'], len(expected_instances))
class MyProductsViewTest(APITestCase): class MyProductsViewTest(APITestCase):
"""my_products tests """my_products tests

View File

@@ -174,6 +174,8 @@ def product_search(request):
ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True) ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True)
serializer = SearchResultSerializer(ranked_products, many=True) serializer = SearchResultSerializer(ranked_products, many=True)
product_results = [dict(i) for i in serializer.data] product_results = [dict(i) for i in serializer.data]
total_results = len(product_results)
# check for pagination # check for pagination
limit = request.GET.get('limit', None) limit = request.GET.get('limit', None)
offset = request.GET.get('offset', None) offset = request.GET.get('offset', None)
@@ -185,6 +187,6 @@ def product_search(request):
limit = int(limit) limit = int(limit)
product_results = product_results[:limit] product_results = product_results[:limit]
return Response(data={"filters": filters, "products": product_results}) return Response(data={"filters": filters, "total_results": total_results, "products": product_results})
except Exception as e: except Exception as e:
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)