count livestocks by type & set in herd model by signals
This commit is contained in:
@@ -5,3 +5,8 @@ from rest_framework import status
|
|||||||
class UniqueRancherApiException(APIException):
|
class UniqueRancherApiException(APIException):
|
||||||
status_code = status.HTTP_403_FORBIDDEN
|
status_code = status.HTTP_403_FORBIDDEN
|
||||||
default_detail = "دامدار با این کد ملی از قبل در سیستم وجود دارد" # noqa
|
default_detail = "دامدار با این کد ملی از قبل در سیستم وجود دارد" # noqa
|
||||||
|
|
||||||
|
|
||||||
|
class HerdCapacityException(APIException):
|
||||||
|
status_code = status.HTTP_403_FORBIDDEN
|
||||||
|
default_detail = "مقدار حجم سبک و سنگین وارد شده از ظرفیت گله بیشتر میباشد" # noqa
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ from apps.authentication.api.v1.serializers.serializer import (
|
|||||||
ProvinceSerializer,
|
ProvinceSerializer,
|
||||||
CitySerializer
|
CitySerializer
|
||||||
)
|
)
|
||||||
from rest_framework import serializers
|
from apps.herd.exception import HerdCapacityException
|
||||||
from apps.herd.models import Herd, Rancher
|
from apps.herd.models import Herd, Rancher
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
class HerdSerializer(serializers.ModelSerializer):
|
class HerdSerializer(serializers.ModelSerializer):
|
||||||
@@ -14,6 +15,14 @@ class HerdSerializer(serializers.ModelSerializer):
|
|||||||
model = Herd
|
model = Herd
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
""" some validations for herd """
|
||||||
|
|
||||||
|
if attrs['heavy_livestock_number'] + attrs['light_livestock_number'] > attrs['capacity']:
|
||||||
|
raise HerdCapacityException()
|
||||||
|
|
||||||
|
return attrs
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
""" Customize serializer output """
|
""" Customize serializer output """
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ from django.apps import AppConfig
|
|||||||
class LivestockConfig(AppConfig):
|
class LivestockConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'apps.livestock'
|
name = 'apps.livestock'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
import apps.livestock.signals
|
||||||
|
|||||||
31
apps/livestock/signals.py
Normal file
31
apps/livestock/signals.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
from django.db.models.signals import post_save, post_delete
|
||||||
|
from django.dispatch import receiver
|
||||||
|
from django.db.models import Sum, Count, Q
|
||||||
|
from .models import LiveStock
|
||||||
|
|
||||||
|
|
||||||
|
def update_herd_statistics(instance: LiveStock):
|
||||||
|
"""
|
||||||
|
update light & heavy livestock number in rancher herd
|
||||||
|
"""
|
||||||
|
counts = LiveStock.objects.filter(
|
||||||
|
herd__rancher=instance.herd.rancher,
|
||||||
|
herd=instance.herd
|
||||||
|
).aggregate(
|
||||||
|
light_livestock_number=Count('id', filter=Q(weight_type='L')),
|
||||||
|
heavy_livestock_number=Count('id', filter=Q(weight_type='H'))
|
||||||
|
)
|
||||||
|
|
||||||
|
instance.herd.light_livestock_number = counts['light_livestock_number']
|
||||||
|
instance.herd.heavy_livestock_number = counts['heavy_livestock_number']
|
||||||
|
instance.herd.save(update_fields=['light_livestock_number', 'heavy_livestock_number'])
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=LiveStock)
|
||||||
|
def update_herd_on_livestock_save(sender, instance, **kwargs):
|
||||||
|
update_herd_statistics(instance)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_delete, sender=LiveStock)
|
||||||
|
def update_herd_on_livestock_delete(sender, instance, **kwargs):
|
||||||
|
update_herd_statistics(instance)
|
||||||
Reference in New Issue
Block a user