mirror of https://github.com/jumpserver/jumpserver
perf: 优化查询 acls
parent
1b750cf51d
commit
53c3c90e2d
|
@ -110,11 +110,11 @@ class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
|
||||||
@classmethod
|
@classmethod
|
||||||
def filter_queryset(cls, user=None, asset=None, account=None, account_username=None, **kwargs):
|
def filter_queryset(cls, user=None, asset=None, account=None, account_username=None, **kwargs):
|
||||||
queryset = cls.objects.all()
|
queryset = cls.objects.all()
|
||||||
org_id = None
|
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
q = cls.users.get_filter_q(user)
|
q = cls.users.get_filter_q(user)
|
||||||
queryset = queryset.filter(q)
|
queryset = queryset.filter(q)
|
||||||
|
|
||||||
if asset:
|
if asset:
|
||||||
org_id = asset.org_id
|
org_id = asset.org_id
|
||||||
with tmp_to_org(org_id):
|
with tmp_to_org(org_id):
|
||||||
|
@ -127,8 +127,6 @@ class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
|
||||||
models.Q(accounts__contains='*') | \
|
models.Q(accounts__contains='*') | \
|
||||||
models.Q(accounts__contains='@ALL')
|
models.Q(accounts__contains='@ALL')
|
||||||
queryset = queryset.filter(q)
|
queryset = queryset.filter(q)
|
||||||
if org_id:
|
|
||||||
kwargs['org_id'] = org_id
|
|
||||||
if kwargs:
|
if kwargs:
|
||||||
queryset = queryset.filter(**kwargs)
|
queryset = queryset.filter(**kwargs)
|
||||||
return queryset.valid().distinct()
|
return queryset.valid().distinct()
|
||||||
|
|
|
@ -90,8 +90,10 @@ def create_app_nodes(apps, org_id):
|
||||||
next_value = max([int(k[1]) for k in node_key_split]) + 1
|
next_value = max([int(k[1]) for k in node_key_split]) + 1
|
||||||
parent_key = node_key_split[0][0]
|
parent_key = node_key_split[0][0]
|
||||||
else:
|
else:
|
||||||
root_node = node_model.objects.filter(org_id=org_id)\
|
root_node = node_model.objects.filter(org_id=org_id) \
|
||||||
.filter(parent_key='', key__regex=r'^[0-9]+$').exclude(key__startswith='-').first()
|
.filter(parent_key='', key__regex=r'^[0-9]+$') \
|
||||||
|
.exclude(key__startswith='-') \
|
||||||
|
.first()
|
||||||
if not root_node:
|
if not root_node:
|
||||||
return
|
return
|
||||||
parent_key = root_node.key
|
parent_key = root_node.key
|
||||||
|
|
|
@ -295,7 +295,13 @@ class RelatedManager:
|
||||||
self.instance.__dict__[self.field.name] = value
|
self.instance.__dict__[self.field.name] = value
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_filter_q(cls, value, to_model):
|
def _get_filter_q(cls, value, to_model):
|
||||||
|
"""
|
||||||
|
这个是 instance 去查找 to_model 的 queryset 的 Q
|
||||||
|
:param value:
|
||||||
|
:param to_model:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if not value or not isinstance(value, dict):
|
if not value or not isinstance(value, dict):
|
||||||
return Q()
|
return Q()
|
||||||
|
|
||||||
|
@ -314,7 +320,7 @@ class RelatedManager:
|
||||||
queryset = to_model.get_queryset()
|
queryset = to_model.get_queryset()
|
||||||
else:
|
else:
|
||||||
queryset = to_model.objects.all()
|
queryset = to_model.objects.all()
|
||||||
q = cls.get_filter_q(value, to_model)
|
q = cls._get_filter_q(value, to_model)
|
||||||
return queryset.filter(q).distinct()
|
return queryset.filter(q).distinct()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -442,9 +448,7 @@ class JSONManyToManyDescriptor:
|
||||||
# 自定义的情况:比如 nodes, category
|
# 自定义的情况:比如 nodes, category
|
||||||
res = True
|
res = True
|
||||||
to_model = apps.get_model(self.field.to)
|
to_model = apps.get_model(self.field.to)
|
||||||
src_model = self.field.model
|
custom_attr_filter = getattr(to_model, "get_json_filter_attr_q", None)
|
||||||
field_name = self.field.name
|
|
||||||
custom_attr_filter = getattr(src_model, "get_filter_{}_attr_q".format(field_name), None)
|
|
||||||
|
|
||||||
custom_q = Q()
|
custom_q = Q()
|
||||||
for rule in attr_rules:
|
for rule in attr_rules:
|
||||||
|
@ -513,7 +517,10 @@ class JSONManyToManyDescriptor:
|
||||||
model_cls = self.field.model
|
model_cls = self.field.model
|
||||||
field_name = self.field.column
|
field_name = self.field.column
|
||||||
q = Q(**{f'{field_name}__type': 'all'}) | \
|
q = Q(**{f'{field_name}__type': 'all'}) | \
|
||||||
Q(**{f'{field_name}__type': 'ids', f'{field_name}__ids__contains': [str(instance.id)]})
|
Q(**{
|
||||||
|
f'{field_name}__type': 'ids',
|
||||||
|
f'{field_name}__ids__contains': [str(instance.id)]
|
||||||
|
})
|
||||||
queryset_id_attrs = model_cls.objects \
|
queryset_id_attrs = model_cls.objects \
|
||||||
.filter(**{'{}__type'.format(field_name): 'attrs'}) \
|
.filter(**{'{}__type'.format(field_name): 'attrs'}) \
|
||||||
.values_list('id', '{}__attrs'.format(field_name))
|
.values_list('id', '{}__attrs'.format(field_name))
|
||||||
|
|
Loading…
Reference in New Issue