perf: 优化终端注册时名称长度处理逻辑

pull/6750/head^2
Michael Bai 2021-09-09 13:12:14 +08:00 committed by Jiangjie.Bai
parent 3fb368c741
commit 7a2e93c087
1 changed files with 10 additions and 3 deletions

View File

@ -99,15 +99,22 @@ class TerminalRegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = Terminal
fields = ['name', 'type', 'comment', 'service_account', 'remote_addr']
extra_fields = {
'remote_addr': {'readonly': True}
extra_kwargs = {
'name': {'max_length': 1024},
'remote_addr': {'read_only': True}
}
def is_valid(self, raise_exception=False):
valid = super().is_valid(raise_exception=raise_exception)
if not valid:
return valid
data = {'name': self.validated_data.get('name')}
name = self.validated_data.get('name')
if len(name) > 128:
self.validated_data['comment'] = name
name = '{}...{}'.format(name[:32], name[-32:])
self.validated_data['name'] = name
data = {'name': name}
kwargs = {'data': data}
if self.instance and self.instance.user:
kwargs['instance'] = self.instance.user