perf: 优化各serializer字段翻译

pull/5727/head
ibuler 2021-03-11 20:09:23 +08:00 committed by Jiangjie.Bai
parent 5f48e7aeb2
commit 09303ecc56
8 changed files with 40 additions and 28 deletions

View File

@ -521,18 +521,24 @@ class SomeNodesMixin:
@classmethod
def org_root(cls):
org_roots = cls.org_root_nodes()
if org_roots:
return org_roots[0]
ori_org = get_current_org()
# 如果使用current_org 在set_current_org时会死循环
if ori_org.is_root():
root = cls.default_node()
elif ori_org.is_default():
root = cls.default_node()
else:
ori_org = get_current_org()
if ori_org and ori_org.is_default():
return cls.default_node()
if ori_org and ori_org.is_root():
return None
org_roots = cls.org_root_nodes()
org_roots_length = len(org_roots)
if org_roots_length == 1:
return org_roots[0]
elif org_roots_length == 0:
root = cls.create_org_root_node()
return root
return root
else:
raise ValueError('Current org root node not 1, get {}'.format(org_roots_length))
@classmethod
def initial_some_nodes(cls):

View File

@ -46,6 +46,7 @@ class NodeAssetTreePagination(AssetPaginationBase):
node = self._view.node
if not node:
node = Node.org_root()
logger.debug(f'Hit node.assets_amount[{node.assets_amount}] -> {self._request.get_full_path()}')
return node.assets_amount
if node:
logger.debug(f'Hit node.assets_amount[{node.assets_amount}] -> {self._request.get_full_path()}')
return node.assets_amount
return None

View File

@ -168,7 +168,7 @@ class AssetDisplaySerializer(AssetSerializer):
class PlatformSerializer(serializers.ModelSerializer):
meta = serializers.DictField(required=False, allow_null=True)
meta = serializers.DictField(required=False, allow_null=True, label=_('Meta'))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _
from common.drf.serializers import AdaptedBulkListSerializer
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
@ -9,16 +10,17 @@ from ..models import Label
class LabelSerializer(BulkOrgResourceModelSerializer):
asset_count = serializers.SerializerMethodField()
asset_count = serializers.SerializerMethodField(label=_("Assets amount"))
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category display'))
class Meta:
model = Label
fields = [
'id', 'name', 'value', 'category', 'is_active', 'comment',
'date_created', 'asset_count', 'assets', 'get_category_display'
'date_created', 'asset_count', 'assets', 'category_display'
]
read_only_fields = (
'category', 'date_created', 'asset_count', 'get_category_display'
'category', 'date_created', 'asset_count',
)
extra_kwargs = {
'assets': {'required': False}

View File

@ -33,7 +33,7 @@ class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
'priority', 'username_same_with_user',
'auto_push', 'cmd_filters', 'sudo', 'shell', 'comment',
'auto_generate_key', 'sftp_root', 'token',
'assets_amount', 'date_created', 'created_by',
'assets_amount', 'date_created', 'date_updated', 'created_by',
'home', 'system_groups', 'ad_domain'
]
extra_kwargs = {
@ -155,7 +155,8 @@ class SystemUserListSerializer(SystemUserSerializer):
'auto_push', 'sudo', 'shell', 'comment',
"assets_amount", 'home', 'system_groups',
'auto_generate_key', 'ad_domain',
'sftp_root',
'sftp_root', 'created_by', 'date_created',
'date_updated',
]
extra_kwargs = {
'password': {"write_only": True},

View File

@ -40,7 +40,7 @@ class ActionsDisplayField(ActionsField):
class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
actions = ActionsField(required=False, allow_null=True)
is_valid = serializers.BooleanField(read_only=True)
is_expired = serializers.BooleanField(read_only=True)
is_expired = serializers.BooleanField(read_only=True, label=_('Is expired'))
class Meta:
model = AssetPermission

View File

@ -667,6 +667,13 @@ class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, AbstractUser):
else:
return user_default
@property
def login_blocked(self):
key_prefix_block = "_LOGIN_BLOCK_{}"
key_block = key_prefix_block.format(self.username)
blocked = bool(cache.get(key_block))
return blocked
def delete(self, using=None, keep_parents=False):
if self.pk == 1 or self.username == 'admin':
return

View File

@ -27,15 +27,17 @@ class UserSerializer(CommonBulkSerializerMixin, serializers.ModelSerializer):
choices=PASSWORD_STRATEGY_CHOICES, required=False,
label=_('Password strategy'), write_only=True, default=0
)
mfa_enabled = serializers.BooleanField(label=_('MFA enabled'))
mfa_force_enabled = serializers.BooleanField(label=_('MFA force enabled'))
mfa_level_display = serializers.ReadOnlyField(source='get_mfa_level_display', label=_('MFA level for display'))
login_blocked = serializers.SerializerMethodField(label=_('Login blocked'))
login_blocked = serializers.BooleanField(label=_('Login blocked'))
is_expired = serializers.BooleanField(label=_('Is expired'))
can_update = serializers.SerializerMethodField(label=_('Can update'))
can_delete = serializers.SerializerMethodField(label=_('Can delete'))
org_roles = serializers.ListField(
label=_('Organization role name'), allow_null=True, required=False,
child=serializers.ChoiceField(choices=ORG_ROLE.choices), default=["User"]
)
key_prefix_block = "_LOGIN_BLOCK_{}"
class Meta:
model = User
@ -72,8 +74,6 @@ class UserSerializer(CommonBulkSerializerMixin, serializers.ModelSerializer):
'org_role_display': {'label': _('Organization role name')},
'role_display': {'label': _('Super role name')},
'total_role_display': {'label': _('Total role name')},
'mfa_enabled': {'label': _('MFA enabled')},
'mfa_force_enabled': {'label': _('MFA force enabled')},
'role': {'default': "User"},
}
@ -144,11 +144,6 @@ class UserSerializer(CommonBulkSerializerMixin, serializers.ModelSerializer):
self.context['request'], self.context['view'], obj
)
def get_login_blocked(self, obj):
key_block = self.key_prefix_block.format(obj.username)
blocked = bool(cache.get(key_block))
return blocked
class UserRetrieveSerializer(UserSerializer):
login_confirm_settings = serializers.PrimaryKeyRelatedField(read_only=True,