UnisKB/apps/common/field/common.py

68 lines
1.8 KiB
Python
Raw Normal View History

2025-05-27 07:21:57 +00:00
# coding=utf-8
"""
@project: maxkb
@Author
@file common.py
@date2024/1/11 18:44
@desc:
"""
2025-06-04 03:58:19 +00:00
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field
2025-05-27 07:21:57 +00:00
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
2025-06-04 03:58:19 +00:00
@extend_schema_field(OpenApiTypes.BINARY)
2025-05-27 07:21:57 +00:00
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