jumpserver/apps/applications/api/application.py

39 lines
1.3 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
from common.mixins.api import SuggestionMixin
2021-09-12 13:00:51 +00:00
from ..hands import IsOrgAdminOrAppUser
2021-06-11 03:05:40 +00:00
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
2021-09-12 13:00:51 +00:00
class ApplicationViewSet(SuggestionMixin, 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.AppSerializer,
2021-09-12 13:00:51 +00:00
'get_tree': TreeNodeSerializer,
'suggestion': serializers.MiniAppSerializer
}
@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)