130 lines
3.7 KiB
Python
130 lines
3.7 KiB
Python
from rest_framework import serializers
|
|
from apps.product import models as product_models
|
|
from apps.authorization.api.v1 import serializers as authorize_serializers
|
|
|
|
|
|
class ProductCategorySerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.ProductCategory
|
|
fields = '__all__'
|
|
|
|
|
|
class ProductSerializer(serializers.ModelSerializer):
|
|
""" Serializer of product """
|
|
|
|
class Meta:
|
|
model = product_models.Product
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
""" Custom output of product serializer """
|
|
|
|
representation = super().to_representation(instance)
|
|
if instance.category:
|
|
representation['category'] = {
|
|
'id': instance.category.id,
|
|
'name': instance.category.name
|
|
}
|
|
|
|
return representation
|
|
|
|
|
|
class AttributeSerializer(serializers.ModelSerializer):
|
|
""" serialize attributes of reference product """
|
|
|
|
class Meta:
|
|
model = product_models.Attribute
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
representation = super().to_representation(instance)
|
|
if instance.product:
|
|
representation['product'] = {
|
|
'id': instance.product.id,
|
|
'name': instance.product.name
|
|
}
|
|
return representation
|
|
|
|
|
|
class AttributeValueSerializer(serializers.ModelSerializer):
|
|
""" serialize attribute values for child products """
|
|
|
|
class Meta:
|
|
model = product_models.AttributeValue
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
""" Custom output """
|
|
|
|
representation = super().to_representation(instance)
|
|
if instance.product:
|
|
representation['product'] = ProductSerializer(instance.product).data
|
|
if instance.attribute:
|
|
representation['attribute'] = AttributeSerializer(instance.attribute).data
|
|
|
|
return representation
|
|
|
|
|
|
class BrokerSerializer(serializers.ModelSerializer):
|
|
""" serialize product broker """
|
|
|
|
class Meta:
|
|
model = product_models.Broker
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
representation = super().to_representation(instance)
|
|
if instance.organization_relations:
|
|
representation['organization_relations'] = authorize_serializers.UserRelationSerializer(
|
|
instance.organization_relations
|
|
).data
|
|
|
|
return representation
|
|
|
|
|
|
class SaleUnitSerializer(serializers.ModelSerializer):
|
|
""" serialize unit of products for sale """
|
|
|
|
class Meta:
|
|
model = product_models.SaleUnit
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
representation = super().to_representation(instance)
|
|
if instance.product:
|
|
representation['product'] = {
|
|
'id': instance.product.id,
|
|
'name': instance.product.name
|
|
}
|
|
return representation
|
|
|
|
|
|
class IncentivePlanSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.IncentivePlan
|
|
fields = '__all__'
|
|
|
|
|
|
class QuotaSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.Quota
|
|
fields = '__all__'
|
|
|
|
|
|
class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.QuotaIncentiveAssignment
|
|
fields = '__all__'
|
|
|
|
|
|
class QuotaBrokerValueSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.QuotaBrokerValue
|
|
fields = '__all__'
|
|
|
|
|
|
class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = product_models.QuotaLivestockAllocation
|
|
fields = '__all__'
|