2019-05-20 11:39:53 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
2020-10-29 10:17:41 +00:00
|
|
|
from common.utils import get_logger
|
2021-01-03 21:27:03 +00:00
|
|
|
from ..models import Application
|
2019-05-20 11:39:53 +00:00
|
|
|
|
|
|
|
|
2020-10-29 10:17:41 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2019-05-20 11:39:53 +00:00
|
|
|
|
2021-01-03 21:27:03 +00:00
|
|
|
__all__ = ['RemoteAppConnectionInfoSerializer']
|
2019-05-20 11:39:53 +00:00
|
|
|
|
|
|
|
|
2020-10-28 06:28:29 +00:00
|
|
|
class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer):
|
|
|
|
parameter_remote_app = serializers.SerializerMethodField()
|
|
|
|
asset = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Application
|
|
|
|
fields = [
|
|
|
|
'id', 'name', 'asset', 'parameter_remote_app',
|
|
|
|
]
|
|
|
|
read_only_fields = ['parameter_remote_app']
|
|
|
|
|
2021-01-03 21:27:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_asset(obj):
|
|
|
|
return obj.attrs.get('asset')
|
|
|
|
|
2020-10-28 06:28:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_parameters(obj):
|
|
|
|
"""
|
|
|
|
返回Guacamole需要的RemoteApp配置参数信息中的parameters参数
|
|
|
|
"""
|
2021-01-05 15:39:38 +00:00
|
|
|
from .attrs import get_serializer_class_by_application_type
|
|
|
|
serializer_class = get_serializer_class_by_application_type(obj.type)
|
2021-01-03 21:27:03 +00:00
|
|
|
fields = serializer_class().get_fields()
|
|
|
|
|
|
|
|
parameters = [obj.type]
|
|
|
|
for field_name in list(fields.keys()):
|
|
|
|
if field_name in ['asset']:
|
|
|
|
continue
|
|
|
|
value = obj.attrs.get(field_name)
|
2020-10-28 06:28:29 +00:00
|
|
|
if not value:
|
|
|
|
continue
|
|
|
|
if field_name == 'path':
|
|
|
|
value = '\"%s\"' % value
|
2021-01-03 21:27:03 +00:00
|
|
|
parameters.append(str(value))
|
|
|
|
|
|
|
|
parameters = ' '.join(parameters)
|
|
|
|
return parameters
|
2020-10-28 06:28:29 +00:00
|
|
|
|
|
|
|
def get_parameter_remote_app(self, obj):
|
2021-01-03 21:27:03 +00:00
|
|
|
return {
|
|
|
|
'program': '||jmservisor',
|
2020-10-28 06:28:29 +00:00
|
|
|
'working_directory': '',
|
2021-01-03 21:27:03 +00:00
|
|
|
'parameters': self.get_parameters(obj)
|
2020-10-28 06:28:29 +00:00
|
|
|
}
|