2020-10-19 12:13:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2021-07-27 08:06:00 +00:00
|
|
|
|
2020-10-19 12:13:01 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2021-07-27 08:06:00 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2020-10-19 12:13:01 +00:00
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.tree import TreeNodeSerializer
|
2021-10-21 08:16:50 +00:00
|
|
|
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-01-03 21:27:03 +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'],
|
|
|
|
}
|
2021-07-16 09:09:06 +00:00
|
|
|
search_fields = ('name', 'type', 'category')
|
2020-10-19 12:13:01 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2021-07-27 08:06:00 +00:00
|
|
|
serializer_classes = {
|
2021-09-09 08:04:54 +00:00
|
|
|
'default': serializers.AppSerializer,
|
2021-09-12 13:00:51 +00:00
|
|
|
'get_tree': TreeNodeSerializer,
|
|
|
|
'suggestion': serializers.MiniAppSerializer
|
2021-07-27 08:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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)
|