diff --git a/products/factories.py b/products/factories.py index c656f00..55c3372 100644 --- a/products/factories.py +++ b/products/factories.py @@ -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 diff --git a/products/tests.py b/products/tests.py index df78b6d..e654ea3 100644 --- a/products/tests.py +++ b/products/tests.py @@ -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() diff --git a/products/utils.py b/products/utils.py index b335905..abb78bd 100644 --- a/products/utils.py +++ b/products/utils.py @@ -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: diff --git a/products/views.py b/products/views.py index fa46b42..989c7ae 100644 --- a/products/views.py +++ b/products/views.py @@ -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]