2022-12-07 07:09:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-06-07 09:39:56 +00:00
|
|
|
import itertools
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework.views import Response
|
|
|
|
|
|
|
|
from common.permissions import IsValidUser
|
2023-06-07 09:39:56 +00:00
|
|
|
from common.utils import get_request_os, is_true, distinct
|
2022-12-07 07:09:01 +00:00
|
|
|
from terminal import serializers
|
|
|
|
from terminal.connect_methods import ConnectMethodUtil
|
|
|
|
|
|
|
|
__all__ = ['ConnectMethodListApi']
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectMethodListApi(generics.ListAPIView):
|
|
|
|
serializer_class = serializers.ConnectMethodSerializer
|
|
|
|
permission_classes = [IsValidUser]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2023-06-07 09:39:56 +00:00
|
|
|
os = self.request.query_params.get('os') or get_request_os(self.request)
|
|
|
|
flat = self.request.query_params.get('flat')
|
|
|
|
if is_true(flat):
|
2023-06-09 09:16:23 +00:00
|
|
|
queryset = ConnectMethodUtil.get_filtered_protocols_connect_methods(os)
|
2023-06-07 09:39:56 +00:00
|
|
|
queryset = itertools.chain.from_iterable(queryset.values())
|
|
|
|
queryset = distinct(queryset, key=lambda x: x['value'])
|
|
|
|
else:
|
2023-06-09 09:16:23 +00:00
|
|
|
queryset = ConnectMethodUtil.get_user_allowed_connect_methods(os, self.request.user)
|
2023-06-07 09:39:56 +00:00
|
|
|
return queryset
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
return Response(queryset)
|