remove variation coeffisiont sale unti - change structure
This commit is contained in:
0
apps/product/web/api/v1/serializers/__init__.py
Normal file
0
apps/product/web/api/v1/serializers/__init__.py
Normal file
266
apps/product/web/api/v1/serializers/product_serializers.py
Normal file
266
apps/product/web/api/v1/serializers/product_serializers.py
Normal file
@@ -0,0 +1,266 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if instance.type:
|
||||
representation['type'] = {
|
||||
'id': instance.type.id,
|
||||
'unit': instance.type.unit
|
||||
}
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeValueSerializer(serializers.ModelSerializer):
|
||||
""" serialize attribute values for child products """
|
||||
|
||||
class Meta:
|
||||
model = product_models.AttributeValue
|
||||
fields = '__all__'
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.attribute = validated_data.get('attribute', instance.attribute)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class BrokerSerializer(serializers.ModelSerializer):
|
||||
""" serialize product broker """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Broker
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.organization:
|
||||
representation['organization'] = OrganizationSerializer(
|
||||
instance.organization
|
||||
).data
|
||||
|
||||
if instance.product:
|
||||
representation['product'] = ProductSerializer(
|
||||
instance.product
|
||||
).data
|
||||
|
||||
if instance.calculation_strategy:
|
||||
representation['calculation_strategy'] = {
|
||||
'id': instance.calculation_strategy.id,
|
||||
'unit': instance.calculation_strategy.unit
|
||||
}
|
||||
|
||||
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): # noqa
|
||||
class Meta:
|
||||
model = product_models.IncentivePlan
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class QuotaSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.Quota
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.Quota):
|
||||
representation['incentive_plan'] = QuotaIncentiveAssignmentSerializer(
|
||||
instance.incentive_assignments.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['attribute_values'] = AttributeValueSerializer(
|
||||
instance.attribute_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['brokers'] = QuotaBrokerValueSerializer(
|
||||
instance.broker_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
|
||||
instance.livestock_allocations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
|
||||
instance.livestock_age_limitations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota_id = validated_data.get('quota_id', instance.quota_id)
|
||||
instance.quota_code = validated_data.get('quota_code', instance.quota_code)
|
||||
instance.quota_weight = validated_data.get('quota_weight', instance.quota_weight)
|
||||
instance.remaining_weight = validated_data.get('remaining_weight', instance.remaining_weight)
|
||||
instance.quota_distributed = validated_data.get('quota_distributed', instance.quota_distributed)
|
||||
instance.quota_balance = validated_data.get('quota_balance', instance.quota_balance)
|
||||
instance.product = validated_data.get('product', instance.product)
|
||||
instance.sale_type = validated_data.get('sale_type', instance.sale_type)
|
||||
instance.month_choices = validated_data.get('month_choices', instance.month_choices)
|
||||
instance.group = validated_data.get('group', instance.group)
|
||||
instance.has_distribution_limit = validated_data.get('has_distribution_limit', instance.has_distribution_limit)
|
||||
instance.distribution_mode = validated_data.get('distribution_mode', instance.distribution_mode)
|
||||
instance.base_price_factory = validated_data.get('base_price_factory', instance.base_price_factory)
|
||||
instance.base_price_cooperative = validated_data.get('base_price_cooperative', instance.base_price_cooperative)
|
||||
instance.final_price = validated_data.get('final_price', instance.final_price)
|
||||
instance.is_closed = validated_data.get('is_closed', instance.is_closed)
|
||||
instance.closed_at = validated_data.get('closed_at', instance.closed_at)
|
||||
instance.save()
|
||||
|
||||
instance.assigned_organizations.clear()
|
||||
instance.assigned_organizations.add(
|
||||
*(validated_data.get('assigned_organizations', instance.assigned_organizations))
|
||||
)
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaIncentiveAssignment
|
||||
fields = '__all__'
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.incentive_plan = validated_data.get('incentive_plan', instance.incentive_plan)
|
||||
instance.heavy_value = validated_data.get('heavy_value', instance.heavy_value)
|
||||
instance.light_value = validated_data.get('light_value', instance.light_value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaBrokerValueSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.QuotaBrokerValue
|
||||
fields = '__all__'
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.broker = validated_data.get('broker', instance.broker)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLivestockAllocation
|
||||
fields = '__all__'
|
||||
extra_kwargs = {
|
||||
'livestock_group': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_type': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_subtype': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_group = validated_data.get('livestock_group', instance.livestock_group)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLiveStockAgeLimitation
|
||||
fields = '__all__'
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.age_month = validated_data.get('age_month', instance.age_month)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
@@ -0,0 +1,59 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.product.web.api.v1.serializers.product_serializers import QuotaSerializer
|
||||
from django.db import models
|
||||
from apps.product.exceptions import (
|
||||
QuotaWeightException,
|
||||
QuotaClosedException,
|
||||
QuotaExpiredTimeException
|
||||
)
|
||||
|
||||
|
||||
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaDistribution
|
||||
fields = '__all__'
|
||||
extra_kwargs = {
|
||||
'assigner_organization': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def validate(self, data):
|
||||
"""
|
||||
to validate if distribution weight
|
||||
more than quota weight raise exception
|
||||
or if quota is closed raise exception
|
||||
"""
|
||||
|
||||
quota = data['quota']
|
||||
amount = data['weight']
|
||||
instance_id = self.instance.id if self.instance else None
|
||||
|
||||
# check quota expired time
|
||||
if not quota.is_in_valid_time():
|
||||
raise QuotaExpiredTimeException()
|
||||
|
||||
# check if quota is closed
|
||||
if quota.is_closed:
|
||||
raise QuotaClosedException()
|
||||
|
||||
# total quota distributions weight
|
||||
total = product_models.QuotaDistribution.objects.filter(
|
||||
quota=quota
|
||||
).exclude(id=instance_id).aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
if total + amount > quota.quota_weight:
|
||||
raise QuotaWeightException()
|
||||
|
||||
return data
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.quota:
|
||||
representation['quota'] = QuotaSerializer(instance.quota).data
|
||||
|
||||
return representation
|
||||
Reference in New Issue
Block a user