2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-09-27 08:02:44 +00:00
|
|
|
import abc
|
2022-12-15 08:02:34 +00:00
|
|
|
|
2022-12-01 04:02:20 +00:00
|
|
|
from rest_framework.generics import ListAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
from assets.models import Node
|
2022-12-15 08:02:34 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
2022-12-21 09:36:44 +00:00
|
|
|
from perms.utils import UserPermNodeUtil
|
2022-12-07 10:38:03 +00:00
|
|
|
from .mixin import SelfOrPKUserMixin
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
2022-12-07 10:38:03 +00:00
|
|
|
'UserAllPermedNodesApi',
|
2022-12-06 11:53:36 +00:00
|
|
|
'UserPermedNodeChildrenApi',
|
2019-09-17 04:34:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class BaseUserPermedNodesApi(SelfOrPKUserMixin, ListAPIView):
|
2022-12-15 08:02:34 +00:00
|
|
|
serializer_class = serializers.NodePermedSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
if getattr(self, 'swagger_fake_view', False):
|
|
|
|
return Node.objects.none()
|
|
|
|
return self.get_nodes()
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
@abc.abstractmethod
|
2020-08-16 15:08:58 +00:00
|
|
|
def get_nodes(self):
|
2022-12-07 10:38:03 +00:00
|
|
|
return []
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
@lazyproperty
|
|
|
|
def query_node_util(self):
|
2022-12-21 09:36:44 +00:00
|
|
|
return UserPermNodeUtil(self.user)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserAllPermedNodesApi(BaseUserPermedNodesApi):
|
2022-12-01 04:02:20 +00:00
|
|
|
""" 用户授权的节点 """
|
2022-12-15 08:02:34 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
def get_nodes(self):
|
|
|
|
return self.query_node_util.get_whole_tree_nodes()
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserPermedNodeChildrenApi(BaseUserPermedNodesApi):
|
|
|
|
""" 用户授权的节点下的子节点 """
|
2022-12-15 08:02:34 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
def get_nodes(self):
|
|
|
|
key = self.request.query_params.get('key')
|
|
|
|
nodes = self.query_node_util.get_node_children(key)
|
|
|
|
return nodes
|