perf: 资产 api 返回特有属性

pull/8991/head
ibuler 2022-10-19 18:56:46 +08:00
parent eb16e3c7cb
commit 1a2193d091
6 changed files with 36 additions and 7 deletions

View File

@ -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,

View File

@ -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

View File

@ -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")

View File

@ -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"))

View File

@ -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. """

View File

@ -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")
},
}