From 1a2193d091379273a709bc329b298bd6244e1207 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 19 Oct 2022 18:56:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B5=84=E4=BA=A7=20api=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=89=B9=E6=9C=89=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/migrations/0092_add_host.py | 6 +++--- apps/assets/models/asset/common.py | 8 ++++++++ apps/assets/models/asset/database.py | 6 ++++++ apps/assets/models/asset/web.py | 7 ++++--- apps/assets/serializers/asset/common.py | 9 ++++++++- apps/assets/serializers/asset/host.py | 7 +++++++ 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/apps/assets/migrations/0092_add_host.py b/apps/assets/migrations/0092_add_host.py index a6138c30a..10a5c8367 100644 --- a/apps/assets/migrations/0092_add_host.py +++ b/apps/assets/migrations/0092_add_host.py @@ -93,9 +93,9 @@ class Migration(migrations.Migration): fields=[ ('asset_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='assets.asset')), ('autofill', models.CharField(default='basic', max_length=16)), - ('password_selector', models.CharField(blank=True, default='', max_length=128)), - ('submit_selector', models.CharField(blank=True, default='', max_length=128)), - ('username_selector', models.CharField(blank=True, default='', max_length=128)) + ('password_selector', models.CharField(blank=True, default='', max_length=128, verbose_name='Password selector')), + ('submit_selector', models.CharField(blank=True, default='', max_length=128, verbose_name='Submit selector')), + ('username_selector', models.CharField(blank=True, default='', max_length=128, verbose_name='Username selector')), ], options={ 'abstract': False, diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index 573091959..ae3c99b60 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -105,6 +105,14 @@ class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel): def __str__(self): return '{0.name}({0.address})'.format(self) + @property + def category_property(self): + if not hasattr(self, self.category): + return {} + instance = getattr(self, self.category) + private_fields = [i.name for i in instance._meta.local_fields if i.name != 'asset_ptr'] + return {i: getattr(instance, i) for i in private_fields} + def get_target_ip(self): return self.address diff --git a/apps/assets/models/asset/database.py b/apps/assets/models/asset/database.py index a8f0daf15..de6b6a758 100644 --- a/apps/assets/models/asset/database.py +++ b/apps/assets/models/asset/database.py @@ -14,5 +14,11 @@ class Database(Asset): def ip(self): return self.address + @property + def category_property(self): + return { + 'db_name': self.db_name, + } + class Meta: verbose_name = _("Database") diff --git a/apps/assets/models/asset/web.py b/apps/assets/models/asset/web.py index afe7bed72..1bcd3a766 100644 --- a/apps/assets/models/asset/web.py +++ b/apps/assets/models/asset/web.py @@ -1,10 +1,11 @@ from django.db import models +from django.utils.translation import gettext_lazy as _ from .common import Asset class Web(Asset): autofill = models.CharField(max_length=16, default='basic') - username_selector = models.CharField(max_length=128, blank=True, default='') - password_selector = models.CharField(max_length=128, blank=True, default='') - submit_selector = models.CharField(max_length=128, blank=True, default='') + username_selector = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Username selector")) + password_selector = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Password selector")) + submit_selector = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Submit selector")) diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 059b7ffe3..8690b6c10 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -77,7 +77,8 @@ class AssetSerializer(OrgResourceSerializerMixin, WritableNestedModelSerializer) 'nodes', 'labels', 'accounts', 'protocols', 'nodes_display', ] read_only_fields = [ - 'category', 'type', 'connectivity', 'date_verified', + 'category', 'type', 'category_property', + 'connectivity', 'date_verified', 'created_by', 'date_created', ] fields = fields_small + fields_fk + fields_m2m + read_only_fields @@ -86,6 +87,12 @@ class AssetSerializer(OrgResourceSerializerMixin, WritableNestedModelSerializer) 'address': {'label': _('Address')}, } + def get_field_names(self, declared_fields, info): + names = super().get_field_names(declared_fields, info) + if self.__class__.__name__ != 'AssetSerializer': + names.remove('category_property') + return names + @classmethod def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ diff --git a/apps/assets/serializers/asset/host.py b/apps/assets/serializers/asset/host.py index 35f1ca00a..e01e1bab9 100644 --- a/apps/assets/serializers/asset/host.py +++ b/apps/assets/serializers/asset/host.py @@ -34,4 +34,11 @@ class HostSerializer(AssetSerializer): class Meta(AssetSerializer.Meta): model = Host fields = AssetSerializer.Meta.fields + ['info'] + extra_kwargs = { + **AssetSerializer.Meta.extra_kwargs, + 'address': { + 'label': _("IP/Host") + }, + } +