2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-09-27 08:02:44 +00:00
|
|
|
import abc
|
2019-09-17 04:34:47 +00:00
|
|
|
from rest_framework.generics import (
|
2020-08-16 15:08:58 +00:00
|
|
|
ListAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
)
|
2020-08-16 15:08:58 +00:00
|
|
|
from rest_framework.response import Response
|
2020-09-27 08:02:44 +00:00
|
|
|
from rest_framework.request import Request
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.api.mixin import SerializeToTreeNodeMixin
|
2019-09-17 04:34:47 +00:00
|
|
|
from common.utils import get_logger
|
2021-02-05 05:29:29 +00:00
|
|
|
from .mixin import RoleAdminMixin, RoleUserMixin
|
|
|
|
from perms.hands import User
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
from perms.utils.asset.user_permission import UserGrantedNodesQueryUtils
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
2020-08-16 15:08:58 +00:00
|
|
|
'UserGrantedNodesForAdminApi',
|
|
|
|
'MyGrantedNodesApi',
|
|
|
|
'MyGrantedNodesAsTreeApi',
|
|
|
|
'UserGrantedNodeChildrenForAdminApi',
|
|
|
|
'MyGrantedNodeChildrenApi',
|
|
|
|
'UserGrantedNodeChildrenAsTreeForAdminApi',
|
|
|
|
'MyGrantedNodeChildrenAsTreeApi',
|
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
|
|
|
|
|
|
|
# ------------------------------------------
|
|
|
|
# 最终的 api
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserGrantedNodeChildrenForAdminApi(RoleAdminMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenApi):
|
2019-09-17 04:34:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class MyGrantedNodeChildrenApi(RoleUserMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserGrantedNodeChildrenAsTreeForAdminApi(RoleAdminMixin, 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
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class MyGrantedNodeChildrenAsTreeApi(RoleUserMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenAsTreeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserGrantedNodesForAdminApi(RoleAdminMixin, UserGrantedNodesMixin, BaseGrantedNodeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class MyGrantedNodesApi(RoleUserMixin, UserGrantedNodesMixin, BaseGrantedNodeApi):
|
2019-09-17 04:34:47 +00:00
|
|
|
pass
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class MyGrantedNodesAsTreeApi(RoleUserMixin, UserGrantedNodesMixin, BaseGrantedNodeAsTreeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# ------------------------------------------
|