import - service area in organization & validate in org rancher link
This commit is contained in:
@@ -146,6 +146,7 @@ class Organization(BaseModel):
|
|||||||
separate_warehouse = models.BooleanField(default=False)
|
separate_warehouse = models.BooleanField(default=False)
|
||||||
has_pos = models.BooleanField(default=False)
|
has_pos = models.BooleanField(default=False)
|
||||||
additional_data = models.JSONField(default=dict)
|
additional_data = models.JSONField(default=dict)
|
||||||
|
service_area = models.ManyToManyField(City, related_name='service_area')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.name}-{self.type}'
|
return f'{self.name}-{self.type}'
|
||||||
|
|||||||
@@ -15,3 +15,22 @@ class DuplicateRancherException(APIException):
|
|||||||
class HerdCapacityException(APIException):
|
class HerdCapacityException(APIException):
|
||||||
status_code = status.HTTP_403_FORBIDDEN
|
status_code = status.HTTP_403_FORBIDDEN
|
||||||
default_detail = "مقدار حجم سبک و سنگین وارد شده از ظرفیت گله بیشتر میباشد" # noqa
|
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)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from apps.authentication.api.v1.serializers.serializer import (
|
|||||||
ProvinceSerializer,
|
ProvinceSerializer,
|
||||||
CitySerializer
|
CitySerializer
|
||||||
)
|
)
|
||||||
from apps.herd.exception import HerdCapacityException
|
from apps.herd.exception import HerdCapacityException, HerdException
|
||||||
from apps.herd.models import Herd, Rancher, RancherOrganizationLink
|
from apps.herd.models import Herd, Rancher, RancherOrganizationLink
|
||||||
|
|
||||||
|
|
||||||
@@ -70,6 +70,19 @@ class RancherOrganizationLinkSerializer(serializers.ModelSerializer):
|
|||||||
model = RancherOrganizationLink
|
model = RancherOrganizationLink
|
||||||
fields = '__all__'
|
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):
|
def to_representation(self, instance):
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user