Browse Source

perf: user filter (#9413)

Co-authored-by: feng <1304903146@qq.com>
pull/9415/head
fit2bot 2 years ago committed by GitHub
parent
commit
be151523f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      apps/users/filters.py

24
apps/users/filters.py

@ -6,12 +6,8 @@ from rbac.models import Role
class UserFilter(BaseFilterSet):
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'
)
system_roles = filters.CharFilter(method='filter_system_roles')
org_roles = filters.CharFilter(method='filter_org_roles')
class Meta:
model = User
@ -22,16 +18,18 @@ class UserFilter(BaseFilterSet):
@staticmethod
def filter_system_roles(queryset, name, value):
queryset = queryset.prefetch_related('role_bindings')\
.filter(role_bindings__role_id=value.id)\
.distinct()
queryset = queryset.prefetch_related('role_bindings') \
.filter(
role_bindings__role__name=value,
role_bindings__role__scope='system'
).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()
.filter(
role_bindings__role__name=value,
role_bindings__role__scope='org'
).distinct()
return queryset

Loading…
Cancel
Save