diff --git a/apps/authentication/api/v1/api.py b/apps/authentication/api/v1/api.py index ae4352d..9a42096 100644 --- a/apps/authentication/api/v1/api.py +++ b/apps/authentication/api/v1/api.py @@ -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) diff --git a/apps/authentication/api/v1/serializers/serializer.py b/apps/authentication/api/v1/serializers/serializer.py index f31c688..1428157 100644 --- a/apps/authentication/api/v1/serializers/serializer.py +++ b/apps/authentication/api/v1/serializers/serializer.py @@ -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) diff --git a/apps/authentication/exceptions.py b/apps/authentication/exceptions.py index 73f976c..e703830 100644 --- a/apps/authentication/exceptions.py +++ b/apps/authentication/exceptions.py @@ -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 """ diff --git a/apps/core/api.py b/apps/core/api.py index 501cff3..76ece9b 100644 --- a/apps/core/api.py +++ b/apps/core/api.py @@ -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 diff --git a/logs/django_requests.log b/logs/django_requests.log index 58edc77..c681011 100644 --- a/logs/django_requests.log +++ b/logs/django_requests.log @@ -312,3 +312,13 @@ django.core.exceptions.FieldError: Unsupported lookup 'name' for ForeignKey or j [2025-10-28 13:50:37,425] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader [2025-10-28 13:53:02,285] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\api.py changed, reloading. [2025-10-28 13:53:05,015] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 13:54:21,998] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\serializers\serializer.py changed, reloading. +[2025-10-28 13:54:25,611] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 13:54:54,355] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\serializers\serializer.py changed, reloading. +[2025-10-28 13:54:58,709] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 14:28:42,019] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\exceptions.py changed, reloading. +[2025-10-28 14:28:44,198] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 14:28:53,263] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\serializers\serializer.py changed, reloading. +[2025-10-28 14:28:55,553] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 14:35:00,268] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading. +[2025-10-28 14:35:02,391] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader