2020-10-22 09:05:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-03-29 09:22:59 +00:00
|
|
|
from typing import Callable
|
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
from rest_framework.generics import ListAPIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2021-01-03 21:27:03 +00:00
|
|
|
from common.mixins.api import CommonApiMixin
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.tree import TreeNodeSerializer
|
2020-10-22 09:05:47 +00:00
|
|
|
from perms import serializers
|
2022-03-29 09:22:59 +00:00
|
|
|
from perms.tree.app import GrantedAppTreeUtil
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.utils.application.user_permission import (
|
2020-10-22 09:05:47 +00:00
|
|
|
get_user_granted_all_applications
|
|
|
|
)
|
2022-03-29 09:22:59 +00:00
|
|
|
from .mixin import AppRoleAdminMixin, AppRoleUserMixin
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserAllGrantedApplicationsApi',
|
|
|
|
'MyAllGrantedApplicationsApi',
|
|
|
|
'UserAllGrantedApplicationsAsTreeApi',
|
|
|
|
'MyAllGrantedApplicationsAsTreeApi',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
class AllGrantedApplicationsApi(CommonApiMixin, ListAPIView):
|
2021-09-09 08:04:54 +00:00
|
|
|
only_fields = serializers.AppGrantedSerializer.Meta.only_fields
|
|
|
|
serializer_class = serializers.AppGrantedSerializer
|
2021-07-21 09:26:51 +00:00
|
|
|
filterset_fields = {
|
|
|
|
'id': ['exact'],
|
|
|
|
'name': ['exact'],
|
|
|
|
'category': ['exact'],
|
|
|
|
'type': ['exact', 'in'],
|
|
|
|
'comment': ['exact'],
|
|
|
|
}
|
2020-10-22 09:05:47 +00:00
|
|
|
search_fields = ['name', 'comment']
|
|
|
|
user: None
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = get_user_granted_all_applications(self.user)
|
|
|
|
return queryset.only(*self.only_fields)
|
|
|
|
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
class UserAllGrantedApplicationsApi(AppRoleAdminMixin, AllGrantedApplicationsApi):
|
2020-10-30 03:50:37 +00:00
|
|
|
pass
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
class MyAllGrantedApplicationsApi(AppRoleUserMixin, AllGrantedApplicationsApi):
|
2020-10-22 09:05:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
class ApplicationsAsTreeMixin:
|
2020-10-22 09:05:47 +00:00
|
|
|
"""
|
|
|
|
将应用序列化成树的结构返回
|
|
|
|
"""
|
2021-07-27 08:06:00 +00:00
|
|
|
serializer_class = TreeNodeSerializer
|
2021-08-30 08:48:46 +00:00
|
|
|
user: None
|
2022-03-29 09:22:59 +00:00
|
|
|
filter_queryset: Callable
|
|
|
|
get_queryset: Callable
|
|
|
|
get_serializer: Callable
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
2021-08-30 08:48:46 +00:00
|
|
|
tree_id = request.query_params.get('tree_id', None)
|
|
|
|
parent_info = request.query_params.get('parentInfo', None)
|
2020-10-22 09:05:47 +00:00
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
2022-03-29 09:22:59 +00:00
|
|
|
util = GrantedAppTreeUtil()
|
|
|
|
|
|
|
|
if not tree_id:
|
|
|
|
tree_nodes = util.create_tree_nodes(queryset)
|
|
|
|
else:
|
|
|
|
tree_nodes = util.get_children_nodes(tree_id, parent_info, self.user)
|
2021-07-27 08:06:00 +00:00
|
|
|
serializer = self.get_serializer(tree_nodes, many=True)
|
|
|
|
return Response(data=serializer.data)
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserAllGrantedApplicationsAsTreeApi(ApplicationsAsTreeMixin, UserAllGrantedApplicationsApi):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MyAllGrantedApplicationsAsTreeApi(ApplicationsAsTreeMixin, MyAllGrantedApplicationsApi):
|
|
|
|
pass
|