mirror of https://github.com/jumpserver/jumpserver
perf: 修改 m2m json filter (#12087)
* perf: 修改 m2m json filter * perf: 修复 json 过滤问题 --------- Co-authored-by: ibuler <ibuler@qq.com>pull/12092/head
parent
f8a4a0e108
commit
0f9223331c
|
@ -131,8 +131,16 @@ class JSONFilterMixin:
|
||||||
value = [value]
|
value = [value]
|
||||||
if name == 'nodes':
|
if name == 'nodes':
|
||||||
nodes = Node.objects.filter(id__in=value)
|
nodes = Node.objects.filter(id__in=value)
|
||||||
children = Node.get_nodes_all_children(nodes, with_self=True).values_list('id', flat=True)
|
if match == 'm2m_all':
|
||||||
return Q(nodes__in=children)
|
assets = Asset.objects.all()
|
||||||
|
for n in nodes:
|
||||||
|
children_pattern = Node.get_node_all_children_key_pattern(n.key)
|
||||||
|
assets = assets.filter(nodes__key__regex=children_pattern)
|
||||||
|
q = Q(id__in=assets.values_list('id', flat=True))
|
||||||
|
return q
|
||||||
|
else:
|
||||||
|
children = Node.get_nodes_all_children(nodes, with_self=True).values_list('id', flat=True)
|
||||||
|
return Q(nodes__in=children)
|
||||||
elif name == 'category':
|
elif name == 'category':
|
||||||
return Q(platform__category__in=value)
|
return Q(platform__category__in=value)
|
||||||
elif name == 'type':
|
elif name == 'type':
|
||||||
|
|
|
@ -12,6 +12,7 @@ from django.contrib.auth.hashers import check_password
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Count
|
||||||
from django.shortcuts import reverse
|
from django.shortcuts import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
@ -708,29 +709,29 @@ class MFAMixin:
|
||||||
|
|
||||||
|
|
||||||
class JSONFilterMixin:
|
class JSONFilterMixin:
|
||||||
"""
|
|
||||||
users = JSONManyToManyField('users.User', blank=True, null=True)
|
|
||||||
"""
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_json_filter_attr_q(name, value, match):
|
def get_json_filter_attr_q(name, value, match):
|
||||||
from rbac.models import RoleBinding
|
from rbac.models import RoleBinding
|
||||||
from orgs.utils import current_org
|
from orgs.utils import current_org
|
||||||
|
|
||||||
|
kwargs = {}
|
||||||
if name == 'system_roles':
|
if name == 'system_roles':
|
||||||
user_id = RoleBinding.objects \
|
kwargs['scope'] = 'system'
|
||||||
.filter(role__in=value, scope='system') \
|
|
||||||
.values_list('user_id', flat=True)
|
|
||||||
return models.Q(id__in=user_id)
|
|
||||||
elif name == 'org_roles':
|
elif name == 'org_roles':
|
||||||
kwargs = dict(role__in=value, scope='org')
|
kwargs['scope'] = 'org'
|
||||||
if not current_org.is_root():
|
if not current_org.is_root():
|
||||||
kwargs['org_id'] = current_org.id
|
kwargs['org_id'] = current_org.id
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
user_id = RoleBinding.objects.filter(**kwargs) \
|
bindings = RoleBinding.objects.filter(**kwargs, role__in=value)
|
||||||
.values_list('user_id', flat=True)
|
if match == 'm2m_all':
|
||||||
return models.Q(id__in=user_id)
|
user_id = bindings.values('user_id').annotate(count=Count('user_id')) \
|
||||||
return None
|
.filter(count=len(value)).values_list('user_id', flat=True)
|
||||||
|
else:
|
||||||
|
user_id = bindings.values_list('user_id', flat=True)
|
||||||
|
|
||||||
|
return models.Q(id__in=user_id)
|
||||||
|
|
||||||
|
|
||||||
class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, JSONFilterMixin, AbstractUser):
|
class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, JSONFilterMixin, AbstractUser):
|
||||||
|
|
Loading…
Reference in New Issue