fix update organization - set signal for calculate remaining quota weight after distribution
This commit is contained in:
@@ -221,6 +221,34 @@ class OrganizationViewSet(ModelViewSet):
|
|||||||
else:
|
else:
|
||||||
return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
|
return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def update(self, request, *args, **kwargs):
|
||||||
|
""" update organization data """
|
||||||
|
|
||||||
|
partial = kwargs.pop('partial', False)
|
||||||
|
instance = self.get_object() # get organization instance
|
||||||
|
serializer = self.get_serializer(
|
||||||
|
instance,
|
||||||
|
data=request.data['organization'],
|
||||||
|
partial=partial
|
||||||
|
)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
organization = serializer.save()
|
||||||
|
|
||||||
|
if 'user_relations' in request.data.keys():
|
||||||
|
user_relations = CustomOperations().custom_update( # update user relations
|
||||||
|
request=request,
|
||||||
|
view=authorize_view.UserRelationViewSet(),
|
||||||
|
data_key='user_relations',
|
||||||
|
additional_data={'organization': organization.id} # noqa
|
||||||
|
)
|
||||||
|
serializer_data = serializer.data
|
||||||
|
serializer_data.update(
|
||||||
|
{'user_relations': user_relations}
|
||||||
|
)
|
||||||
|
return Response(serializer_data, status=status.HTTP_200_OK)
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
methods=['get'],
|
methods=['get'],
|
||||||
detail=False,
|
detail=False,
|
||||||
|
|||||||
@@ -214,15 +214,13 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
|||||||
""" update user organization information """ # noqa
|
""" update user organization information """ # noqa
|
||||||
instance.name = validated_data.get('name', instance.name)
|
instance.name = validated_data.get('name', instance.name)
|
||||||
if validated_data.get('type'):
|
if validated_data.get('type'):
|
||||||
instance.type = OrganizationType.objects.get(id=validated_data.get('type', instance.type))
|
instance.type = validated_data['type']
|
||||||
if validated_data.get('province'):
|
if validated_data.get('province'):
|
||||||
instance.province = Province.objects.get(id=validated_data.get('province', instance.province))
|
instance.province = validated_data['province']
|
||||||
if validated_data.get('city'):
|
if validated_data.get('city'):
|
||||||
instance.city = City.objects.get(id=validated_data.get('city', instance.city))
|
instance.city = validated_data['city']
|
||||||
if validated_data.get('parent_organization'):
|
if validated_data.get('parent_organization'):
|
||||||
instance.parent_organization = Organization.objects.get(
|
instance.parent_organization = validated_data['parent_organization']
|
||||||
id=validated_data.get('parent_organization', instance.parent_organization)
|
|
||||||
)
|
|
||||||
instance.national_unique_id = validated_data.get('national_unique_id', instance.national_unique_id)
|
instance.national_unique_id = validated_data.get('national_unique_id', instance.national_unique_id)
|
||||||
instance.save()
|
instance.save()
|
||||||
return instance
|
return instance
|
||||||
|
|||||||
@@ -246,6 +246,7 @@ class Quota(BaseModel):
|
|||||||
quota_id = models.PositiveBigIntegerField(null=True, blank=True)
|
quota_id = models.PositiveBigIntegerField(null=True, blank=True)
|
||||||
quota_code = models.CharField(max_length=15, null=True)
|
quota_code = models.CharField(max_length=15, null=True)
|
||||||
quota_weight = models.PositiveIntegerField(default=0)
|
quota_weight = models.PositiveIntegerField(default=0)
|
||||||
|
remaining_quota_weight = models.PositiveBigIntegerField(default=0)
|
||||||
quota_distributed = models.PositiveIntegerField(default=0)
|
quota_distributed = models.PositiveIntegerField(default=0)
|
||||||
quota_balance = models.PositiveIntegerField(default=0)
|
quota_balance = models.PositiveIntegerField(default=0)
|
||||||
product = models.ForeignKey(
|
product = models.ForeignKey(
|
||||||
|
|||||||
19
apps/product/signals.py
Normal file
19
apps/product/signals.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from django.db.models import Sum
|
||||||
|
from django.db.models.signals import post_save, post_delete
|
||||||
|
from django.dispatch import receiver
|
||||||
|
from .models import QuotaDistribution, Quota
|
||||||
|
|
||||||
|
|
||||||
|
def recalculate_remaining_amount(quota):
|
||||||
|
total_distributed = quota.distributions.aggregate(
|
||||||
|
total=Sum('amount_kg')
|
||||||
|
)['total'] or 0
|
||||||
|
|
||||||
|
quota.remaining_amount_kg = quota.total_amount_kg - total_distributed
|
||||||
|
quota.save(update_fields=["remaining_amount_kg"])
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=QuotaDistribution)
|
||||||
|
@receiver(post_delete, sender=QuotaDistribution)
|
||||||
|
def update_quota_remaining(sender, instance, **kwargs):
|
||||||
|
recalculate_remaining_amount(instance.quota)
|
||||||
@@ -352,6 +352,7 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
|||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
quota = serializer.save()
|
quota = serializer.save()
|
||||||
|
quota.remaining_quota_weight = quota.quota_weight
|
||||||
|
|
||||||
# create incentive plan
|
# create incentive plan
|
||||||
if 'incentive_plan_data' in request.data.keys():
|
if 'incentive_plan_data' in request.data.keys():
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
""" to validate if distribution weight
|
""" to validate if distribution weight
|
||||||
more than quota weight raise exception """
|
more than quota weight raise exception """
|
||||||
|
|
||||||
quota = product_models.Quota.objects.get(id=data['quota'])
|
quota = data['quota']
|
||||||
amount = data['weight']
|
amount = data['weight']
|
||||||
instance_id = self.instance.id if self.instance else None
|
instance_id = self.instance.id if self.instance else None
|
||||||
|
|
||||||
@@ -29,8 +29,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
).exclude(id=instance_id).aggregate(
|
).exclude(id=instance_id).aggregate(
|
||||||
total=models.Sum('weight')
|
total=models.Sum('weight')
|
||||||
)['total'] or 0
|
)['total'] or 0
|
||||||
print(total)
|
if total + amount > quota.quota_weight:
|
||||||
if total + amount > self.instance.weight:
|
|
||||||
raise QuotaWeightException()
|
raise QuotaWeightException()
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user