From 8dbe61100b920f84f95435d9efd192abf2e5286b Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 4 Jul 2023 10:29:27 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=20port=20from=20addr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/const/base.py | 2 +- apps/assets/const/protocol.py | 5 +++++ apps/assets/models/platform.py | 7 +++++++ apps/assets/serializers/platform.py | 6 +++--- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/assets/const/base.py b/apps/assets/const/base.py index 5aea334d4..6dc172c0e 100644 --- a/apps/assets/const/base.py +++ b/apps/assets/const/base.py @@ -56,7 +56,7 @@ class BaseType(TextChoices): for k, v in cls.get_choices(): tp_base = {**base_default, **base.get(k, {})} tp_auto = {**automation_default, **automation.get(k, {})} - tp_protocols = {**protocols_default, **protocols.get(k, {})} + tp_protocols = {**protocols_default, **{'port_from_addr': False}, **protocols.get(k, {})} tp_protocols = cls._parse_protocols(tp_protocols, k) tp_constrains = {**tp_base, 'protocols': tp_protocols, 'automation': tp_auto} constrains[k] = tp_constrains diff --git a/apps/assets/const/protocol.py b/apps/assets/const/protocol.py index a0a7c19e9..d20fd02c7 100644 --- a/apps/assets/const/protocol.py +++ b/apps/assets/const/protocol.py @@ -2,6 +2,7 @@ from django.db import models from django.utils.translation import gettext_lazy as _ from common.db.models import ChoicesMixin +from common.decorators import cached_method from .base import FillType __all__ = ['Protocol'] @@ -150,11 +151,13 @@ class Protocol(ChoicesMixin, models.TextChoices): return { cls.k8s: { 'port': 443, + 'port_from_addr': True, 'required': True, 'secret_types': ['token'], }, cls.http: { 'port': 80, + 'port_from_addr': True, 'secret_types': ['password'], 'setting': { 'autofill': { @@ -187,6 +190,7 @@ class Protocol(ChoicesMixin, models.TextChoices): cls.chatgpt: { 'port': 443, 'required': True, + 'port_from_addr': True, 'secret_types': ['api_key'], 'setting': { 'api_mode': { @@ -205,6 +209,7 @@ class Protocol(ChoicesMixin, models.TextChoices): } @classmethod + @cached_method(ttl=600) def settings(cls): return { **cls.device_protocols(), diff --git a/apps/assets/models/platform.py b/apps/assets/models/platform.py index f50177642..8fed01acf 100644 --- a/apps/assets/models/platform.py +++ b/apps/assets/models/platform.py @@ -8,6 +8,8 @@ from common.db.models import JMSBaseModel __all__ = ['Platform', 'PlatformProtocol', 'PlatformAutomation'] +from common.utils import lazyproperty + class PlatformProtocol(models.Model): name = models.CharField(max_length=32, verbose_name=_('Name')) @@ -26,6 +28,11 @@ class PlatformProtocol(models.Model): def secret_types(self): return Protocol.settings().get(self.name, {}).get('secret_types', ['password']) + @lazyproperty + def port_from_addr(self): + from assets.const.protocol import Protocol as ProtocolConst + return ProtocolConst.settings().get(self.name, {}).get('port_from_addr', False) + class PlatformAutomation(models.Model): ansible_enabled = models.BooleanField(default=False, verbose_name=_("Enabled")) diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py index 1915adcdb..8101bde23 100644 --- a/apps/assets/serializers/platform.py +++ b/apps/assets/serializers/platform.py @@ -46,13 +46,13 @@ class PlatformAutomationSerializer(serializers.ModelSerializer): class PlatformProtocolSerializer(serializers.ModelSerializer): setting = MethodSerializer(required=False, label=_("Setting")) + port_from_addr = serializers.BooleanField(label=_("Port from addr"), read_only=True) class Meta: model = PlatformProtocol fields = [ - "id", "name", "port", "primary", - "required", "default", "public", - "secret_types", "setting", + "id", "name", "port", "port_from_addr", "primary", + "required", "default", "public", "secret_types", "setting", ] extra_kwargs = { "primary": {