add - limitation of organization by is_repeatable org_type
This commit is contained in:
@@ -4,7 +4,8 @@ from django.contrib.auth.hashers import make_password
|
||||
from django.db.models import Q
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.authentication.exceptions import UserExistException, OrganizationNationalUniqueIDException
|
||||
from apps.authentication.exceptions import UserExistException, OrganizationNationalUniqueIDException, \
|
||||
OrganizationTypeRepeatableException
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
@@ -231,6 +232,8 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
||||
@ validate national_unique_code to be unique
|
||||
@ validate to organization type field
|
||||
"""
|
||||
|
||||
# check national_unique_id
|
||||
national_unique_id = attrs['national_unique_id']
|
||||
|
||||
if not self.instance:
|
||||
@@ -241,6 +244,31 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
||||
if self.Meta.model.objects.filter(national_unique_id=national_unique_id).exists():
|
||||
raise OrganizationNationalUniqueIDException()
|
||||
|
||||
# check organization type field
|
||||
# if is repeatable of type, organization will not be recreating
|
||||
org_type = attrs['type']
|
||||
if not org_type.is_repeatable:
|
||||
if org_type.org_type_field == 'PR' and self.Meta.model.objects.filter(
|
||||
type=org_type,
|
||||
province=attrs['province']
|
||||
).exists():
|
||||
raise OrganizationTypeRepeatableException()
|
||||
|
||||
if org_type.org_type_field == 'CI' and self.Meta.model.objects.filter(
|
||||
type=org_type,
|
||||
city=attrs['city']
|
||||
).exists():
|
||||
raise OrganizationTypeRepeatableException()
|
||||
|
||||
if org_type.org_type_field == 'CO' and self.Meta.model.objects.filter(
|
||||
type=org_type,
|
||||
province=attrs['province'],
|
||||
city=attrs['city']
|
||||
).exists():
|
||||
raise OrganizationTypeRepeatableException()
|
||||
|
||||
return attrs
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output """
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
@@ -27,6 +27,14 @@ class OrganizationNationalUniqueIDException(APIException):
|
||||
default_code = 'organization_unique_id_exist'
|
||||
|
||||
|
||||
class OrganizationTypeRepeatableException(APIException):
|
||||
""" if organization type is repeatable """
|
||||
|
||||
status_code = status.HTTP_403_FORBIDDEN
|
||||
default_detail = _('این نوع سازمان قابلیت تکرار ندارد و از قبل وجود دارد') # noqa
|
||||
default_code = 'organization_type_repeatable'
|
||||
|
||||
|
||||
class UserExistException(APIException):
|
||||
""" if user exist """
|
||||
|
||||
|
||||
Reference in New Issue
Block a user