#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 .products import ProductSerializer, ListProductSerializer
|
||||
from .products import (
|
||||
ProductSerializer,
|
||||
ListProductSerializer,
|
||||
ProductCategorySerializer,
|
||||
ProductCategoryBriefSerializer,
|
||||
)
|
||||
from .customers import CustomerSerializer, ListCustomerSerializer
|
||||
from .sales import (
|
||||
SaleSerializer,
|
||||
@@ -26,6 +31,8 @@ __all__ = [
|
||||
# Products
|
||||
"ProductSerializer",
|
||||
"ListProductSerializer",
|
||||
"ProductCategorySerializer",
|
||||
"ProductCategoryBriefSerializer",
|
||||
# Customers
|
||||
"CustomerSerializer",
|
||||
"ListCustomerSerializer",
|
||||
|
||||
@@ -4,11 +4,32 @@ from ..models.products import Product, ProductCategory
|
||||
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):
|
||||
catalogue_images = serializers.SerializerMethodField()
|
||||
suppliers = serializers.PrimaryKeyRelatedField(
|
||||
many=True, queryset=Supplier.objects.all(), required=False
|
||||
)
|
||||
categories = ProductCategoryBriefSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from ..models.products import ProductCategory
|
||||
from ..models.products import Product, ProductCategory
|
||||
|
||||
|
||||
class TestProductCategoryModel(TestCase):
|
||||
@@ -23,3 +23,55 @@ class TestProductCategoryModel(TestCase):
|
||||
self.assertTrue(category.active)
|
||||
self.assertEqual(category.description, "")
|
||||
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