[Update] 优化用户授权的 RemoteApp API 过滤逻辑

pull/3267/head
BaiJiangJie 5 years ago
parent 3f9e2f0af7
commit e805e9cede

@ -14,7 +14,6 @@ from ..utils import (
parse_remote_app_to_tree_node,
)
from ..hands import User, RemoteApp, RemoteAppSerializer, UserGroup, SystemUser
from ..mixins import RemoteAppFilterMixin
from .mixin import UserPermissionMixin
from .. import serializers
@ -26,10 +25,11 @@ __all__ = [
]
class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView):
class UserGrantedRemoteAppsApi(ListAPIView):
permission_classes = (IsOrgAdminOrAppUser,)
serializer_class = RemoteAppSerializer
filter_fields = ['id']
filter_fields = ['name', 'id']
search_fields = ['name']
def get_object(self):
user_id = self.kwargs.get('pk', '')
@ -54,15 +54,14 @@ class UserGrantedRemoteAppsAsTreeApi(UserGrantedRemoteAppsApi):
serializer_class = TreeNodeSerializer
permission_classes = (IsOrgAdminOrAppUser,)
def get_serializer(self, *args, **kwargs):
def get_serializer(self, remote_apps=None, *args, **kwargs):
only_remote_app = self.request.query_params.get('only', '0') == '1'
tree_root = None
data = []
if not only_remote_app:
tree_root = construct_remote_apps_tree_root()
data.append(tree_root)
queryset = super().get_queryset()
for remote_app in queryset:
for remote_app in remote_apps:
node = parse_remote_app_to_tree_node(tree_root, remote_app)
data.append(node)
data.sort()

@ -4,7 +4,7 @@
from orgs.utils import set_to_root_org
__all__ = [
'AssetsFilterMixin', 'RemoteAppFilterMixin', 'ChangeOrgIfNeedMixin',
'AssetsFilterMixin', 'ChangeOrgIfNeedMixin',
]
@ -68,41 +68,6 @@ class AssetsFilterMixin(object):
return _queryset
class RemoteAppFilterMixin(object):
"""
对RemoteApp进行过滤(查询排序)
"""
def filter_queryset(self, queryset):
queryset = self.search_remote_apps(queryset)
queryset = self.sort_remote_apps(queryset)
return queryset
def search_remote_apps(self, queryset):
value = self.request.query_params.get('search')
if not value:
return queryset
queryset = [
remote_app for remote_app in queryset if value in remote_app.name
]
return queryset
def sort_remote_apps(self, queryset):
order_by = self.request.query_params.get('order')
if not order_by:
order_by = 'name'
if order_by.startswith('-'):
order_by = order_by.lstrip('-')
reverse = True
else:
reverse = False
queryset = sorted(
queryset, key=lambda x: getattr(x, order_by), reverse=reverse
)
return queryset
class ChangeOrgIfNeedMixin(object):
@staticmethod

Loading…
Cancel
Save