implemented pagination for my_products and my_company

This commit is contained in:
Sam
2021-03-04 11:50:26 +00:00
parent 8068921bb7
commit c99b81b751
4 changed files with 106 additions and 8 deletions

View File

@@ -34,6 +34,9 @@ class ProductViewSetTest(APITestCase):
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
self.user = CustomUserFactory(email="test@mail.com", password=self.password, is_active=True)
def tearDown(self):
self.model.objects.all().delete()
# anon user
def test_anon_user_cannot_create_instance(self):
"""Not logged-in user cannot create new instance
@@ -192,6 +195,26 @@ class ProductViewSetTest(APITestCase):
self.assertEquals(len(expected_instance), len(payload))
# authenticated user
def test_auth_user_can_paginate_instances(self):
"""authenticated user can paginate instances
"""
# Authenticate
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# create instances
instances = [self.factory(creator=self.user) for n in range(12)]
# Request list
url = f"{self.endpoint}?limit=5&offset=10"
response = self.client.get(url)
# Assert access is allowed
self.assertEqual(response.status_code, status.HTTP_200_OK)
# assert only 2 instances in response
payload = response.json()
self.assertEquals(2, len(payload['results']))
def test_auth_user_can_list_instances(self):
"""Regular logged-in user can list instance
"""
@@ -926,6 +949,26 @@ class MyProductsViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEquals(len(user_instances), len(payload))
def test_auth_user_can_paginate_instances(self):
"""authenticated user can paginate instances
"""
# Authenticate
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# create instances
instances = [self.factory(creator=self.user) for n in range(12)]
# Request list
url = f"{self.endpoint}?limit=5&offset=10"
response = self.client.get(url)
# Assert access is allowed
self.assertEqual(response.status_code, status.HTTP_200_OK)
# assert only 2 instances in response
payload = response.json()
self.assertEquals(2, len(payload))
def test_anon_user_cannot_access(self):
# send in request
response = self.client.get(self.endpoint)

View File

@@ -54,9 +54,20 @@ class ProductViewSet(viewsets.ModelViewSet):
@api_view(['GET',])
@permission_classes([IsAuthenticated,])
def my_products(request):
qs = Product.objects.filter(creator=request.user)
product_serializer = ProductSerializer(qs, many=True)
return Response(data=product_serializer.data)
limit = request.GET.get('limit')
offset = request.GET.get('offset')
qs = Product.objects.filter(creator=request.user)
product_serializer = ProductSerializer(qs, many=True)
data = product_serializer.data
# RESULTS PAGINATION
if limit is not None and offset is not None:
limit = int(limit)
offset = int(offset)
data = data[offset:(limit+offset)]
elif limit is not None:
limit = int(limit)
data = data[:limit]
return Response(data=data)
@api_view(['POST',])