From 4f3996369143f272253f1e4c55e92ee3cf94412f Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 23 Feb 2021 13:40:49 +0000 Subject: [PATCH] enabled per-company products filtering --- products/tests.py | 15 +++++++++------ products/views.py | 14 +++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/products/tests.py b/products/tests.py index f721c94..84d4385 100644 --- a/products/tests.py +++ b/products/tests.py @@ -83,8 +83,8 @@ class ProductViewSetTest(APITestCase): def test_anon_user_can_filter_name(self): # create instances - self.factory(name='product1', tags="zapatos, verdes") - self.factory(name='product2', tags="rojos") + expected = self.factory(name='product1', tags="zapatos, verdes") + unexpected = self.factory(name='product2', tags="rojos") url = f"{self.endpoint}?name=product1" @@ -96,12 +96,14 @@ class ProductViewSetTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert number of instnaces in response self.assertEquals(1, len(payload)) + self.assertEquals(payload[0]['tags'], expected.tags) def test_anon_user_can_filter_tags(self): # create instances expected_instance = [ self.factory(name='product1', tags="zapatos, rojos"), - self.factory(name='product2', tags="rojos") + self.factory(name='product2', tags="rojos"), + self.factory(name='product2', tags="colores, rojos") ] unexpected_instance = [ self.factory(name='sadfdsa', tags="zapatos, azules"), @@ -123,7 +125,8 @@ class ProductViewSetTest(APITestCase): # create instances expected_instance = [ self.factory(attributes='xxl', tags="zapatos, rojos"), - self.factory(attributes='blue, xxl', tags="rojos") + self.factory(attributes='blue, xxl', tags="rojos"), + self.factory(attributes='sizes xxl', tags="rojos") ] unexpected_instance = [ self.factory(name='sadfdsa', tags="zapatos, azules"), @@ -145,7 +148,8 @@ class ProductViewSetTest(APITestCase): # create instances expected_instance = [ self.factory(category='ropa', tags="zapatos, rojos"), - self.factory(category='ropa', tags="rojos") + self.factory(category='ropa', tags="rojos"), + self.factory(category='ropa', tags="colores/rojos"), ] unexpected_instance = [ self.factory(category='roperos', tags="zapatos, azules"), @@ -181,7 +185,6 @@ class ProductViewSetTest(APITestCase): # Request list response = self.client.get(url) payload = response.json() - import ipdb; ipdb.set_trace() # Assert access is granted self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/products/views.py b/products/views.py index fd69c1b..a695710 100644 --- a/products/views.py +++ b/products/views.py @@ -15,7 +15,9 @@ from rest_framework import viewsets from rest_framework.response import Response from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser, IsAuthenticated from rest_framework.decorators import api_view, permission_classes, action +from rest_framework.filters import OrderingFilter +from django_filters.rest_framework import DjangoFilterBackend import requests from products.models import Product @@ -38,11 +40,21 @@ logging.basicConfig( class ProductViewSet(viewsets.ModelViewSet): - queryset = Product.objects.all() + # queryset = Product.objects.all() serializer_class = ProductSerializer permission_classes = [IsAuthenticatedOrReadOnly, IsCreator] + filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_class = ProductTagFilter filterset_fields = ['name', 'tags', 'category', 'attributes', 'company', 'created'] + # TODO: figure out why cant use filterset for company filter + def get_queryset(self): + try: + company_id = self.request.GET.get('company') + company_id = int(company_id) + queryset = Product.objects.filter(company__id=company_id) + except: + queryset = Product.objects.all() + return queryset def perform_create(self, serializer): serializer.save(creator=self.request.user)