2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-09-27 08:02:44 +00:00
|
|
|
import abc
|
|
|
|
from rest_framework.request import Request
|
2022-12-01 04:02:20 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.generics import ListAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
2022-12-01 04:02:20 +00:00
|
|
|
from assets.api.mixin import SerializeToTreeNodeMixin
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
2022-12-01 04:02:20 +00:00
|
|
|
from perms.hands import User
|
2022-08-23 02:23:48 +00:00
|
|
|
from perms.utils.user_permission import UserGrantedNodesQueryUtils
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2022-12-01 04:02:20 +00:00
|
|
|
from .mixin import SelfOrPKUserMixin, RebuildTreeMixin
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
2022-12-06 11:53:36 +00:00
|
|
|
'UserPermedNodesApi',
|
|
|
|
'UserPermedNodesAsTreeApi',
|
|
|
|
'UserPermedNodeChildrenApi',
|
|
|
|
'UserPermedNodeChildrenAsTreeApi',
|
2020-09-27 08:02:44 +00:00
|
|
|
'BaseGrantedNodeAsTreeApi',
|
|
|
|
'UserGrantedNodesMixin',
|
2019-09-17 04:34:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class _GrantedNodeStructApi(ListAPIView, metaclass=abc.ABCMeta):
|
|
|
|
@property
|
2020-08-16 15:08:58 +00:00
|
|
|
def user(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
# 不使用 `get_queryset` 单独定义 `get_nodes` 的原因是
|
|
|
|
# `get_nodes` 返回的不一定是 `queryset`
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class NodeChildrenMixin:
|
|
|
|
def get_children(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
nodes = self.get_children()
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
|
|
class BaseGrantedNodeApi(_GrantedNodeStructApi, metaclass=abc.ABCMeta):
|
2020-08-16 15:08:58 +00:00
|
|
|
serializer_class = serializers.NodeGrantedSerializer
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
nodes = self.get_nodes()
|
|
|
|
serializer = self.get_serializer(nodes, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class BaseNodeChildrenApi(NodeChildrenMixin, BaseGrantedNodeApi, metaclass=abc.ABCMeta):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BaseGrantedNodeAsTreeApi(SerializeToTreeNodeMixin, _GrantedNodeStructApi, metaclass=abc.ABCMeta):
|
2020-09-27 11:47:13 +00:00
|
|
|
def list(self, request: Request, *args, **kwargs):
|
2020-08-16 15:08:58 +00:00
|
|
|
nodes = self.get_nodes()
|
|
|
|
nodes = self.serialize_nodes(nodes, with_asset_amount=True)
|
|
|
|
return Response(data=nodes)
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class BaseNodeChildrenAsTreeApi(NodeChildrenMixin, BaseGrantedNodeAsTreeApi, metaclass=abc.ABCMeta):
|
|
|
|
pass
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserGrantedNodeChildrenMixin:
|
2020-09-27 08:02:44 +00:00
|
|
|
user: User
|
|
|
|
request: Request
|
|
|
|
|
|
|
|
def get_children(self):
|
2020-08-16 15:08:58 +00:00
|
|
|
user = self.user
|
|
|
|
key = self.request.query_params.get('key')
|
2021-02-05 05:29:29 +00:00
|
|
|
nodes = UserGrantedNodesQueryUtils(user).get_node_children(key)
|
2020-08-16 15:08:58 +00:00
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedNodesMixin:
|
2019-09-17 04:34:47 +00:00
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
查询用户授权的所有节点 直接授权节点 + 授权资产关联的节点
|
2019-09-17 04:34:47 +00:00
|
|
|
"""
|
2020-09-27 08:02:44 +00:00
|
|
|
user: User
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
def get_nodes(self):
|
2021-02-05 05:29:29 +00:00
|
|
|
utils = UserGrantedNodesQueryUtils(self.user)
|
|
|
|
nodes = utils.get_whole_tree_nodes()
|
2020-11-17 10:27:09 +00:00
|
|
|
return nodes
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2022-12-01 04:02:20 +00:00
|
|
|
# API
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2022-12-06 11:53:36 +00:00
|
|
|
class UserPermedNodeChildrenApi(
|
2022-12-01 04:02:20 +00:00
|
|
|
SelfOrPKUserMixin,
|
|
|
|
UserGrantedNodeChildrenMixin,
|
|
|
|
BaseNodeChildrenApi
|
|
|
|
):
|
|
|
|
""" 用户授权的节点下的子节点"""
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2022-12-06 11:53:36 +00:00
|
|
|
class UserPermedNodeChildrenAsTreeApi(
|
2022-12-01 04:02:20 +00:00
|
|
|
SelfOrPKUserMixin,
|
|
|
|
RebuildTreeMixin,
|
|
|
|
UserGrantedNodeChildrenMixin,
|
|
|
|
BaseNodeChildrenAsTreeApi
|
|
|
|
):
|
|
|
|
""" 用户授权的节点下的子节点树"""
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2022-12-06 11:53:36 +00:00
|
|
|
class UserPermedNodesApi(
|
2022-12-01 04:02:20 +00:00
|
|
|
SelfOrPKUserMixin,
|
|
|
|
UserGrantedNodesMixin,
|
|
|
|
BaseGrantedNodeApi
|
|
|
|
):
|
|
|
|
""" 用户授权的节点 """
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2022-12-06 11:53:36 +00:00
|
|
|
class UserPermedNodesAsTreeApi(
|
2022-12-01 04:02:20 +00:00
|
|
|
SelfOrPKUserMixin,
|
|
|
|
RebuildTreeMixin,
|
|
|
|
UserGrantedNodesMixin,
|
|
|
|
BaseGrantedNodeAsTreeApi
|
|
|
|
):
|
|
|
|
""" 用户授权的节点树 """
|
2019-09-17 04:34:47 +00:00
|
|
|
pass
|