filter all apis of organization city & province - cant remove own user or organoization
This commit is contained in:
0
apps/authentication/mixins/__init__.py
Normal file
0
apps/authentication/mixins/__init__.py
Normal file
41
apps/authentication/mixins/region_filter.py
Normal file
41
apps/authentication/mixins/region_filter.py
Normal 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
|
||||
Reference in New Issue
Block a user