diff --git a/apps/assets/api/platform.py b/apps/assets/api/platform.py index bce8505cf..ca6dc882f 100644 --- a/apps/assets/api/platform.py +++ b/apps/assets/api/platform.py @@ -1,4 +1,5 @@ from django.db.models import Count +from django_filters import rest_framework as filters from rest_framework import generics from rest_framework import serializers from rest_framework.decorators import action @@ -14,6 +15,14 @@ from common.serializers import GroupedChoiceSerializer __all__ = ['AssetPlatformViewSet', 'PlatformAutomationMethodsApi', 'PlatformProtocolViewSet'] +class PlatformFilter(filters.FilterSet): + name__startswith = filters.CharFilter(field_name='name', lookup_expr='istartswith') + + class Meta: + model = Platform + fields = ['name', 'category', 'type'] + + class AssetPlatformViewSet(JMSModelViewSet): queryset = Platform.objects.all() serializer_classes = { @@ -21,7 +30,7 @@ class AssetPlatformViewSet(JMSModelViewSet): 'list': PlatformListSerializer, 'categories': GroupedChoiceSerializer, } - filterset_fields = ['name', 'category', 'type'] + filterset_class = PlatformFilter search_fields = ['name'] ordering = ['-internal', 'name'] rbac_perms = { diff --git a/apps/assets/serializers/gateway.py b/apps/assets/serializers/gateway.py index e596ef5a3..e435f0af7 100644 --- a/apps/assets/serializers/gateway.py +++ b/apps/assets/serializers/gateway.py @@ -14,6 +14,11 @@ class GatewaySerializer(HostSerializer): class Meta(HostSerializer.Meta): model = Gateway + def validate_platform(self, p): + if not p.name.startswith('Gateway'): + raise serializers.ValidationError(_('The platform must start with Gateway')) + return p + def validate_name(self, value): queryset = Asset.objects.filter(name=value) if self.instance: diff --git a/apps/i18n/lina/en.json b/apps/i18n/lina/en.json index 9f555cd26..b3b98cddc 100644 --- a/apps/i18n/lina/en.json +++ b/apps/i18n/lina/en.json @@ -543,6 +543,7 @@ "GatewayCreate": "Create gateway", "GatewayList": "Gateways", "GatewayUpdate": "Update the gateway", + "GatewayPlatformHelpText": "Only platforms with names starting with ‘Gateway’ can be used as gateways.", "GatherAccounts": "Gather accounts", "GatherAccountsHelpText": "Collect account information on assets. the collected account information can be imported into the system for centralized management.", "GatheredAccountList": "Gathered accounts", diff --git a/apps/i18n/lina/zh.json b/apps/i18n/lina/zh.json index 7b8d3c4d5..ef832d014 100644 --- a/apps/i18n/lina/zh.json +++ b/apps/i18n/lina/zh.json @@ -543,6 +543,7 @@ "GatewayCreate": "创建网关", "GatewayList": "网关列表", "GatewayUpdate": "更新网关", + "GatewayPlatformHelpText": "网关平台只能选择以 Gateway 开头的平台", "GatherAccounts": "账号收集", "GatherAccountsHelpText": "收集资产上的账号信息。收集后的账号信息可以导入到系统中,方便统一管理", "GatheredAccountList": "收集的账号",