2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.core.cache import cache
|
2021-03-26 11:09:34 +00:00
|
|
|
from rest_framework import generics
|
2018-11-23 02:25:35 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2021-01-20 10:33:38 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from django.conf import settings
|
2021-05-19 06:51:24 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-01-20 10:33:38 +00:00
|
|
|
|
2021-05-19 06:51:24 +00:00
|
|
|
from common.exceptions import JMSException
|
2020-07-29 06:57:44 +00:00
|
|
|
from common.drf.api import JMSBulkModelViewSet
|
2022-04-12 07:11:51 +00:00
|
|
|
from common.utils import get_object_or_none, get_request_ip
|
2022-02-17 12:13:31 +00:00
|
|
|
from common.permissions import WithBootstrapToken
|
2022-10-25 04:57:34 +00:00
|
|
|
from terminal.models import Terminal
|
|
|
|
from terminal import serializers
|
|
|
|
from terminal import exceptions
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2021-03-26 11:09:34 +00:00
|
|
|
'TerminalViewSet', 'TerminalConfig',
|
2021-01-20 10:33:38 +00:00
|
|
|
'TerminalRegistrationApi',
|
2018-11-23 02:25:35 +00:00
|
|
|
]
|
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
class TerminalViewSet(JMSBulkModelViewSet):
|
2018-11-23 02:25:35 +00:00
|
|
|
queryset = Terminal.objects.filter(is_deleted=False)
|
|
|
|
serializer_class = serializers.TerminalSerializer
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['name', 'remote_addr', 'type']
|
2021-09-27 03:28:29 +00:00
|
|
|
custom_filter_fields = ['status']
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2021-05-19 06:51:24 +00:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
if instance.get_online_session_count() > 0:
|
|
|
|
raise JMSException(
|
|
|
|
code='have_online_session',
|
|
|
|
detail=_('Have online sessions')
|
|
|
|
)
|
|
|
|
|
|
|
|
self.perform_destroy(instance)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
2021-09-27 03:28:29 +00:00
|
|
|
s = self.request.query_params.get('status')
|
|
|
|
if not s:
|
2020-12-10 12:50:22 +00:00
|
|
|
return queryset
|
2022-11-04 03:40:16 +00:00
|
|
|
filtered_queryset_id = [str(q.id) for q in queryset if q.load == s]
|
2020-12-10 12:50:22 +00:00
|
|
|
queryset = queryset.filter(id__in=filtered_queryset_id)
|
|
|
|
return queryset
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
class TerminalConfig(APIView):
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-21 08:24:03 +00:00
|
|
|
'GET': 'terminal.view_terminalconfig'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def get(self, request):
|
2021-01-12 10:06:42 +00:00
|
|
|
config = request.user.terminal.config
|
2021-01-20 10:33:38 +00:00
|
|
|
return Response(config, status=200)
|
|
|
|
|
|
|
|
|
|
|
|
class TerminalRegistrationApi(generics.CreateAPIView):
|
|
|
|
serializer_class = serializers.TerminalRegistrationSerializer
|
|
|
|
permission_classes = [WithBootstrapToken]
|
|
|
|
http_method_names = ['post']
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
if not settings.SECURITY_SERVICE_ACCOUNT_REGISTRATION:
|
|
|
|
data = {"error": "service account registration disabled"}
|
|
|
|
return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
return super().create(request, *args, **kwargs)
|