2020-10-19 12:13:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2021-08-30 08:48:46 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
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
|
2022-02-24 04:03:40 +00:00
|
|
|
from common.mixins.api import SuggestionMixin
|
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'],
|
2022-03-17 07:26:38 +00:00
|
|
|
'category': ['exact', 'in'],
|
2021-07-21 09:26:51 +00:00
|
|
|
'type': ['exact', 'in'],
|
|
|
|
}
|
2021-07-16 09:09:06 +00:00
|
|
|
search_fields = ('name', 'type', 'category')
|
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
|
|
|
}
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-03-10 09:24:24 +00:00
|
|
|
'get_tree': 'applications.view_application',
|
2022-03-16 08:21:00 +00:00
|
|
|
'match': 'applications.match_application'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
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)
|