2020-10-19 12:13:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2022-09-22 06:51:19 +00:00
|
|
|
from rest_framework import status
|
2021-07-27 08:06:00 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2022-09-21 12:56:40 +00:00
|
|
|
from rest_framework.viewsets import GenericViewSet
|
|
|
|
|
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
|
2022-09-21 12:56:40 +00:00
|
|
|
from ..utils import db_port_manager
|
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
|
|
|
|
2022-09-21 12:56:40 +00:00
|
|
|
__all__ = ['ApplicationViewSet', 'DBListenPortViewSet']
|
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)
|
2022-09-21 12:56:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DBListenPortViewSet(GenericViewSet):
|
|
|
|
rbac_perms = {
|
|
|
|
'GET': 'applications.view_application',
|
|
|
|
'list': 'applications.view_application',
|
|
|
|
'db_info': 'applications.view_application',
|
|
|
|
}
|
|
|
|
|
|
|
|
http_method_names = ['get', 'post']
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
ports = db_port_manager.get_already_use_ports()
|
|
|
|
return Response(data=ports, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
@action(methods=['post'], detail=False, url_path='db-info')
|
|
|
|
def db_info(self, request, *args, **kwargs):
|
|
|
|
port = request.data.get("port")
|
|
|
|
db, msg = db_port_manager.get_db_by_port(port)
|
2022-09-22 06:51:19 +00:00
|
|
|
if db:
|
|
|
|
serializer = serializers.AppSerializer(instance=db)
|
|
|
|
data = serializer.data
|
|
|
|
_status = status.HTTP_200_OK
|
|
|
|
else:
|
2022-09-21 12:56:40 +00:00
|
|
|
data = {'error': msg}
|
2022-09-22 06:51:19 +00:00
|
|
|
_status = status.HTTP_404_NOT_FOUND
|
|
|
|
return Response(data=data, status=_status)
|