From 6bbb1819278ef2357e6d403a724b559b668b73ae Mon Sep 17 00:00:00 2001 From: shaohuzhang1 <80892890+shaohuzhang1@users.noreply.github.com> Date: Tue, 27 May 2025 15:21:57 +0800 Subject: [PATCH] feat: application save (#3151) --- apps/common/field/common.py | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 apps/common/field/common.py diff --git a/apps/common/field/common.py b/apps/common/field/common.py new file mode 100644 index 000000000..44452bc54 --- /dev/null +++ b/apps/common/field/common.py @@ -0,0 +1,66 @@ +# coding=utf-8 +""" + @project: maxkb + @Author:虎 + @file: common.py + @date:2024/1/11 18:44 + @desc: +""" +from rest_framework import serializers +from django.utils.translation import gettext_lazy as _ + + +class ObjectField(serializers.Field): + def __init__(self, model_type_list, **kwargs): + self.model_type_list = model_type_list + super().__init__(**kwargs) + + def to_internal_value(self, data): + for model_type in self.model_type_list: + if isinstance(data, model_type): + return data + self.fail(_('Message type error'), value=data) + + def to_representation(self, value): + return value + + +class InstanceField(serializers.Field): + def __init__(self, model_type, **kwargs): + self.model_type = model_type + super().__init__(**kwargs) + + def to_internal_value(self, data): + if not isinstance(data, self.model_type): + self.fail(_('Message type error'), value=data) + return data + + def to_representation(self, value): + return value + + +class FunctionField(serializers.Field): + + def to_internal_value(self, data): + if not callable(data): + self.fail(_('not a function'), value=data) + return data + + def to_representation(self, value): + return value + + +class UploadedImageField(serializers.ImageField): + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def to_representation(self, value): + return value + + +class UploadedFileField(serializers.FileField): + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def to_representation(self, value): + return value