diff --git a/apps/assets/models/asset/__init__.py b/apps/assets/models/asset/__init__.py index 51640e7cf..28f5e30c5 100644 --- a/apps/assets/models/asset/__init__.py +++ b/apps/assets/models/asset/__init__.py @@ -1,2 +1,6 @@ from .common import * from .host import * +from .database import * +from .network import * +from .remote_app import * +from .cloud import * diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index 05404ff94..04abf1f25 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -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") diff --git a/apps/assets/models/asset/database.py b/apps/assets/models/asset/database.py index 84d174277..7c688e6d6 100644 --- a/apps/assets/models/asset/database.py +++ b/apps/assets/models/asset/database.py @@ -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") diff --git a/apps/assets/models/asset/network.py b/apps/assets/models/asset/network.py index a17d99a2e..c6506bf6d 100644 --- a/apps/assets/models/asset/network.py +++ b/apps/assets/models/asset/network.py @@ -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 diff --git a/apps/assets/models/asset/remote_app.py b/apps/assets/models/asset/remote_app.py index 7eb7872c8..c7528baae 100644 --- a/apps/assets/models/asset/remote_app.py +++ b/apps/assets/models/asset/remote_app.py @@ -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')) diff --git a/apps/assets/serializers/__init__.py b/apps/assets/serializers/__init__.py index f48552c9d..f6d166db9 100644 --- a/apps/assets/serializers/__init__.py +++ b/apps/assets/serializers/__init__.py @@ -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 * diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 4070c6fc7..5028ba7c6 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -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 diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py new file mode 100644 index 000000000..0a8cfd265 --- /dev/null +++ b/apps/assets/serializers/platform.py @@ -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' + ]