2021-08-17 10:50:15 +00:00
|
|
|
from django_filters import rest_framework as filters
|
|
|
|
from django.db.models import Q
|
2020-07-06 03:14:20 +00:00
|
|
|
from rest_framework.compat import coreapi, coreschema
|
2021-08-17 10:50:15 +00:00
|
|
|
from rest_framework.filters import BaseFilterBackend
|
2020-07-06 03:14:20 +00:00
|
|
|
|
2021-08-17 10:50:15 +00:00
|
|
|
from common.drf.filters import BaseFilterSet
|
2020-07-09 06:06:06 +00:00
|
|
|
from users.models.user import User
|
2021-08-17 10:50:15 +00:00
|
|
|
from users.const import SystemOrOrgRole
|
2020-07-06 03:14:20 +00:00
|
|
|
from orgs.utils import current_org
|
|
|
|
|
|
|
|
|
2021-08-17 10:50:15 +00:00
|
|
|
class OrgRoleUserFilterBackend(BaseFilterBackend):
|
2020-07-06 03:14:20 +00:00
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
org_role = request.query_params.get('org_role')
|
|
|
|
if not org_role:
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
if org_role == 'admins':
|
2020-07-28 09:53:01 +00:00
|
|
|
return queryset & (current_org.admins | User.objects.filter(role=User.ROLE.ADMIN))
|
2020-07-06 03:14:20 +00:00
|
|
|
elif org_role == 'auditors':
|
2020-07-20 02:42:22 +00:00
|
|
|
return queryset & current_org.auditors
|
2020-07-06 03:14:20 +00:00
|
|
|
elif org_role == 'users':
|
2020-07-20 02:42:22 +00:00
|
|
|
return queryset & current_org.users
|
2020-07-06 03:14:20 +00:00
|
|
|
elif org_role == 'members':
|
2020-07-20 02:42:22 +00:00
|
|
|
return queryset & current_org.get_members()
|
2020-07-06 03:14:20 +00:00
|
|
|
|
|
|
|
def get_schema_fields(self, view):
|
|
|
|
return [
|
|
|
|
coreapi.Field(
|
|
|
|
name='org_role', location='query', required=False, type='string',
|
|
|
|
schema=coreschema.String(
|
|
|
|
title='Organization role users',
|
|
|
|
description='Organization role users can be {admins|auditors|users|members}'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
2021-08-17 10:50:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserFilter(BaseFilterSet):
|
|
|
|
system_or_org_role = filters.ChoiceFilter(choices=SystemOrOrgRole.choices, method='filter_system_or_org_role')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
|
|
|
'id', 'username', 'email', 'name', 'source', 'system_or_org_role'
|
|
|
|
)
|
|
|
|
|
|
|
|
def filter_system_or_org_role(self, queryset, name, value):
|
|
|
|
value = value.split('_')
|
|
|
|
if len(value) == 1:
|
|
|
|
role_type, value = None, value[0]
|
|
|
|
else:
|
|
|
|
role_type, value = value
|
|
|
|
value = value.title()
|
|
|
|
system_queries = Q(role=value)
|
|
|
|
org_queries = Q(m2m_org_members__role=value, m2m_org_members__org_id=current_org.id)
|
|
|
|
if not role_type:
|
|
|
|
queries = system_queries | org_queries
|
|
|
|
elif role_type == 'system':
|
|
|
|
queries = system_queries
|
|
|
|
elif role_type == 'org':
|
|
|
|
queries = org_queries
|
|
|
|
return queryset.filter(queries)
|