2021-01-12 10:06:42 +00:00
|
|
|
|
import copy
|
2021-01-01 23:25:23 +00:00
|
|
|
|
from rest_framework import serializers
|
2020-07-06 03:14:20 +00:00
|
|
|
|
from rest_framework.serializers import Serializer
|
2020-07-29 06:57:44 +00:00
|
|
|
|
from rest_framework.serializers import ModelSerializer
|
|
|
|
|
from rest_framework_bulk.serializers import BulkListSerializer
|
|
|
|
|
|
|
|
|
|
from common.mixins import BulkListSerializerMixin
|
2021-01-05 15:39:38 +00:00
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
from rest_framework.utils.serializer_helpers import BindingDict
|
2021-01-01 23:25:23 +00:00
|
|
|
|
from common.mixins.serializers import BulkSerializerMixin
|
2022-05-17 13:12:59 +00:00
|
|
|
|
from common.drf.fields import EncryptedField
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
2021-01-06 04:44:12 +00:00
|
|
|
|
'MethodSerializer',
|
2022-05-17 13:12:59 +00:00
|
|
|
|
'EmptySerializer', 'BulkModelSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer',
|
|
|
|
|
'SecretReadableMixin'
|
2021-01-01 23:25:23 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
|
# MethodSerializer
|
|
|
|
|
# ----------------
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
|
class MethodSerializer(serializers.Serializer):
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
|
def __init__(self, method_name=None, **kwargs):
|
|
|
|
|
self.method_name = method_name
|
2021-01-05 15:39:38 +00:00
|
|
|
|
super().__init__(**kwargs)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-11 07:31:28 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
# 生成swagger时使用
|
|
|
|
|
ref_name = None
|
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
def bind(self, field_name, parent):
|
2021-01-06 04:44:12 +00:00
|
|
|
|
if self.method_name is None:
|
|
|
|
|
method_name = 'get_{field_name}_serializer'.format(field_name=field_name)
|
|
|
|
|
self.method_name = method_name
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
super().bind(field_name, parent)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
@cached_property
|
2021-01-06 04:44:12 +00:00
|
|
|
|
def serializer(self) -> serializers.Serializer:
|
|
|
|
|
method = getattr(self.parent, self.method_name)
|
2021-01-12 10:06:42 +00:00
|
|
|
|
_serializer = method()
|
|
|
|
|
# 设置serializer的parent值,否则在serializer实例中获取parent会出现断层
|
|
|
|
|
setattr(_serializer, 'parent', self.parent)
|
|
|
|
|
return _serializer
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
@cached_property
|
|
|
|
|
def fields(self):
|
|
|
|
|
"""
|
2021-01-06 04:44:12 +00:00
|
|
|
|
重写此方法因为在 BindingDict 中要设置每一个 field 的 parent 为 `serializer`,
|
2021-01-05 15:39:38 +00:00
|
|
|
|
这样在调用 field.parent 时, 才会达到预期的结果,
|
|
|
|
|
比如: serializers.SerializerMethodField
|
|
|
|
|
"""
|
2021-01-12 10:06:42 +00:00
|
|
|
|
return self.serializer.fields
|
|
|
|
|
|
|
|
|
|
def run_validation(self, data=serializers.empty):
|
|
|
|
|
return self.serializer.run_validation(data)
|
|
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
return self.serializer.to_representation(instance)
|
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
return self.serializer.get_initial()
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Other Serializer
|
|
|
|
|
# ----------------
|
2020-07-06 03:14:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EmptySerializer(Serializer):
|
|
|
|
|
pass
|
2020-07-29 06:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulkModelSerializer(BulkSerializerMixin, ModelSerializer):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AdaptedBulkListSerializer(BulkListSerializerMixin, BulkListSerializer):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CeleryTaskSerializer(serializers.Serializer):
|
|
|
|
|
task = serializers.CharField(read_only=True)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
2022-05-17 13:12:59 +00:00
|
|
|
|
class SecretReadableMixin(serializers.Serializer):
|
|
|
|
|
""" 加密字段 (EncryptedField) 可读性 """
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super(SecretReadableMixin, self).__init__(*args, **kwargs)
|
|
|
|
|
if not hasattr(self, 'Meta') or not hasattr(self.Meta, 'extra_kwargs'):
|
|
|
|
|
return
|
|
|
|
|
extra_kwargs = self.Meta.extra_kwargs
|
|
|
|
|
for field_name, serializer_field in self.fields.items():
|
|
|
|
|
if not isinstance(serializer_field, EncryptedField):
|
|
|
|
|
continue
|
|
|
|
|
if field_name not in extra_kwargs:
|
|
|
|
|
continue
|
|
|
|
|
field_extra_kwargs = extra_kwargs[field_name]
|
|
|
|
|
if 'write_only' not in field_extra_kwargs:
|
|
|
|
|
continue
|
|
|
|
|
serializer_field.write_only = field_extra_kwargs['write_only']
|