added custom filter with tag support to products
This commit is contained in:
@@ -98,8 +98,15 @@ class ProductViewSetTest(APITestCase):
|
|||||||
|
|
||||||
def test_anon_user_can_filter_tags(self):
|
def test_anon_user_can_filter_tags(self):
|
||||||
# create instances
|
# create instances
|
||||||
self.factory(name='product1', tags="zapatos, verdes")
|
expected_instance = [
|
||||||
self.factory(name='product2', tags="rojos")
|
self.factory(name='product1', tags="zapatos, rojos"),
|
||||||
|
self.factory(name='product2', tags="rojos")
|
||||||
|
]
|
||||||
|
unexpected_instance = [
|
||||||
|
self.factory(name='sadfdsa', tags="zapatos, azules"),
|
||||||
|
self.factory(name='qwerw', tags="xxl")
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
url = f"{self.endpoint}?tags=rojos"
|
url = f"{self.endpoint}?tags=rojos"
|
||||||
|
|
||||||
@@ -107,12 +114,10 @@ class ProductViewSetTest(APITestCase):
|
|||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
payload = response.json()
|
payload = response.json()
|
||||||
|
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
# Assert access is granted
|
# Assert access is granted
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
# Assert number of instnaces in response
|
# Assert number of instnaces in response
|
||||||
self.assertEquals(1, len(payload))
|
self.assertEquals(len(expected_instance), len(payload))
|
||||||
|
|
||||||
|
|
||||||
# authenticated user
|
# authenticated user
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ from rest_framework import viewsets
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser, IsAuthenticated
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser, IsAuthenticated
|
||||||
from rest_framework.decorators import api_view, permission_classes, action
|
from rest_framework.decorators import api_view, permission_classes, action
|
||||||
import django_filters.rest_framework
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ class ProductViewSet(viewsets.ModelViewSet):
|
|||||||
queryset = Product.objects.all()
|
queryset = Product.objects.all()
|
||||||
serializer_class = ProductSerializer
|
serializer_class = ProductSerializer
|
||||||
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
||||||
filter_backends = ProductTagFilter
|
filterset_class = ProductTagFilter
|
||||||
filterset_fields = ['name', 'tags']
|
filterset_fields = ['name', 'tags']
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
|
|||||||
@@ -3,9 +3,15 @@ from products.models import Product
|
|||||||
|
|
||||||
|
|
||||||
class ProductTagFilter(django_filters.FilterSet):
|
class ProductTagFilter(django_filters.FilterSet):
|
||||||
tags = django_filters.CharFilter(field_name='tags.name', lookup_expr='iexact')
|
tags = django_filters.CharFilter(method='tag_filter')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = ['name',]
|
fields = ['name', 'tags']
|
||||||
|
|
||||||
|
def tag_filter(self, queryset, name, value):
|
||||||
|
if name == 'tags':
|
||||||
|
return queryset.filter(tags=value)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user