testing tag filtering
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from tagulous.serializers.json import Serializer
|
||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,41 @@ class ProductViewSetTest(APITestCase):
|
|||||||
# Assert access is forbidden
|
# Assert access is forbidden
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def test_anon_user_can_filter_name(self):
|
||||||
|
# create instances
|
||||||
|
self.factory(name='product1', tags="zapatos, verdes")
|
||||||
|
self.factory(name='product2', tags="rojos")
|
||||||
|
|
||||||
|
url = f"{self.endpoint}?name=product1"
|
||||||
|
|
||||||
|
# Request list
|
||||||
|
response = self.client.get(url)
|
||||||
|
payload = response.json()
|
||||||
|
|
||||||
|
# Assert access is granted
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
# Assert number of instnaces in response
|
||||||
|
self.assertEquals(1, len(payload))
|
||||||
|
|
||||||
|
def test_anon_user_can_filter_tags(self):
|
||||||
|
# create instances
|
||||||
|
self.factory(name='product1', tags="zapatos, verdes")
|
||||||
|
self.factory(name='product2', tags="rojos")
|
||||||
|
|
||||||
|
url = f"{self.endpoint}?tags=rojos"
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
# Assert number of instnaces in response
|
||||||
|
self.assertEquals(1, len(payload))
|
||||||
|
|
||||||
|
|
||||||
# authenticated user
|
# authenticated user
|
||||||
def test_auth_user_can_list_instances(self):
|
def test_auth_user_can_list_instances(self):
|
||||||
"""Regular logged-in user can list instance
|
"""Regular logged-in user can list instance
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ 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
|
||||||
|
|
||||||
@@ -25,6 +26,8 @@ from history.models import HistorySync
|
|||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
from .utils import extract_search_filters
|
from .utils import extract_search_filters
|
||||||
from utils.tag_serializers import TaggitSerializer
|
from utils.tag_serializers import TaggitSerializer
|
||||||
|
from utils.tag_filters import ProductTagFilter
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename='logs/product-load.log',
|
filename='logs/product-load.log',
|
||||||
@@ -38,6 +41,8 @@ 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_fields = ['name', 'tags']
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
serializer.save(creator=self.request.user)
|
serializer.save(creator=self.request.user)
|
||||||
|
|||||||
11
utils/tag_filters.py
Normal file
11
utils/tag_filters.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import django_filters
|
||||||
|
from products.models import Product
|
||||||
|
|
||||||
|
|
||||||
|
class ProductTagFilter(django_filters.FilterSet):
|
||||||
|
tags = django_filters.CharFilter(field_name='tags.name', lookup_expr='iexact')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Product
|
||||||
|
fields = ['name',]
|
||||||
|
|
||||||
Reference in New Issue
Block a user