From 9aed51ffe974bda0b4a77b17f9e7851ac55631ea Mon Sep 17 00:00:00 2001 From: Bai Date: Wed, 28 Oct 2020 14:28:29 +0800 Subject: [PATCH] =?UTF-8?q?perf(applications):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=BA=8F=E5=88=97=E7=B1=BB=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/applications/serializers/database_app.py | 6 +- apps/applications/serializers/k8s_app.py | 2 +- apps/applications/serializers/remote_app.py | 138 +++++++++--------- 3 files changed, 76 insertions(+), 70 deletions(-) diff --git a/apps/applications/serializers/database_app.py b/apps/applications/serializers/database_app.py index e14ec0ef6..4f9df2e6d 100644 --- a/apps/applications/serializers/database_app.py +++ b/apps/applications/serializers/database_app.py @@ -10,9 +10,11 @@ from .. import models class DatabaseCategorySerializer(serializers.Serializer): - host = serializers.CharField(label=_('Host')) + host = serializers.CharField(max_length=128, label=_('Host')) port = serializers.IntegerField(label=_('Port')) - database = serializers.CharField(allow_blank=True, allow_null=True, label=_('Database')) + database = serializers.CharField( + max_length=128, allow_blank=True, allow_null=True, label=_('Database') + ) class DatabaseAttrsSerializer(DatabaseCategorySerializer): diff --git a/apps/applications/serializers/k8s_app.py b/apps/applications/serializers/k8s_app.py index 0296e221b..bd5bbbbe2 100644 --- a/apps/applications/serializers/k8s_app.py +++ b/apps/applications/serializers/k8s_app.py @@ -6,7 +6,7 @@ from .. import models class CloudCategorySerializer(serializers.Serializer): - cluster = serializers.CharField(label=_('Cluster')) + cluster = serializers.CharField(max_length=1024, label=_('Cluster')) class K8sAttrsSerializer(CloudCategorySerializer): diff --git a/apps/applications/serializers/remote_app.py b/apps/applications/serializers/remote_app.py index d0db0ce90..d1304cd4b 100644 --- a/apps/applications/serializers/remote_app.py +++ b/apps/applications/serializers/remote_app.py @@ -19,89 +19,40 @@ class RemmoteAppCategorySerializer(serializers.Serializer): class RemoteAppAttrsSerializer(RemmoteAppCategorySerializer): - path = serializers.CharField(label=_('Remote App path')) + path = serializers.CharField(max_length=128, label=_('Remote App path')) class ChromeAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' - path = serializers.CharField(label=_('Remote App path'), default=REMOTE_APP_PATH) - chrome_target = serializers.CharField(label=_('Target URL')) - chrome_username = serializers.CharField(label=_('Username')) - chrome_password = serializers.CharField(write_only=True, label=_('Password')) + path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) + chrome_target = serializers.CharField(max_length=128, label=_('Target URL')) + chrome_username = serializers.CharField(max_length=128, label=_('Username')) + chrome_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) class MySQLWorkbenchAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files\MySQL\MySQL Workbench 8.0 CE\MySQLWorkbench.exe' - path = serializers.CharField(label=_('Remote App path'), default=REMOTE_APP_PATH) - mysql_workbench_ip = serializers.CharField(label=_('IP')) + path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) + mysql_workbench_ip = serializers.CharField(max_length=128, label=_('IP')) mysql_workbench_port = serializers.IntegerField(label=_('Port')) - mysql_workbench_name = serializers.CharField(label=_('Database')) - mysql_workbench_username = serializers.CharField(label=_('Username')) - mysql_workbench_password = serializers.CharField(write_only=True, label=_('Password')) + mysql_workbench_name = serializers.CharField(max_length=128, label=_('Database')) + mysql_workbench_username = serializers.CharField(max_length=128, label=_('Username')) + mysql_workbench_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) class VMwareClientAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files (x86)\VMware\Infrastructure\Virtual Infrastructure Client\Launcher\VpxClient.exe' - path = serializers.CharField(label=_('Remote App path'), default=REMOTE_APP_PATH) - vmware_target = serializers.CharField(label=_('Target URL')) - vmware_username = serializers.CharField(label=_('Username')) - vmware_password = serializers.CharField(write_only=True, label=_('Password')) + path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) + vmware_target = serializers.CharField(max_length=128, label=_('Target URL')) + vmware_username = serializers.CharField(max_length=128, label=_('Username')) + vmware_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) class CustomRemoteAppAttrsSeralizers(RemoteAppAttrsSerializer): - custom_cmdline = serializers.CharField(label=_('Operating parameter')) - custom_target = serializers.CharField(label=_('Target url')) - custom_username = serializers.CharField(label=_('Username')) - custom_password = serializers.CharField(write_only=True, label=_('Password')) - - -class RemoteAppParamsDictField(CustomMetaDictField): - type_fields_map = const.REMOTE_APP_TYPE_FIELDS_MAP - default_type = const.REMOTE_APP_TYPE_CHROME - convert_key_remove_type_prefix = False - convert_key_to_upper = False - - -class RemoteAppSerializer(BulkOrgResourceModelSerializer): - params = RemoteAppParamsDictField() - type_fields_map = const.REMOTE_APP_TYPE_FIELDS_MAP - - class Meta: - model = RemoteApp - list_serializer_class = AdaptedBulkListSerializer - fields = [ - 'id', 'name', 'asset', 'asset_info', 'type', 'get_type_display', - 'path', 'params', 'date_created', 'created_by', 'comment', - ] - read_only_fields = [ - 'created_by', 'date_created', 'asset_info', - 'get_type_display' - ] - - def process_params(self, instance, validated_data): - new_params = copy.deepcopy(validated_data.get('params', {})) - tp = validated_data.get('type', '') - - if tp != instance.type: - return new_params - - old_params = instance.params - fields = self.type_fields_map.get(instance.type, []) - for field in fields: - if not field.get('write_only', False): - continue - field_name = field['name'] - new_value = new_params.get(field_name, '') - old_value = old_params.get(field_name, '') - field_value = new_value if new_value else old_value - new_params[field_name] = field_value - - return new_params - - def update(self, instance, validated_data): - params = self.process_params(instance, validated_data) - validated_data['params'] = params - return super().update(instance, validated_data) + custom_cmdline = serializers.CharField(max_length=128, label=_('Operating parameter')) + custom_target = serializers.CharField(max_length=128, label=_('Target url')) + custom_username = serializers.CharField(max_length=128, label=_('Username')) + custom_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer): @@ -149,3 +100,56 @@ class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer): @staticmethod def get_asset(obj): return obj.attrs.get('asset') + + +# TODO: DELETE +class RemoteAppParamsDictField(CustomMetaDictField): + type_fields_map = const.REMOTE_APP_TYPE_FIELDS_MAP + default_type = const.REMOTE_APP_TYPE_CHROME + convert_key_remove_type_prefix = False + convert_key_to_upper = False + + +# TODO: DELETE +class RemoteAppSerializer(BulkOrgResourceModelSerializer): + params = RemoteAppParamsDictField() + type_fields_map = const.REMOTE_APP_TYPE_FIELDS_MAP + + class Meta: + model = RemoteApp + list_serializer_class = AdaptedBulkListSerializer + fields = [ + 'id', 'name', 'asset', 'asset_info', 'type', 'get_type_display', + 'path', 'params', 'date_created', 'created_by', 'comment', + ] + read_only_fields = [ + 'created_by', 'date_created', 'asset_info', + 'get_type_display' + ] + + def process_params(self, instance, validated_data): + new_params = copy.deepcopy(validated_data.get('params', {})) + tp = validated_data.get('type', '') + + if tp != instance.type: + return new_params + + old_params = instance.params + fields = self.type_fields_map.get(instance.type, []) + for field in fields: + if not field.get('write_only', False): + continue + field_name = field['name'] + new_value = new_params.get(field_name, '') + old_value = old_params.get(field_name, '') + field_value = new_value if new_value else old_value + new_params[field_name] = field_value + + return new_params + + def update(self, instance, validated_data): + params = self.process_params(instance, validated_data) + validated_data['params'] = params + return super().update(instance, validated_data) + +