mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-12 13:31:56 +00:00
reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (#5379)
* reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 * reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (2) * reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (3) Co-authored-by: Bai <bugatti_it@163.com>
This commit is contained in:
@@ -4,20 +4,37 @@
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from common.drf.fields import DynamicMappingField
|
||||
from .attrs import get_attrs_field_dynamic_mapping_rules
|
||||
from common.drf.serializers import DynamicMappingSerializer
|
||||
from .attrs import attrs_field_dynamic_mapping_serializers
|
||||
|
||||
from .. import models
|
||||
|
||||
__all__ = [
|
||||
'ApplicationSerializer',
|
||||
'IncludeDynamicMappingSerializerFieldApplicationSerializerMixin',
|
||||
]
|
||||
|
||||
|
||||
class ApplicationSerializer(BulkOrgResourceModelSerializer):
|
||||
class IncludeDynamicMappingSerializerFieldApplicationSerializerMixin(serializers.Serializer):
|
||||
attrs = DynamicMappingSerializer(mapping_serializers=attrs_field_dynamic_mapping_serializers)
|
||||
|
||||
def get_attrs_mapping_path(self, mapping_serializers):
|
||||
request = self.context['request']
|
||||
query_type = request.query_params.get('type')
|
||||
query_category = request.query_params.get('category')
|
||||
if query_type:
|
||||
mapping_path = ['type', query_type]
|
||||
elif query_category:
|
||||
mapping_path = ['category', query_category]
|
||||
else:
|
||||
mapping_path = ['default']
|
||||
return mapping_path
|
||||
|
||||
|
||||
class ApplicationSerializer(IncludeDynamicMappingSerializerFieldApplicationSerializerMixin,
|
||||
BulkOrgResourceModelSerializer):
|
||||
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category'))
|
||||
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type'))
|
||||
attrs = DynamicMappingField(mapping_rules=get_attrs_field_dynamic_mapping_rules())
|
||||
|
||||
class Meta:
|
||||
model = models.Application
|
||||
@@ -33,3 +50,4 @@ class ApplicationSerializer(BulkOrgResourceModelSerializer):
|
||||
_attrs = self.instance.attrs if self.instance else {}
|
||||
_attrs.update(attrs)
|
||||
return _attrs
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..category import RemoteAppSerializer
|
||||
from ..application_category import RemoteAppSerializer
|
||||
|
||||
|
||||
__all__ = ['ChromeSerializer']
|
@@ -2,7 +2,7 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..category import RemoteAppSerializer
|
||||
from ..application_category import RemoteAppSerializer
|
||||
|
||||
|
||||
__all__ = ['CustomSerializer']
|
@@ -1,4 +1,4 @@
|
||||
from ..category import CloudSerializer
|
||||
from ..application_category import CloudSerializer
|
||||
|
||||
|
||||
__all__ = ['K8SSerializer']
|
@@ -1,7 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..category import DBSerializer
|
||||
from ..application_category import DBSerializer
|
||||
|
||||
|
||||
__all__ = ['MySQLSerializer']
|
@@ -1,7 +1,7 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..category import RemoteAppSerializer
|
||||
from ..application_category import RemoteAppSerializer
|
||||
|
||||
|
||||
__all__ = ['MySQLWorkbenchSerializer']
|
@@ -1,7 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..category import DBSerializer
|
||||
from ..application_category import DBSerializer
|
||||
|
||||
|
||||
__all__ = ['OracleSerializer']
|
@@ -1,7 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..category import DBSerializer
|
||||
from ..application_category import DBSerializer
|
||||
|
||||
|
||||
__all__ = ['PostgreSerializer']
|
@@ -1,7 +1,7 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..category import RemoteAppSerializer
|
||||
from ..application_category import RemoteAppSerializer
|
||||
|
||||
|
||||
__all__ = ['VMwareClientSerializer']
|
@@ -1,12 +1,11 @@
|
||||
import copy
|
||||
from rest_framework import serializers
|
||||
from applications import const
|
||||
from common.drf.fields import IgnoreSensitiveInfoReadOnlyJSONField
|
||||
from . import category, type as application_type
|
||||
from . import application_category, application_type
|
||||
|
||||
|
||||
__all__ = [
|
||||
'get_attrs_field_dynamic_mapping_rules', 'get_attrs_field_mapping_rule_by_view',
|
||||
'get_serializer_by_application_type',
|
||||
'attrs_field_dynamic_mapping_serializers',
|
||||
'get_serializer_class_by_application_type',
|
||||
]
|
||||
|
||||
|
||||
@@ -35,16 +34,15 @@ type_custom = const.ApplicationTypeChoices.custom.value
|
||||
type_k8s = const.ApplicationTypeChoices.k8s.value
|
||||
|
||||
|
||||
# define `attrs` field `DynamicMappingField` mapping_rules
|
||||
# -----------------------------------------------------
|
||||
# define `attrs` field `dynamic mapping serializers`
|
||||
# --------------------------------------------------
|
||||
|
||||
|
||||
__ATTRS_FIELD_DYNAMIC_MAPPING_RULES = {
|
||||
'default': IgnoreSensitiveInfoReadOnlyJSONField,
|
||||
attrs_field_dynamic_mapping_serializers = {
|
||||
'category': {
|
||||
category_db: category.DBSerializer,
|
||||
category_remote_app: category.RemoteAppSerializer,
|
||||
category_cloud: category.CloudSerializer,
|
||||
category_db: application_category.DBSerializer,
|
||||
category_remote_app: application_category.RemoteAppSerializer,
|
||||
category_cloud: application_category.CloudSerializer,
|
||||
},
|
||||
'type': {
|
||||
# db
|
||||
@@ -63,32 +61,5 @@ __ATTRS_FIELD_DYNAMIC_MAPPING_RULES = {
|
||||
}
|
||||
|
||||
|
||||
# Note:
|
||||
# The dynamic mapping rules of `attrs` field is obtained
|
||||
# through call method `get_attrs_field_dynamic_mapping_rules`
|
||||
|
||||
def get_attrs_field_dynamic_mapping_rules():
|
||||
return copy.deepcopy(__ATTRS_FIELD_DYNAMIC_MAPPING_RULES)
|
||||
|
||||
|
||||
# get `attrs dynamic field` mapping rule by `view object`
|
||||
# ----------------------------------------------------
|
||||
|
||||
|
||||
def get_attrs_field_mapping_rule_by_view(view):
|
||||
query_type = view.request.query_params.get('type')
|
||||
query_category = view.request.query_params.get('category')
|
||||
if query_type:
|
||||
mapping_rule = ['type', query_type]
|
||||
elif query_category:
|
||||
mapping_rule = ['category', query_category]
|
||||
else:
|
||||
mapping_rule = ['default']
|
||||
return mapping_rule
|
||||
|
||||
|
||||
# get `category` mapping `serializer`
|
||||
# -----------------------------------
|
||||
|
||||
def get_serializer_by_application_type(app_tp):
|
||||
return __ATTRS_FIELD_DYNAMIC_MAPPING_RULES['type'].get(app_tp)
|
||||
def get_serializer_class_by_application_type(_application_type):
|
||||
return attrs_field_dynamic_mapping_serializers['type'].get(_application_type)
|
||||
|
@@ -31,8 +31,8 @@ class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
返回Guacamole需要的RemoteApp配置参数信息中的parameters参数
|
||||
"""
|
||||
from .attrs import get_serializer_by_application_type
|
||||
serializer_class = get_serializer_by_application_type(obj.type)
|
||||
from .attrs import get_serializer_class_by_application_type
|
||||
serializer_class = get_serializer_class_by_application_type(obj.type)
|
||||
fields = serializer_class().get_fields()
|
||||
|
||||
parameters = [obj.type]
|
||||
|
@@ -1,16 +0,0 @@
|
||||
from .attrs import get_attrs_field_mapping_rule_by_view
|
||||
|
||||
__all__ = [
|
||||
'get_dynamic_mapping_fields_mapping_rule_by_view'
|
||||
]
|
||||
|
||||
|
||||
#
|
||||
# get `dynamic fields` mapping rule by `view object`
|
||||
# ----------------------------------------------------
|
||||
|
||||
|
||||
def get_dynamic_mapping_fields_mapping_rule_by_view(view):
|
||||
return {
|
||||
'attrs': get_attrs_field_mapping_rule_by_view(view=view),
|
||||
}
|
Reference in New Issue
Block a user