perf: 优化写法

pull/10420/head
ibuler 2 years ago committed by Jiangjie.Bai
parent 2aa03d5b79
commit 737032418a

@ -25,7 +25,8 @@ from terminal.models import EndpointRule
from ..models import ConnectionToken, date_expired_default from ..models import ConnectionToken, date_expired_default
from ..serializers import ( from ..serializers import (
ConnectionTokenSerializer, ConnectionTokenSecretSerializer, ConnectionTokenSerializer, ConnectionTokenSecretSerializer,
SuperConnectionTokenSerializer, ConnectTokenAppletOptionSerializer, ConnectionTokenUpdateSerializer SuperConnectionTokenSerializer, ConnectTokenAppletOptionSerializer,
ConnectionTokenUpdateSerializer
) )
__all__ = ['ConnectionTokenViewSet', 'SuperConnectionTokenViewSet'] __all__ = ['ConnectionTokenViewSet', 'SuperConnectionTokenViewSet']

@ -4,7 +4,6 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from common.serializers.fields import EncryptedField from common.serializers.fields import EncryptedField
from common.utils import lazyproperty
from orgs.mixins.serializers import OrgResourceModelSerializerMixin from orgs.mixins.serializers import OrgResourceModelSerializerMixin
from perms.serializers.permission import ActionChoicesField from perms.serializers.permission import ActionChoicesField
from ..models import ConnectionToken from ..models import ConnectionToken
@ -66,8 +65,7 @@ class ConnectionTokenUpdateSerializer(ConnectionTokenSerializer):
can_update_fields = ['is_reusable'] can_update_fields = ['is_reusable']
read_only_fields = list(set(ConnectionTokenSerializer.Meta.fields) - set(can_update_fields)) read_only_fields = list(set(ConnectionTokenSerializer.Meta.fields) - set(can_update_fields))
@lazyproperty def _get_date_expired(self):
def date_expired_max(self):
delta = self.instance.date_expired - self.instance.date_created delta = self.instance.date_expired - self.instance.date_created
if delta.total_seconds() > 3600 * 24: if delta.total_seconds() > 3600 * 24:
return self.instance.date_expired return self.instance.date_expired
@ -84,7 +82,7 @@ class ConnectionTokenUpdateSerializer(ConnectionTokenSerializer):
def validate(self, attrs): def validate(self, attrs):
reusable = attrs.get('is_reusable', False) reusable = attrs.get('is_reusable', False)
if reusable: if reusable:
attrs['date_expired'] = self.date_expired_max attrs['date_expired'] = self._get_date_expired()
return attrs return attrs

Loading…
Cancel
Save