Browse Source

Merge pull request #10711 from jumpserver/pr@dev@perf_json_field_select

perf: 优化查询 acls
pull/10715/head
老广 1 year ago committed by GitHub
parent
commit
29ebdb03e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      apps/acls/models/base.py
  2. 6
      apps/assets/migrations/0098_auto_20220430_2126.py
  3. 10
      apps/common/db/fields.py

4
apps/acls/models/base.py

@ -110,11 +110,11 @@ class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
@classmethod
def filter_queryset(cls, user=None, asset=None, account=None, account_username=None, **kwargs):
queryset = cls.objects.all()
org_id = None
if user:
q = cls.users.get_filter_q(user)
queryset = queryset.filter(q)
if asset:
org_id = asset.org_id
with tmp_to_org(org_id):
@ -127,8 +127,6 @@ class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
models.Q(accounts__contains='*') | \
models.Q(accounts__contains='@ALL')
queryset = queryset.filter(q)
if org_id:
kwargs['org_id'] = org_id
if kwargs:
queryset = queryset.filter(**kwargs)
return queryset.valid().distinct()

6
apps/assets/migrations/0098_auto_20220430_2126.py

@ -90,8 +90,10 @@ def create_app_nodes(apps, org_id):
next_value = max([int(k[1]) for k in node_key_split]) + 1
parent_key = node_key_split[0][0]
else:
root_node = node_model.objects.filter(org_id=org_id)\
.filter(parent_key='', key__regex=r'^[0-9]+$').exclude(key__startswith='-').first()
root_node = node_model.objects.filter(org_id=org_id) \
.filter(parent_key='', key__regex=r'^[0-9]+$') \
.exclude(key__startswith='-') \
.first()
if not root_node:
return
parent_key = root_node.key

10
apps/common/db/fields.py

@ -295,7 +295,13 @@ class RelatedManager:
self.instance.__dict__[self.field.name] = value
@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):
return Q()
@ -314,7 +320,7 @@ class RelatedManager:
queryset = to_model.get_queryset()
else:
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()
@staticmethod

Loading…
Cancel
Save