filter all apis of organization city & province - cant remove own user or organoization

This commit is contained in:
2025-10-27 11:21:29 +03:30
parent 96cbd5a475
commit f567110286
9 changed files with 172 additions and 95 deletions

View File

View File

@@ -0,0 +1,41 @@
import typing
from common.helpers import get_organization_by_user
class RegionFilterMixin:
"""
Filters queryset automatically based on:
- Query params (city / province)
- Or the organization type of the current user
"""
def filter_by_region(self, queryset, org: bool = None) -> typing.Any:
request = self.request # noqa
city_id = int(request.query_params.get('city_id'))
province_id = int(request.query_params.get('province_id'))
organization = get_organization_by_user(self.request.user) # noqa
if city_id:
queryset = queryset.filter(city_id=city_id)
elif province_id:
if hasattr(queryset.model, 'province_id'):
queryset = queryset.filter(province_id=province_id)
# filter by organization type region
if org:
scope = organization.activity_fields
if scope == 'CI':
queryset = queryset.filter(city=organization.city)
elif scope == 'PR':
if hasattr(queryset.model, 'province_id'):
queryset = queryset.filter(province=organization.province)
# if organization is admin of system
elif scope == 'CO':
return queryset
return queryset