mirror of https://github.com/jumpserver/jumpserver
feat(applications): 修改ApplicationAPI方法获取序列类的逻辑
parent
cc30b766f8
commit
ca2fc3cb5e
|
@ -24,20 +24,33 @@ class ApplicationViewSet(OrgBulkModelViewSet):
|
|||
serializer_class = super().get_serializer_class()
|
||||
app_type = self.request.query_params.get('type')
|
||||
app_category = self.request.query_params.get('category')
|
||||
type_options = list(dict(models.Category.get_all_type_serializer_mapper()).keys())
|
||||
category_options = list(dict(models.Category.get_category_serializer_mapper()).keys())
|
||||
|
||||
# TODO: app_type invalid
|
||||
# TODO: app_category invalid
|
||||
if app_type and app_type not in type_options:
|
||||
raise JMSException(
|
||||
'Invalid query parameter `type`, select from the following options: {}'
|
||||
''.format(type_options)
|
||||
)
|
||||
if app_category and app_category not in category_options:
|
||||
raise JMSException(
|
||||
'Invalid query parameter `category`, select from the following options: {}'
|
||||
''.format(category_options)
|
||||
)
|
||||
|
||||
if self.action in [
|
||||
'create', 'update', 'partial_update', 'bulk_update', 'partial_bulk_update'
|
||||
] and not app_type:
|
||||
# action: create / update ...
|
||||
raise JMSException(
|
||||
'The `{}` action must take the `type` query parameter'.format(self.action)
|
||||
)
|
||||
|
||||
attrs_cls = None
|
||||
if app_type:
|
||||
attrs_cls = models.Category.get_type_serializer_cls(app_type)
|
||||
elif self.action in ['list', 'retrieve', 'metadata']:
|
||||
if app_category:
|
||||
attrs_cls = models.Category.get_category_serializer_cls(app_category)
|
||||
else:
|
||||
attrs_cls = serializers.CommonCategorySerializer
|
||||
|
||||
if not attrs_cls:
|
||||
raise JMSException(detail='Please bring the query parameter Category or Type')
|
||||
|
||||
elif app_category:
|
||||
# action: list / retrieve / metadata
|
||||
attrs_cls = models.Category.get_category_serializer_cls(app_category)
|
||||
else:
|
||||
attrs_cls = serializers.CommonCategorySerializer
|
||||
return type('ApplicationDynamicSerializer', (serializer_class,), {'attrs': attrs_cls()})
|
||||
|
|
|
@ -99,13 +99,17 @@ class Category(ChoiceSet):
|
|||
return mapper.get(tp, None)
|
||||
|
||||
@classmethod
|
||||
def get_category_serializer_cls(cls, cg):
|
||||
def get_category_serializer_mapper(cls):
|
||||
from ..serializers import remote_app, database_app, k8s_app
|
||||
mapper = {
|
||||
return {
|
||||
cls.db: database_app.DatabaseCategorySerializer,
|
||||
cls.remote_app: remote_app.RemmoteAppCategorySerializer,
|
||||
cls.cloud: k8s_app.CloudCategorySerializer,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_category_serializer_cls(cls, cg):
|
||||
mapper = cls.get_category_serializer_mapper()
|
||||
return mapper.get(cg, None)
|
||||
|
||||
|
||||
|
|
|
@ -21,25 +21,6 @@ class ApplicationSerializer(BulkOrgResourceModelSerializer):
|
|||
'created_by', 'date_created', 'date_updated', 'get_type_display',
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
app_type = ''
|
||||
attrs_data = {}
|
||||
request = self.context.get('request')
|
||||
if request:
|
||||
app_type = request.query_params.get('type')
|
||||
if hasattr(self, 'initial_data'):
|
||||
app_type = self.initial_data.get('type')
|
||||
attrs_data = self.initial_data.get('attrs')
|
||||
if not app_type:
|
||||
return
|
||||
attrs_cls = models.Category.get_type_serializer_cls(app_type)
|
||||
if attrs_data:
|
||||
attrs_serializer = attrs_cls(data=attrs_data)
|
||||
else:
|
||||
attrs_serializer = attrs_cls()
|
||||
self.fields['attrs'] = attrs_serializer
|
||||
|
||||
def create(self, validated_data):
|
||||
attrs = validated_data.pop('attrs', {})
|
||||
instance = super().create(validated_data)
|
||||
|
|
Loading…
Reference in New Issue