show_my_org in organizations list - validation of org national_unique_id
This commit is contained in:
@@ -319,7 +319,7 @@ class OrganizationViewSet(BaseViewSet, SoftDeleteMixin, ModelViewSet, DynamicSea
|
||||
""" list of organizations by province """
|
||||
|
||||
if 'province' in request.GET.keys():
|
||||
queryset = self.get_queryset().filter(province=int(request.GET['province']))
|
||||
queryset = self.get_queryset(show_my_org=True).filter(province=int(request.GET['province']))
|
||||
else:
|
||||
queryset = self.get_queryset().filter(province=request.user.province)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ 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
|
||||
from apps.authentication.exceptions import UserExistException, OrganizationNationalUniqueIDException
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
@@ -224,6 +224,17 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
||||
]
|
||||
extra_kwargs = {}
|
||||
|
||||
def validate(self, attrs):
|
||||
national_unique_id = self.context['request'].data['organization']['national_unique_id']
|
||||
|
||||
if not self.instance:
|
||||
if self.Meta.model.objects.filter(national_unique_id=national_unique_id).exists():
|
||||
raise OrganizationNationalUniqueIDException()
|
||||
if self.instance:
|
||||
if self.instance.national_unique_id != national_unique_id:
|
||||
if self.Meta.model.objects.filter(national_unique_id=national_unique_id).exists():
|
||||
raise OrganizationNationalUniqueIDException()
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output """
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
@@ -19,6 +19,14 @@ class OrganizationBankAccountException(APIException):
|
||||
default_code = "برای این سازمان حساب بانکی تعریف نشده است" # noqa
|
||||
|
||||
|
||||
class OrganizationNationalUniqueIDException(APIException):
|
||||
""" if organization unique id exist """
|
||||
|
||||
status_code = status.HTTP_403_FORBIDDEN
|
||||
default_detail = _('این شناسه ملی قبلا ثبت شده است') # noqa
|
||||
default_code = 'organization_unique_id_exist'
|
||||
|
||||
|
||||
class UserExistException(APIException):
|
||||
""" if user exist """
|
||||
|
||||
@@ -26,6 +34,7 @@ class UserExistException(APIException):
|
||||
default_detail = _('کاربری با این شماره موبایل یا با این نام کاربری از قبل وجود دارد') # noqa
|
||||
default_code = 'user_does_not_exist'
|
||||
|
||||
|
||||
class AdminDeleteException(APIException):
|
||||
""" admin user can not be deleted """
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
|
||||
It applies region-based filtering automatically to GET (list) requests.
|
||||
"""
|
||||
|
||||
def get_queryset(self):
|
||||
def get_queryset(self, show_my_org: bool = None):
|
||||
queryset = super().get_queryset()
|
||||
request = self.request
|
||||
user = request.user
|
||||
@@ -29,6 +29,10 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
|
||||
# get all child orgs
|
||||
child_orgs = get_all_org_child(org)
|
||||
|
||||
# if show_my_org is True, add current org to queryset
|
||||
if show_my_org:
|
||||
child_orgs.append(org)
|
||||
|
||||
model_name = queryset.model.__name__.lower()
|
||||
|
||||
if model_name == 'userrelations': # noqa
|
||||
|
||||
Reference in New Issue
Block a user