perf: 优化 manager 和 acl 的 组织

pull/10681/head
ibuler 2023-06-12 16:00:14 +08:00
parent 24272d3162
commit 5c2b54ad3b
5 changed files with 20 additions and 4 deletions

View File

@ -1,6 +1,7 @@
from django_filters import rest_framework as drf_filters
from common.api import JMSBulkModelViewSet
from orgs.utils import tmp_to_root_org
from .common import ACLUserFilterMixin
from .. import serializers
from ..models import ConnectMethodACL
@ -21,3 +22,8 @@ class ConnectMethodACLViewSet(JMSBulkModelViewSet):
filterset_class = ConnectMethodFilter
search_fields = ('name',)
serializer_class = serializers.ConnectMethodACLSerializer
def filter_queryset(self, queryset):
with tmp_to_root_org():
return super().filter_queryset(queryset)

View File

@ -1,4 +1,6 @@
from common.api import JMSBulkModelViewSet
from orgs.utils import tmp_to_root_org
from .common import ACLUserFilterMixin
from .. import serializers
from ..models import LoginACL
@ -17,3 +19,8 @@ class LoginACLViewSet(JMSBulkModelViewSet):
filterset_class = LoginACLFilter
search_fields = ('name',)
serializer_class = serializers.LoginACLSerializer
def filter_queryset(self, queryset):
with tmp_to_root_org():
return super().filter_queryset(queryset)

View File

@ -12,6 +12,8 @@ __all__ = [
'BaseACL', 'UserBaseACL', 'UserAssetAccountBaseACL',
]
from orgs.utils import tmp_to_root_org
class ActionChoices(models.TextChoices):
reject = 'reject', _('Reject')
@ -88,7 +90,8 @@ class UserBaseACL(BaseACL):
@classmethod
def get_user_acls(cls, user):
queryset = cls.objects.all()
q = cls.users.get_filter_q(user)
with tmp_to_root_org():
q = cls.users.get_filter_q(user)
queryset = queryset.filter(q)
return queryset.filter(is_active=True).distinct()
@ -99,8 +102,6 @@ class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
accounts = models.JSONField(default=list, verbose_name=_("Accounts"))
objects = OrgManager.from_queryset(BaseACLQuerySet)()
objects = OrgManager.from_queryset(BaseACLQuerySet)()
class Meta(UserBaseACL.Meta):
unique_together = [('name', 'org_id')]
abstract = True

View File

@ -369,6 +369,7 @@ class AuthACLMixin:
logger.debug('Login confirm acl id: {}'.format(acl_id))
if not acl_id:
return
acl = LoginACL.get_user_acls(user).filter(id=acl_id).first()
if not acl:
return

View File

@ -392,7 +392,8 @@ class RelatedManager:
return self.filter_queryset_by_model(value, to_model)
def get_attr_q(self):
q = self._get_filter_attrs_q(self.value, apps.get_model(self.field.to))
to_model = apps.get_model(self.field.to)
q = self._get_filter_attrs_q(self.value, to_model)
return q
def all(self):