From 80a70638f1b0bc26a8723dee10f46c6098467d56 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Mon, 27 Oct 2025 12:01:00 +0330 Subject: [PATCH] fix bug of filter_by_region for models that had no relation to region but has user relation --- apps/authentication/mixins/region_filter.py | 18 ++++++++++++++---- apps/core/api.py | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/authentication/mixins/region_filter.py b/apps/authentication/mixins/region_filter.py index b5c07ef..197157b 100644 --- a/apps/authentication/mixins/region_filter.py +++ b/apps/authentication/mixins/region_filter.py @@ -17,22 +17,32 @@ class RegionFilterMixin: organization = get_organization_by_user(self.request.user) # noqa if city_id: - queryset = queryset.filter(city_id=id(city_id)) + if hasattr(queryset.model, 'city_id'): + queryset = queryset.filter(city_id=id(city_id)) + elif hasattr(queryset.model, 'user'): + queryset = queryset.filter(user__city=organization.city) elif province_id: if hasattr(queryset.model, 'province_id'): queryset = queryset.filter(province_id=id(province_id)) + elif hasattr(queryset.model, 'user'): + queryset = queryset.filter(user__province=organization.city) # filter by organization type region if org: - scope = organization.activity_fields - + scope = organization.field_of_activity + print(scope) if scope == 'CI': - queryset = queryset.filter(city=organization.city) + if hasattr(queryset.model, 'city_id'): + queryset = queryset.filter(city=organization.city) + elif hasattr(queryset.model, 'user'): + queryset = queryset.filter(user__city=organization.city) elif scope == 'PR': if hasattr(queryset.model, 'province_id'): queryset = queryset.filter(province=organization.province) + elif hasattr(queryset.model, 'user'): + queryset = queryset.filter(user__province=organization.city) # if organization is admin of system elif scope == 'CO': diff --git a/apps/core/api.py b/apps/core/api.py index 5144b6c..ec28883 100644 --- a/apps/core/api.py +++ b/apps/core/api.py @@ -19,6 +19,7 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet): if self.request.method.lower() == 'get' and not self.kwargs.get('pk'): queryset = self.filter_by_region(queryset, org=True) + print(queryset) if not user_relation.first().role.type.key == 'ADM': model_name = queryset.model.__name__.lower()