feat(applications): 修改ApplicationAPI方法获取序列类的逻辑

pull/4886/head
Bai 2020-10-27 14:56:07 +08:00 committed by 老广
parent cc30b766f8
commit ca2fc3cb5e
3 changed files with 31 additions and 33 deletions

View File

@ -24,20 +24,33 @@ class ApplicationViewSet(OrgBulkModelViewSet):
serializer_class = super().get_serializer_class() serializer_class = super().get_serializer_class()
app_type = self.request.query_params.get('type') app_type = self.request.query_params.get('type')
app_category = self.request.query_params.get('category') 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 if app_type and app_type not in type_options:
# TODO: app_category invalid 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: if app_type:
attrs_cls = models.Category.get_type_serializer_cls(app_type) attrs_cls = models.Category.get_type_serializer_cls(app_type)
elif self.action in ['list', 'retrieve', 'metadata']: elif app_category:
if app_category: # action: list / retrieve / metadata
attrs_cls = models.Category.get_category_serializer_cls(app_category) attrs_cls = models.Category.get_category_serializer_cls(app_category)
else: else:
attrs_cls = serializers.CommonCategorySerializer attrs_cls = serializers.CommonCategorySerializer
if not attrs_cls:
raise JMSException(detail='Please bring the query parameter Category or Type')
return type('ApplicationDynamicSerializer', (serializer_class,), {'attrs': attrs_cls()}) return type('ApplicationDynamicSerializer', (serializer_class,), {'attrs': attrs_cls()})

View File

@ -99,13 +99,17 @@ class Category(ChoiceSet):
return mapper.get(tp, None) return mapper.get(tp, None)
@classmethod @classmethod
def get_category_serializer_cls(cls, cg): def get_category_serializer_mapper(cls):
from ..serializers import remote_app, database_app, k8s_app from ..serializers import remote_app, database_app, k8s_app
mapper = { return {
cls.db: database_app.DatabaseCategorySerializer, cls.db: database_app.DatabaseCategorySerializer,
cls.remote_app: remote_app.RemmoteAppCategorySerializer, cls.remote_app: remote_app.RemmoteAppCategorySerializer,
cls.cloud: k8s_app.CloudCategorySerializer, cls.cloud: k8s_app.CloudCategorySerializer,
} }
@classmethod
def get_category_serializer_cls(cls, cg):
mapper = cls.get_category_serializer_mapper()
return mapper.get(cg, None) return mapper.get(cg, None)

View File

@ -21,25 +21,6 @@ class ApplicationSerializer(BulkOrgResourceModelSerializer):
'created_by', 'date_created', 'date_updated', 'get_type_display', '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): def create(self, validated_data):
attrs = validated_data.pop('attrs', {}) attrs = validated_data.pop('attrs', {})
instance = super().create(validated_data) instance = super().create(validated_data)