mirror of https://github.com/jumpserver/jumpserver
42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
|
|
from rest_framework import serializers
|
|
|
|
from common.utils import decrypt_password
|
|
from common.local import add_encrypted_field_set
|
|
|
|
__all__ = [
|
|
'ReadableHiddenField', 'EncryptedField'
|
|
]
|
|
|
|
|
|
# ReadableHiddenField
|
|
# -------------------
|
|
|
|
|
|
class ReadableHiddenField(serializers.HiddenField):
|
|
""" 可读的 HiddenField """
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.write_only = False
|
|
|
|
def to_representation(self, value):
|
|
if hasattr(value, 'id'):
|
|
return getattr(value, 'id')
|
|
return value
|
|
|
|
|
|
class EncryptedField(serializers.CharField):
|
|
def __init__(self, write_only=None, **kwargs):
|
|
if write_only is None:
|
|
write_only = True
|
|
kwargs['write_only'] = write_only
|
|
encrypted_key = kwargs.pop('encrypted_key', None)
|
|
super().__init__(**kwargs)
|
|
add_encrypted_field_set(encrypted_key or self.label)
|
|
|
|
def to_internal_value(self, value):
|
|
value = super().to_internal_value(value)
|
|
return decrypt_password(value)
|