#51 feat: serializadores de categorias y categorias anidadas en productos
Issue #51: discriminacion y administracion de categorias de productos. - ProductCategorySerializer (name/external_id read_only) - ProductCategoryBriefSerializer para el JSON de productos - ProductSerializer.categories ahora devuelve objetos anidados
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
from .catalogue_images import CatalogueImageSerializer
|
from .catalogue_images import CatalogueImageSerializer
|
||||||
from .products import ProductSerializer, ListProductSerializer
|
from .products import (
|
||||||
|
ProductSerializer,
|
||||||
|
ListProductSerializer,
|
||||||
|
ProductCategorySerializer,
|
||||||
|
ProductCategoryBriefSerializer,
|
||||||
|
)
|
||||||
from .customers import CustomerSerializer, ListCustomerSerializer
|
from .customers import CustomerSerializer, ListCustomerSerializer
|
||||||
from .sales import (
|
from .sales import (
|
||||||
SaleSerializer,
|
SaleSerializer,
|
||||||
@@ -26,6 +31,8 @@ __all__ = [
|
|||||||
# Products
|
# Products
|
||||||
"ProductSerializer",
|
"ProductSerializer",
|
||||||
"ListProductSerializer",
|
"ListProductSerializer",
|
||||||
|
"ProductCategorySerializer",
|
||||||
|
"ProductCategoryBriefSerializer",
|
||||||
# Customers
|
# Customers
|
||||||
"CustomerSerializer",
|
"CustomerSerializer",
|
||||||
"ListCustomerSerializer",
|
"ListCustomerSerializer",
|
||||||
|
|||||||
@@ -4,11 +4,32 @@ from ..models.products import Product, ProductCategory
|
|||||||
from ..models.provenance import Supplier
|
from ..models.provenance import Supplier
|
||||||
|
|
||||||
|
|
||||||
|
class ProductCategorySerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ProductCategory
|
||||||
|
fields = [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"external_id",
|
||||||
|
"description",
|
||||||
|
"alternative_name",
|
||||||
|
"active",
|
||||||
|
]
|
||||||
|
read_only_fields = ["name", "external_id"]
|
||||||
|
|
||||||
|
|
||||||
|
class ProductCategoryBriefSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ProductCategory
|
||||||
|
fields = ["id", "name", "alternative_name", "active"]
|
||||||
|
|
||||||
|
|
||||||
class ProductSerializer(serializers.ModelSerializer):
|
class ProductSerializer(serializers.ModelSerializer):
|
||||||
catalogue_images = serializers.SerializerMethodField()
|
catalogue_images = serializers.SerializerMethodField()
|
||||||
suppliers = serializers.PrimaryKeyRelatedField(
|
suppliers = serializers.PrimaryKeyRelatedField(
|
||||||
many=True, queryset=Supplier.objects.all(), required=False
|
many=True, queryset=Supplier.objects.all(), required=False
|
||||||
)
|
)
|
||||||
|
categories = ProductCategoryBriefSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from ..models.products import ProductCategory
|
from ..models.products import Product, ProductCategory
|
||||||
|
|
||||||
|
|
||||||
class TestProductCategoryModel(TestCase):
|
class TestProductCategoryModel(TestCase):
|
||||||
@@ -22,4 +22,56 @@ class TestProductCategoryModel(TestCase):
|
|||||||
category = ProductCategory.objects.create(name="Granos")
|
category = ProductCategory.objects.create(name="Granos")
|
||||||
self.assertTrue(category.active)
|
self.assertTrue(category.active)
|
||||||
self.assertEqual(category.description, "")
|
self.assertEqual(category.description, "")
|
||||||
self.assertIsNone(category.alternative_name)
|
self.assertIsNone(category.alternative_name)
|
||||||
|
|
||||||
|
|
||||||
|
class TestProductCategorySerializers(TestCase):
|
||||||
|
def test_product_category_serializer_devuelve_campos_don_confiao(self):
|
||||||
|
from ..serializers import ProductCategorySerializer
|
||||||
|
|
||||||
|
category = ProductCategory.objects.create(
|
||||||
|
name="Bebidas",
|
||||||
|
external_id="8",
|
||||||
|
description="Bebidas naturales",
|
||||||
|
alternative_name="Jugos",
|
||||||
|
active=False,
|
||||||
|
)
|
||||||
|
data = ProductCategorySerializer(category).data
|
||||||
|
self.assertEqual(
|
||||||
|
data,
|
||||||
|
{
|
||||||
|
"id": category.pk,
|
||||||
|
"name": "Bebidas",
|
||||||
|
"external_id": "8",
|
||||||
|
"description": "Bebidas naturales",
|
||||||
|
"alternative_name": "Jugos",
|
||||||
|
"active": False,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_product_serializer_incluye_categorias_anidadas(self):
|
||||||
|
from ..serializers import ProductSerializer
|
||||||
|
|
||||||
|
category = ProductCategory.objects.create(
|
||||||
|
name="Bebidas", external_id="8", alternative_name="Jugos"
|
||||||
|
)
|
||||||
|
product = Product.objects.create(
|
||||||
|
name="Jugo de naranja",
|
||||||
|
price=2500,
|
||||||
|
measuring_unit="UNIT",
|
||||||
|
external_id="10",
|
||||||
|
)
|
||||||
|
product.categories.add(category)
|
||||||
|
|
||||||
|
data = ProductSerializer(product).data
|
||||||
|
self.assertEqual(
|
||||||
|
data["categories"],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": category.pk,
|
||||||
|
"name": "Bebidas",
|
||||||
|
"alternative_name": "Jugos",
|
||||||
|
"active": True,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user