From ca2fc3cb5e5783518c0b35482e8d1a6b8234edd1 Mon Sep 17 00:00:00 2001
From: Bai <bugatti_it@163.com>
Date: Tue, 27 Oct 2020 14:56:07 +0800
Subject: [PATCH] =?UTF-8?q?feat(applications):=20=E4=BF=AE=E6=94=B9Applica?=
 =?UTF-8?q?tionAPI=E6=96=B9=E6=B3=95=E8=8E=B7=E5=8F=96=E5=BA=8F=E5=88=97?=
 =?UTF-8?q?=E7=B1=BB=E7=9A=84=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 apps/applications/api/application.py         | 37 +++++++++++++-------
 apps/applications/models/application.py      |  8 +++--
 apps/applications/serializers/application.py | 19 ----------
 3 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/apps/applications/api/application.py b/apps/applications/api/application.py
index 076f0028e..49fc71664 100644
--- a/apps/applications/api/application.py
+++ b/apps/applications/api/application.py
@@ -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()})
diff --git a/apps/applications/models/application.py b/apps/applications/models/application.py
index 1d9483e42..3a21942f2 100644
--- a/apps/applications/models/application.py
+++ b/apps/applications/models/application.py
@@ -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)
 
 
diff --git a/apps/applications/serializers/application.py b/apps/applications/serializers/application.py
index 2c8038e60..2badfde2b 100644
--- a/apps/applications/serializers/application.py
+++ b/apps/applications/serializers/application.py
@@ -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)