From 4de246f58e112fd55ac7816c6f2e6e5a934ca558 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Mon, 22 Dec 2025 15:28:40 +0330 Subject: [PATCH] import - service area in organization & validate in org rancher link --- apps/authentication/models.py | 1 + apps/herd/exception.py | 19 +++++++++++++++++++ apps/herd/web/api/v1/serializers.py | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/apps/authentication/models.py b/apps/authentication/models.py index 4d12395..a418683 100644 --- a/apps/authentication/models.py +++ b/apps/authentication/models.py @@ -146,6 +146,7 @@ class Organization(BaseModel): separate_warehouse = models.BooleanField(default=False) has_pos = models.BooleanField(default=False) additional_data = models.JSONField(default=dict) + service_area = models.ManyToManyField(City, related_name='service_area') def __str__(self): return f'{self.name}-{self.type}' diff --git a/apps/herd/exception.py b/apps/herd/exception.py index d4b6664..8c0c2ca 100644 --- a/apps/herd/exception.py +++ b/apps/herd/exception.py @@ -15,3 +15,22 @@ class DuplicateRancherException(APIException): class HerdCapacityException(APIException): status_code = status.HTTP_403_FORBIDDEN default_detail = "مقدار حجم سبک و سنگین وارد شده از ظرفیت گله بیشتر میباشد" # noqa + + +class HerdException(APIException): + """ if quota is not available """ + + status_code = status.HTTP_400_BAD_REQUEST + default_detail = "خطا در اطلاعات گله" # noqa + default_code = 'error' + + def __init__(self, message=None, status_code=None, code=None): + if status_code is not None: + self.status_code = status_code + + detail = { + "message": message, + "status_code": status_code + } + + super().__init__(detail) diff --git a/apps/herd/web/api/v1/serializers.py b/apps/herd/web/api/v1/serializers.py index 0c74c81..a9fb02f 100644 --- a/apps/herd/web/api/v1/serializers.py +++ b/apps/herd/web/api/v1/serializers.py @@ -5,7 +5,7 @@ from apps.authentication.api.v1.serializers.serializer import ( ProvinceSerializer, CitySerializer ) -from apps.herd.exception import HerdCapacityException +from apps.herd.exception import HerdCapacityException, HerdException from apps.herd.models import Herd, Rancher, RancherOrganizationLink @@ -70,6 +70,19 @@ class RancherOrganizationLinkSerializer(serializers.ModelSerializer): model = RancherOrganizationLink fields = '__all__' + def validate(self, attrs): + rancher = attrs['rancher'] + organization = attrs['organization'] + + if self.Meta.model.objects.filter(rancher=rancher, organization=organization).exists(): + raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa + + if self.instance: + if rancher.city not in organization.service_area: + raise HerdException(message="دامدار در محدوده فعالیت تعاونی قرار ندارد", status_code=403) # noqa + + return attrs + def to_representation(self, instance): representation = super().to_representation(instance)