query params filter working with tags in Product
This commit is contained in:
@@ -61,7 +61,6 @@ class Command(BaseCommand):
|
||||
logging.info(f"Country instance created for: {name}")
|
||||
except Exception as e:
|
||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||
# import ipdb; ipdb.set_trace()
|
||||
|
||||
# region instances
|
||||
logging.info("loading region instances")
|
||||
@@ -83,7 +82,6 @@ class Command(BaseCommand):
|
||||
logging.info(f"Region instance created for: {name}")
|
||||
except Exception as e:
|
||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||
# import ipdb; ipdb.set_trace()
|
||||
|
||||
# province instances
|
||||
logging.info("loading province instances")
|
||||
@@ -106,7 +104,6 @@ class Command(BaseCommand):
|
||||
logging.info(f"Province instance created for: {name}")
|
||||
except Exception as e:
|
||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||
import ipdb; ipdb.set_trace()
|
||||
|
||||
# city instances
|
||||
logging.info("loading city instances")
|
||||
@@ -129,7 +126,6 @@ class Command(BaseCommand):
|
||||
logging.debug(f"City instance created for: {name}")
|
||||
except Exception as e:
|
||||
logging.error(f"[{type(e)}] {str(e)}")
|
||||
# import ipdb; ipdb.set_trace()
|
||||
|
||||
logging.info(f"Country instances created: {country_counter}")
|
||||
logging.info(f"Region instances created: {region_counter}")
|
||||
|
||||
@@ -106,8 +106,7 @@ class ProductViewSetTest(APITestCase):
|
||||
self.factory(name='sadfdsa', tags="zapatos, azules"),
|
||||
self.factory(name='qwerw', tags="xxl")
|
||||
]
|
||||
|
||||
|
||||
# prepare url
|
||||
url = f"{self.endpoint}?tags=rojos"
|
||||
|
||||
# Request list
|
||||
@@ -119,6 +118,49 @@ class ProductViewSetTest(APITestCase):
|
||||
# Assert number of instnaces in response
|
||||
self.assertEquals(len(expected_instance), len(payload))
|
||||
|
||||
def test_anon_user_can_filter_attributes(self):
|
||||
# create instances
|
||||
expected_instance = [
|
||||
self.factory(attributes='xxl', tags="zapatos, rojos"),
|
||||
self.factory(attributes='blue, xxl', tags="rojos")
|
||||
]
|
||||
unexpected_instance = [
|
||||
self.factory(name='sadfdsa', tags="zapatos, azules"),
|
||||
self.factory(name='qwerw', tags="xxl")
|
||||
]
|
||||
# prepare url
|
||||
url = f"{self.endpoint}?attributes=xxl"
|
||||
|
||||
# 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(len(expected_instance), len(payload))
|
||||
|
||||
def test_anon_user_can_filter_category(self):
|
||||
# create instances
|
||||
expected_instance = [
|
||||
self.factory(category='ropa', tags="zapatos, rojos"),
|
||||
self.factory(category='ropa', tags="rojos")
|
||||
]
|
||||
unexpected_instance = [
|
||||
self.factory(category='roperos', tags="zapatos, azules"),
|
||||
self.factory(category='enropados', tags="xxl")
|
||||
]
|
||||
# prepare url
|
||||
url = f"{self.endpoint}?category=ropa"
|
||||
|
||||
# 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(len(expected_instance), len(payload))
|
||||
|
||||
# authenticated user
|
||||
def test_auth_user_can_list_instances(self):
|
||||
|
||||
@@ -41,7 +41,7 @@ class ProductViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = ProductSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
||||
filterset_class = ProductTagFilter
|
||||
filterset_fields = ['name', 'tags']
|
||||
filterset_fields = ['name', 'tags', 'category', 'attributes']
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(creator=self.request.user)
|
||||
|
||||
@@ -3,15 +3,16 @@ from products.models import Product
|
||||
|
||||
|
||||
class ProductTagFilter(django_filters.FilterSet):
|
||||
|
||||
tags = django_filters.CharFilter(method='tag_filter')
|
||||
attributes = django_filters.CharFilter(method='tag_filter')
|
||||
category = django_filters.CharFilter(method='tag_filter')
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ['name', 'tags']
|
||||
fields = ['name', 'tags', 'category', 'attributes']
|
||||
|
||||
def tag_filter(self, queryset, name, value):
|
||||
if name == 'tags':
|
||||
return queryset.filter(tags=value)
|
||||
return []
|
||||
|
||||
|
||||
return queryset.filter(**{
|
||||
name: value,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user