pull/8605/head
ibuler 2022-05-04 09:57:45 +08:00
parent 7025d46070
commit 8de57773aa
4 changed files with 22 additions and 34 deletions

View File

@ -37,17 +37,17 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet)
search_fields = ("hostname", "ip") search_fields = ("hostname", "ip")
ordering_fields = ("hostname", "ip", "port") ordering_fields = ("hostname", "ip", "port")
ordering = ('hostname', ) ordering = ('hostname', )
serializer_classes = { serializer_classes = (
'default': serializers.AssetSerializer, ('default', serializers.AssetSerializer),
'suggestion': serializers.MiniAssetSerializer, ('suggestion', serializers.MiniAssetSerializer),
'platform': serializers.PlatformSerializer, ('platform', serializers.PlatformSerializer),
'gateways': serializers.GatewayWithAuthSerializer ('gateways', serializers.GatewayWithAuthSerializer)
} )
rbac_perms = { rbac_perms = (
'match': 'assets.match_asset', ('match', 'assets.match_asset'),
'platform': 'assets.view_platform', ('platform', 'assets.view_platform'),
'gateways': 'assets.view_gateway' ('gateways', 'assets.view_gateway')
} )
extra_filter_backends = [ extra_filter_backends = [
FilterAssetByNodeFilterBackend, FilterAssetByNodeFilterBackend,
LabelFilterBackend, LabelFilterBackend,

View File

@ -52,17 +52,3 @@ class DatabaseSerializer(AssetSerializer):
**AssetSerializer.Meta.extra_kwargs, **AssetSerializer.Meta.extra_kwargs,
'db_name': {'required': True} 'db_name': {'required': True}
} }
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not self.instance:
self.set_port_default()
def set_port_default(self):
port = self.fields['port']
type_port_mapper = {
'mysql': 3306,
'postgresql': 5432,
'oracle': 22
}
port.default = ''

View File

@ -16,28 +16,29 @@ class SerializerMixin:
single_actions = ['put', 'retrieve', 'patch'] single_actions = ['put', 'retrieve', 'patch']
def get_serializer_classes(self): def get_serializer_classes(self):
return getattr(self, 'serializer_classes', None) classes = getattr(self, 'serializer_classes', None) or {}
return dict(classes)
def get_serializer_class_by_view_action(self): def get_serializer_class_by_view_action(self):
serializer_classes = self.get_serializer_classes() serializer_classes = self.get_serializer_classes()
if serializer_classes is None: if serializer_classes is None:
return None return None
if not isinstance(self.serializer_classes, dict): if not isinstance(serializer_classes, dict):
return None return None
serializer_classes = dict(serializer_classes)
view_action = self.request.query_params.get('action') or self.action or 'list' view_action = self.request.query_params.get('action') or self.action or 'list'
serializer_class = self.serializer_classes.get(view_action) serializer_class = serializer_classes.get(view_action)
if serializer_class is None: if serializer_class is None:
view_method = self.request.method.lower() view_method = self.request.method.lower()
serializer_class = self.serializer_classes.get(view_method) serializer_class = serializer_classes.get(view_method)
if serializer_class is None and view_action in self.single_actions: if serializer_class is None and view_action in self.single_actions:
serializer_class = self.serializer_classes.get('single') serializer_class = serializer_classes.get('single')
if serializer_class is None: if serializer_class is None:
serializer_class = self.serializer_classes.get('display') serializer_class = serializer_classes.get('display')
if serializer_class is None: if serializer_class is None:
serializer_class = self.serializer_classes.get('default') serializer_class = serializer_classes.get('default')
return serializer_class return serializer_class
def get_serializer_class(self): def get_serializer_class(self):

View File

@ -93,7 +93,8 @@ class RBACPermission(permissions.DjangoModelPermissions):
try: try:
queryset = self._queryset(view) queryset = self._queryset(view)
model_cls = queryset.model model_cls = queryset.model
except: except Exception as e:
raise e
model_cls = None model_cls = None
return model_cls return model_cls