fixed product views to only list active products

This commit is contained in:
Sam
2021-03-05 11:49:21 +00:00
parent 375db09bb3
commit 8518bd09ce
4 changed files with 33 additions and 8 deletions

View File

@@ -42,3 +42,28 @@ class ProductFactory(DjangoModelFactory):
class Meta:
model = Product
class ActiveProductFactory(DjangoModelFactory):
company = SubFactory(CompanyFactory)
sku = FuzzyText(prefix='SKU_', length=10)
name = FuzzyText(prefix='NAME_', length=10)
description = FuzzyText(prefix='DECRIPTION', length=100)
url = FuzzyText(prefix='http://WEB-LINK-', suffix='.test', length=10)
price = FuzzyDecimal(low=1.00)
shipping_cost = FuzzyDecimal(low=1.00)
shipping_terms = FuzzyText(prefix='SHIPPING_TERMS', length=100)
source = FuzzyChoice(choices=[x[1] for x in Product.SOURCES])
sourcing_date = FuzzyDateTime(start_dt=timezone.now())
update_date = FuzzyDateTime(start_dt=timezone.now())
discount = FuzzyDecimal(low=0.00, high=100.00)
stock = FuzzyInteger(low=0)
tags = ['test']
category = FuzzyChoice(choices=FAKE_CATEGORIES) # main tag category
attributes = ['programming/python', 'testing']
identifiers = FuzzyText(prefix='IDENTIFIERS_', length=100)
active = True
class Meta:
model = Product

View File

@@ -11,7 +11,7 @@ from rest_framework.test import APITestCase
from rest_framework import status
from companies.factories import CompanyFactory
from products.factories import ProductFactory
from products.factories import ProductFactory, ActiveProductFactory
from products.models import Product
from products.utils import find_related_products_v3
@@ -28,7 +28,7 @@ class ProductViewSetTest(APITestCase):
"""Tests setup
"""
self.endpoint = '/api/v1/products/'
self.factory = ProductFactory
self.factory = ActiveProductFactory
self.model = Product
# create user
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
@@ -425,7 +425,7 @@ class LoadCoopProductsTestCase(APITestCase):
"""
self.endpoint = '/api/v1/load_products/'
self.model = Product
self.factory = ProductFactory
self.factory = ActiveProductFactory
# create admin user
self.admin_email = f"admin_user@mail.com"
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
@@ -504,7 +504,7 @@ class ProductSearchTest(TestCase):
"""
self.endpoint = '/api/v1/search_products/'
self.model = Product
self.factory = ProductFactory
self.factory = ActiveProductFactory
# create admin user
self.admin_email = "admin_user@mail.com"
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
@@ -923,7 +923,7 @@ class MyProductsViewTest(APITestCase):
"""Tests setup
"""
self.endpoint = '/api/v1/my_products/'
self.factory = ProductFactory
self.factory = ActiveProductFactory
self.model = Product
# create user
self.email = f"user@mail.com"
@@ -986,7 +986,7 @@ class FindRelatedProductsTest(APITestCase):
def setUp(self):
"""Tests setup
"""
self.factory = ProductFactory
self.factory = ActiveProductFactory
self.model = Product
# clear table
self.model.objects.all().delete()

View File

@@ -119,7 +119,7 @@ def find_related_products_v6(keyword, shipping_cost=None, discount=None, categor
products_qs = Product.objects.annotate(
rank=SearchRank(vector, query)
).filter(rank__gt=0.05)
).filter(rank__gt=0.05, active=True)
# filter by category
if category is not None:

View File

@@ -37,7 +37,7 @@ logging.basicConfig(
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all().order_by('-created')
queryset = Product.objects.filter(active=True).order_by('-created')
serializer_class = ProductSerializer
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
filter_backends = [DjangoFilterBackend, OrderingFilter]