2022-11-14 06:44:18 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-07-11 10:09:06 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-12-21 09:17:54 +00:00
|
|
|
from common.drf.fields import EncryptedField
|
2022-11-14 06:44:18 +00:00
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
2022-12-06 03:03:14 +00:00
|
|
|
from ..models import ConnectionToken
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
__all__ = [
|
2022-12-05 02:48:19 +00:00
|
|
|
'ConnectionTokenSerializer', 'SuperConnectionTokenSerializer',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
|
2022-07-12 10:27:07 +00:00
|
|
|
expire_time = serializers.IntegerField(read_only=True, label=_('Expired time'))
|
2022-12-21 09:17:54 +00:00
|
|
|
input_secret = EncryptedField(
|
|
|
|
label=_("Input secret"), max_length=40960, required=False, allow_blank=True
|
|
|
|
)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ConnectionToken
|
2022-11-25 15:09:55 +00:00
|
|
|
fields_mini = ['id', 'value']
|
2022-07-11 10:09:06 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-12-05 04:42:15 +00:00
|
|
|
'user', 'asset', 'account', 'input_username',
|
2022-12-05 02:48:19 +00:00
|
|
|
'input_secret', 'connect_method', 'protocol', 'actions',
|
|
|
|
'date_expired', 'date_created', 'date_updated', 'created_by',
|
2022-11-22 13:54:40 +00:00
|
|
|
'updated_by', 'org_id', 'org_name',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
# 普通 Token 不支持指定 user
|
2022-11-23 08:11:17 +00:00
|
|
|
'user', 'expire_time',
|
2022-10-28 07:58:05 +00:00
|
|
|
'user_display', 'asset_display',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
2022-11-29 06:42:04 +00:00
|
|
|
fields = fields_small + read_only_fields
|
2022-11-25 15:09:55 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'value': {'read_only': True},
|
|
|
|
}
|
2022-07-11 10:09:06 +00:00
|
|
|
|
2022-12-05 05:37:37 +00:00
|
|
|
def get_user(self, attrs):
|
2022-10-28 07:58:05 +00:00
|
|
|
request = self.context.get('request')
|
|
|
|
user = request.user if request else None
|
|
|
|
return user
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
class SuperConnectionTokenSerializer(ConnectionTokenSerializer):
|
|
|
|
class Meta(ConnectionTokenSerializer.Meta):
|
2022-12-05 05:37:37 +00:00
|
|
|
read_only_fields = list(set(ConnectionTokenSerializer.Meta.read_only_fields) - {'user'})
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
def get_user(self, attrs):
|
2022-12-05 05:37:37 +00:00
|
|
|
return attrs.get('user')
|