2021-08-17 10:50:15 +00:00
|
|
|
from django_filters import rest_framework as filters
|
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
|
2022-02-17 12:13:31 +00:00
|
|
|
from rbac.models import Role
|
2021-08-17 10:50:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserFilter(BaseFilterSet):
|
2022-02-17 12:13:31 +00:00
|
|
|
system_roles = filters.ModelChoiceFilter(
|
|
|
|
queryset=Role.objects.filter(scope='system'), method='filter_system_roles'
|
|
|
|
)
|
|
|
|
org_roles = filters.ModelChoiceFilter(
|
|
|
|
queryset=Role.objects.filter(scope='org'), method='filter_org_roles'
|
|
|
|
)
|
2021-08-17 10:50:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
2022-02-17 12:13:31 +00:00
|
|
|
'id', 'username', 'email', 'name', 'source',
|
2022-06-30 10:21:12 +00:00
|
|
|
'org_roles', 'system_roles', 'is_active',
|
2021-08-17 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def filter_system_roles(queryset, name, value):
|
|
|
|
queryset = queryset.prefetch_related('role_bindings')\
|
|
|
|
.filter(role_bindings__role_id=value.id)\
|
|
|
|
.distinct()
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def filter_org_roles(queryset, name, value):
|
|
|
|
queryset = queryset.prefetch_related('role_bindings') \
|
|
|
|
.filter(role_bindings__role_id=value.id) \
|
|
|
|
.distinct()
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|