From 2a05beec072d2b9b6d5f2d734c6175855e4d7632 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 26 Jan 2021 12:00:44 +0000 Subject: [PATCH] serialized tags working --- back_latienda/settings/base.py | 1 + products/factories.py | 7 +++---- products/models.py | 6 +++--- products/serializers.py | 4 +--- products/tests.py | 28 +++++++++++++++++++++++++--- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/back_latienda/settings/base.py b/back_latienda/settings/base.py index ff94f1e..a407d89 100644 --- a/back_latienda/settings/base.py +++ b/back_latienda/settings/base.py @@ -45,6 +45,7 @@ INSTALLED_APPS = [ 'django_filters', 'corsheaders', 'taggit_serializer', + 'tagulous', # local apps 'core', diff --git a/products/factories.py b/products/factories.py index 5fc7e17..77ebf13 100644 --- a/products/factories.py +++ b/products/factories.py @@ -16,7 +16,6 @@ class ProductFactory(DjangoModelFactory): sku = FuzzyText(prefix='SKU_', length=10) name = FuzzyText(prefix='NAME_', length=10) description = FuzzyText(prefix='DECRIPTION', length=100) - image = None url = FuzzyText(prefix='http://WEB-LINK-', suffix='.test', length=10) price = FuzzyDecimal(low=1.00) shipping_cost = FuzzyDecimal(low=1.00) @@ -26,9 +25,9 @@ class ProductFactory(DjangoModelFactory): update_date = FuzzyDateTime(start_dt=timezone.now()) discount = FuzzyDecimal(low=0.00, high=100.00) stock = FuzzyInteger(low=0) - # tags = models.ManyToMany(Tag, null=True, blank=True ) - # category = models.ForeignKey(Tag, null=true) # main tag category - # attributes = models.ManyToMany(Tag, null=True, blank=True ) + tags = ['test-tag'] + category = 'top-category' # main tag category + attributes = ['programming/python', 'testing'] identifiers = FuzzyText(prefix='IDENTIFIERS_', length=100) class Meta: diff --git a/products/models.py b/products/models.py index 8afd717..3dc9742 100644 --- a/products/models.py +++ b/products/models.py @@ -32,9 +32,9 @@ class Product(models.Model): update_date = models.DateTimeField('Fecha de actualización de producto', null=True, blank=True) discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True) stock = models.PositiveIntegerField('Stock', null=True) - tags = TagField( ) - category = SingleTagField() # main tag category - attributes = TagField() + tags = TagField(force_lowercase=True,max_count=5, tree=True) + category = SingleTagField(null=True) # main tag category + attributes = TagField(force_lowercase=True,max_count=5, tree=True) identifiers = models.TextField('Identificador único de producto', null=True, blank=True) # internal diff --git a/products/serializers.py b/products/serializers.py index 70f85ab..e7d2fcf 100644 --- a/products/serializers.py +++ b/products/serializers.py @@ -1,6 +1,5 @@ from rest_framework import serializers -from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer from products.models import Product from utils.tag_serializers import TagListSerializerField, TaggitSerializer, SingleTagSerializerField @@ -14,5 +13,4 @@ class ProductSerializer(TaggitSerializer, serializers.ModelSerializer): class Meta: model = Product - # exclude = ['created', 'updated', 'creator'] - fields = '__all__' + exclude = ['created', 'updated', 'creator'] diff --git a/products/tests.py b/products/tests.py index b7c8759..fcb973a 100644 --- a/products/tests.py +++ b/products/tests.py @@ -118,7 +118,7 @@ class ProductViewSetTest(APITestCase): 'discount': '0.05', 'stock': 22, 'tags': ['tag1, tag2'], - # 'category': '', + 'category': 'Mr', 'attributes': ['color/red', 'size/xxl'], 'identifiers': '34rf34f43c43', } @@ -129,7 +129,6 @@ class ProductViewSetTest(APITestCase): # Query endpoint response = self.client.post(self.endpoint, data=data, format='json') - import ipdb; ipdb.set_trace() # Assert endpoint returns created status self.assertEqual(response.status_code, status.HTTP_201_CREATED) @@ -188,13 +187,36 @@ class ProductViewSetTest(APITestCase): # Create instances instance = self.factory() + # Define request data + company = CompanyFactory() + data = { + 'company': company.id, + 'sku': 'qwerewq', + 'name': 'qwerewq', + 'description': 'qwerewq', + 'url': 'http://qwerewq.com', + 'price': '12.21', + 'shipping_cost': '21.12', + 'shipping_terms': 'qwerewq', + 'source': 'SYNCHRONIZED', + 'sourcing_date': datetime.datetime.now().isoformat()+'Z', + 'update_date': datetime.datetime.now().isoformat()+'Z', + 'discount': '0.05', + 'stock': 22, + 'tags': ['tag1, tag2'], + 'category': 'Mr', + 'attributes': ['color/red', 'size/xxl'], + 'identifiers': '34rf34f43c43', + } + # Authenticate user token = get_tokens_for_user(self.user) self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") # Query endpoint url = self.endpoint + f'{instance.pk}/' - response = self.client.put(url, data={}, format='json') + response = self.client.put(url, data=data, format='json') + # Assert endpoint returns OK code self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)