From 046342ceee465a65fc89c303680753da3b337ffd Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 19 Jul 2023 16:53:09 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=B9=B3=E5=8F=B0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E8=AE=BE=E7=BD=AE=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/serializers/platform.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py index 12de700a9..d42eaab19 100644 --- a/apps/assets/serializers/platform.py +++ b/apps/assets/serializers/platform.py @@ -51,8 +51,9 @@ class PlatformProtocolSerializer(serializers.ModelSerializer): class Meta: model = PlatformProtocol fields = [ - "id", "name", "port", "port_from_addr", "primary", - "required", "default", "public", "secret_types", "setting", + "id", "name", "port", "port_from_addr", + "primary", "required", "default", "public", + "secret_types", "setting", ] extra_kwargs = { "primary": { @@ -154,6 +155,18 @@ class PlatformSerializer(WritableNestedModelSerializer): "domain_default": {"label": _('Default Domain')}, } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.set_initial_value() + + def set_initial_value(self): + if not hasattr(self, 'initial_data'): + return + if self.instance: + return + if not self.initial_data.get('automation'): + self.initial_data['automation'] = {} + @property def platform_category_type(self): if self.instance: @@ -199,8 +212,9 @@ class PlatformSerializer(WritableNestedModelSerializer): def validate_automation(self, automation): automation = automation or {} - automation = automation.get('ansible_enabled', False) \ - and self.constraints['automation'].get('ansible_enabled', False) + ansible_enabled = automation.get('ansible_enabled', False) \ + and self.constraints['automation'].get('ansible_enabled', False) + automation['ansible_enable'] = ansible_enabled return automation