jumpserver/apps/applications/api/application.py

38 lines
1.2 KiB
Python
Raw Normal View History

2020-10-19 12:13:01 +00:00
# coding: utf-8
#
2020-10-19 12:13:01 +00:00
from orgs.mixins.api import OrgBulkModelViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
2020-10-19 12:13:01 +00:00
from common.tree import TreeNodeSerializer
2021-06-11 03:05:40 +00:00
from ..hands import IsOrgAdminOrAppUser
from .. import serializers
2021-05-24 11:11:47 +00:00
from ..models import Application
2020-10-19 12:13:01 +00:00
2021-06-11 03:05:40 +00:00
__all__ = ['ApplicationViewSet']
2020-10-19 12:13:01 +00:00
class ApplicationViewSet(OrgBulkModelViewSet):
2021-05-24 11:11:47 +00:00
model = Application
2021-07-21 09:26:51 +00:00
filterset_fields = {
'name': ['exact'],
'category': ['exact'],
'type': ['exact', 'in'],
}
search_fields = ('name', 'type', 'category')
2020-10-19 12:13:01 +00:00
permission_classes = (IsOrgAdminOrAppUser,)
serializer_classes = {
'default': serializers.ApplicationSerializer,
'get_tree': TreeNodeSerializer
}
@action(methods=['GET'], detail=False, url_path='tree')
def get_tree(self, request, *args, **kwargs):
show_count = request.query_params.get('show_count', '1') == '1'
queryset = self.filter_queryset(self.get_queryset())
tree_nodes = Application.create_tree_nodes(queryset, show_count=show_count)
serializer = self.get_serializer(tree_nodes, many=True)
return Response(serializer.data)