perf(application): 优化一些小细节

pull/4886/head
Bai 2020-10-29 15:46:32 +08:00 committed by 老广
parent 4a09dc6e3e
commit c9065fd96e
11 changed files with 33 additions and 29 deletions

View File

@ -41,16 +41,17 @@ class ApplicationViewSet(OrgBulkModelViewSet):
if self.action in [
'create', 'update', 'partial_update', 'bulk_update', 'partial_bulk_update'
] and not app_type:
# action: create / update ...
# action: create / update
raise JMSException(
'The `{}` action must take the `type` query parameter'.format(self.action)
)
if app_type:
# action: create / update / list / retrieve / metadata
attrs_cls = models.Category.get_type_serializer_cls(app_type)
elif app_category:
# action: list / retrieve / metadata
attrs_cls = models.Category.get_category_serializer_cls(app_category)
else:
attrs_cls = serializers.CommonCategorySerializer
attrs_cls = models.Category.get_no_password_serializer_cls()
return type('ApplicationDynamicSerializer', (serializer_class,), {'attrs': attrs_cls()})

View File

@ -39,11 +39,11 @@ class SerializeApplicationToTreeNodeMixin:
'meta': {'type': 'k8s_app'}
}
def dispatch_serialize(self, application):
def _serialize(self, application):
method_name = f'_serialize_{application.category}'
data = getattr(self, method_name)(application)
return data
def serialize_applications(self, applications):
data = [self.dispatch_serialize(application) for application in applications]
data = [self._serialize(application) for application in applications]
return data

View File

@ -27,8 +27,14 @@ class RemoteAppConnectionInfoApi(generics.RetrieveAPIView):
permission_classes = (IsAppUser, )
serializer_class = RemoteAppConnectionInfoSerializer
@staticmethod
def check_category_allowed(obj):
if not obj.category_is_remote_app:
raise JMSException(
'The request instance(`{}`) is not of category `remote_app`'.format(obj.category)
)
def get_object(self):
obj = super().get_object()
if not models.Category.is_remote_app(obj.category):
raise JMSException('The request instance is not of category `remote_app`')
self.check_category_allowed(obj)
return obj

View File

@ -102,9 +102,9 @@ class Category(ChoiceSet):
def get_category_serializer_mapper(cls):
from ..serializers import remote_app, database_app, k8s_app
return {
cls.db: database_app.DatabaseCategorySerializer,
cls.remote_app: remote_app.RemmoteAppCategorySerializer,
cls.cloud: k8s_app.CloudCategorySerializer,
cls.db: database_app.DBAttrsSerializer,
cls.remote_app: remote_app.RemoteAppAttrsSerializer,
cls.cloud: k8s_app.CloudAttrsSerializer,
}
@classmethod
@ -113,8 +113,9 @@ class Category(ChoiceSet):
return mapper.get(cg, None)
@classmethod
def is_remote_app(cls, cg):
return cg == cls.remote_app
def get_no_password_serializer_cls(cls):
from ..serializers import common
return common.NoPasswordSerializer
class Application(CommonModelMixin, OrgModelMixin):
@ -136,3 +137,6 @@ class Application(CommonModelMixin, OrgModelMixin):
category_display = self.get_category_display()
type_display = self.get_type_display()
return f'{self.name}({type_display})[{category_display}]'
def category_is_remote_app(self):
return self.category == Category.remote_app

View File

@ -1,7 +1,7 @@
from rest_framework import serializers
class CommonCategorySerializer(serializers.JSONField):
class NoPasswordSerializer(serializers.JSONField):
def to_representation(self, value):
new_value = {}
for k, v in value.items():

View File

@ -9,7 +9,7 @@ from common.serializers import AdaptedBulkListSerializer
from .. import models
class DatabaseCategorySerializer(serializers.Serializer):
class DBAttrsSerializer(serializers.Serializer):
host = serializers.CharField(max_length=128, label=_('Host'))
port = serializers.IntegerField(label=_('Port'))
database = serializers.CharField(
@ -17,19 +17,15 @@ class DatabaseCategorySerializer(serializers.Serializer):
)
class DatabaseAttrsSerializer(DatabaseCategorySerializer):
pass
class MySQLAttrsSerializer(DatabaseAttrsSerializer):
class MySQLAttrsSerializer(DBAttrsSerializer):
port = serializers.IntegerField(default=3306, label=_('Port'))
class PostgreAttrsSerializer(DatabaseAttrsSerializer):
class PostgreAttrsSerializer(DBAttrsSerializer):
port = serializers.IntegerField(default=5432, label=_('Port'))
class OracleAttrsSerializer(DatabaseAttrsSerializer):
class OracleAttrsSerializer(DBAttrsSerializer):
port = serializers.IntegerField(default=1521, label=_('Port'))

View File

@ -5,11 +5,11 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from .. import models
class CloudCategorySerializer(serializers.Serializer):
class CloudAttrsSerializer(serializers.Serializer):
cluster = serializers.CharField(max_length=1024, label=_('Cluster'))
class K8sAttrsSerializer(CloudCategorySerializer):
class K8sAttrsSerializer(CloudAttrsSerializer):
pass

View File

@ -14,11 +14,8 @@ from .. import const
from ..models import RemoteApp, Category, Application
class RemmoteAppCategorySerializer(serializers.Serializer):
class RemoteAppAttrsSerializer(serializers.Serializer):
asset = serializers.CharField(max_length=36, label=_('Assets'))
class RemoteAppAttrsSerializer(RemmoteAppCategorySerializer):
path = serializers.CharField(max_length=128, label=_('Remote App path'))

View File

@ -141,14 +141,14 @@ class SystemUser(BaseUser):
]
@property
def k8s_application_protocols(self):
def cloud_application_protocols(self):
return [self.PROTOCOL_K8S]
@property
def application_category_protocols(self):
protocols = []
protocols.extend(self.db_application_protocols)
protocols.extend(self.k8s_application_protocols)
protocols.extend(self.cloud_application_protocols)
return protocols
def is_need_push(self):

View File

@ -1,4 +1,4 @@
from .user_permission import *
from .application_permission import *
from .application_permission_relation import *
from .user_group_permission_application import *
from .user_group_permission import *