import - validation on rancher organization link serializer
This commit is contained in:
@@ -74,13 +74,32 @@ class RancherOrganizationLinkSerializer(serializers.ModelSerializer):
|
||||
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 not self.instance:
|
||||
if self.Meta.model.objects.filter(rancher=rancher, organization=organization).exists():
|
||||
raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa
|
||||
|
||||
elif self.Meta.model.objects.filter(
|
||||
rancher=rancher,
|
||||
organization__parent_organization=organization.parent_organization
|
||||
).exists():
|
||||
raise HerdException(
|
||||
message="دامدار نمیتواند زیر مجموعه دو تعاونی با اتحادیه یکسان باشند", # noqa
|
||||
status_code=403
|
||||
)
|
||||
|
||||
if self.instance:
|
||||
if rancher.city not in organization.service_area:
|
||||
if rancher.city not in organization.service_area.all():
|
||||
raise HerdException(message="دامدار در محدوده فعالیت تعاونی قرار ندارد", status_code=403) # noqa
|
||||
|
||||
elif self.instance.organization == organization:
|
||||
raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa
|
||||
|
||||
elif self.instance.organization.parent_organization == organization.parent_organization:
|
||||
raise HerdException(
|
||||
message="دامدار نمیتواند زیر مجموعه دو تعاونی با اتحادیه یکسان باشند", # noqa
|
||||
status_code=403
|
||||
)
|
||||
|
||||
return attrs
|
||||
|
||||
def to_representation(self, instance):
|
||||
|
||||
Reference in New Issue
Block a user