perf: 其他 asset model

pull/8605/head
ibuler 2022-04-26 21:30:01 +08:00
parent 44d192cbe7
commit 0a2b2ad127
8 changed files with 61 additions and 29 deletions

View File

@ -1,2 +1,6 @@
from .common import *
from .host import *
from .database import *
from .network import *
from .remote_app import *
from .cloud import *

View File

@ -186,7 +186,7 @@ class Asset(AbsConnectivity, ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
@lazyproperty
def platform_base(self):
return self.platform.base
return self.platform.type
@lazyproperty
def admin_user_username(self):
@ -302,6 +302,11 @@ class Asset(AbsConnectivity, ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
system_users = SystemUser.objects.filter(id__in=system_user_ids)
return system_users
def save(self, *args, **kwargs):
self.type = self.platform.type
self.category = self.platform.category
return super().save(*args, **kwargs)
class Meta:
unique_together = [('org_id', 'hostname')]
verbose_name = _("Asset")

View File

@ -1,8 +1,11 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from .common import Asset
class Database(Asset):
database = models.CharField(max_length=1024, verbose_name=_("Database"), blank=True)
db_name = models.CharField(max_length=1024, verbose_name=_("Database"), blank=True)
class Meta:
verbose_name = _("Database")

View File

@ -1,10 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from .common import Asset
from .device_info import DeviceInfo
class Network(Asset):
device_info = models.ForeignKey(DeviceInfo, on_delete=models.SET_NULL,
null=True, verbose_name=_("Device info"))
pass

View File

@ -1,9 +1,16 @@
from django.utils.translation import gettext_lazy as _
from django.db import models
from orgs.mixins.models import OrgModelMixin
from common.mixins.models import CommonModelMixin
from .common import Asset
class RemoteAppHost(CommonModelMixin, OrgModelMixin):
host = models.ForeignKey('assets.Host', verbose_name=_("Host"))
system_user = models.ForeignKey('assets.SystemUser', verbose_name=_("System user"))
class RemoteApp(Asset):
app_path = models.CharField(max_length=1024, verbose_name=_("App path"))
attrs = models.JSONField(default=dict, verbose_name=_('Attrs'))

View File

@ -11,4 +11,5 @@ from .cmd_filter import *
from .gathered_user import *
from .favorite_asset import *
from .account import *
from .platform import *
from .backup import *

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
#
from rest_framework import serializers
from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
@ -9,7 +8,6 @@ from ...models import Asset, Node, Platform, SystemUser
__all__ = [
'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer',
'ProtocolsField', 'PlatformSerializer',
'AssetTaskSerializer', 'AssetsTaskSerializer', 'ProtocolsField',
]
@ -95,6 +93,7 @@ class AssetSerializer(BulkOrgResourceModelSerializer):
]
read_only_fields = [
'connectivity', 'date_verified', 'created_by', 'date_created',
'category', 'type'
]
fields = fields_small + fields_fk + fields_m2m + read_only_fields
extra_kwargs = {
@ -112,6 +111,13 @@ class AssetSerializer(BulkOrgResourceModelSerializer):
admin_user_field.queryset = SystemUser.objects.filter(type=SystemUser.Type.admin)
return fields
def validate_type(self, value):
print(self.initial_data)
return value
def validate_category(self, value):
return value
@classmethod
def setup_eager_loading(cls, queryset):
""" Perform necessary eager loading of data. """
@ -168,25 +174,6 @@ class MiniAssetSerializer(serializers.ModelSerializer):
fields = AssetSerializer.Meta.fields_mini
class PlatformSerializer(serializers.ModelSerializer):
meta = serializers.DictField(required=False, allow_null=True, label=_('Meta'))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# TODO 修复 drf SlugField RegexValidator bug之后记得删除
validators = self.fields['name'].validators
if isinstance(validators[-1], RegexValidator):
validators.pop()
class Meta:
model = Platform
fields = [
'id', 'name', 'category', 'type', 'charset',
'internal', 'meta', 'comment'
]
class AssetSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = Asset

View File

@ -0,0 +1,29 @@
from rest_framework import serializers
from django.core.validators import RegexValidator
from django.utils.translation import gettext_lazy as _
from assets.models import Platform
__all__ = ['PlatformSerializer']
class PlatformSerializer(serializers.ModelSerializer):
category_display = serializers.ReadOnlyField(source='get_category_display', label=_("Category display"))
type_display = serializers.ReadOnlyField(source='get_type_display', label=_("Type display"))
meta = serializers.DictField(required=False, allow_null=True, label=_('Meta'))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# TODO 修复 drf SlugField RegexValidator bug之后记得删除
validators = self.fields['name'].validators
if isinstance(validators[-1], RegexValidator):
validators.pop()
class Meta:
model = Platform
fields = [
'id', 'name', 'category', 'category_display',
'type', 'type_display', 'charset',
'internal', 'meta', 'comment'
]