You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 lines
6.2 KiB
266 lines
6.2 KiB
from rest_framework import serializers
|
|
|
|
from apps.content.models import (
|
|
Content, Image, Text, ImageText, Video,
|
|
Gallery, GalleryImage, ImageObject,
|
|
)
|
|
|
|
from . import Base64ImageField
|
|
|
|
|
|
class ContentCreateSerializer(serializers.Serializer):
|
|
TYPE_CHOICES = (
|
|
'text',
|
|
'image',
|
|
'image-text',
|
|
'images',
|
|
'video',
|
|
)
|
|
type = serializers.ChoiceField(choices=TYPE_CHOICES)
|
|
data = serializers.JSONField()
|
|
|
|
def to_representation(self, obj):
|
|
if isinstance(obj, Image):
|
|
return ImageSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Text):
|
|
return TextSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, ImageText):
|
|
return ImageTextSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Video):
|
|
return VideoSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Gallery):
|
|
return GallerySerializer(obj, context=self.context).to_representation(obj)
|
|
return super(ContentSerializer, self).to_representation(obj)
|
|
|
|
|
|
class ImageObjectSerializer(serializers.ModelSerializer):
|
|
image = Base64ImageField(
|
|
required=True, allow_empty_file=False, allow_null=False, read_only=False,
|
|
)
|
|
|
|
class Meta:
|
|
model = ImageObject
|
|
fields = (
|
|
'id',
|
|
'image',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class ImageCreateSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Image
|
|
fields = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'img',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'image'
|
|
|
|
|
|
class ImageSerializer(ImageCreateSerializer):
|
|
img = ImageObjectSerializer()
|
|
|
|
|
|
class TextCreateSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Text
|
|
fields = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
) + ('txt',)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'text'
|
|
|
|
|
|
class TextSerializer(TextCreateSerializer):
|
|
pass
|
|
|
|
|
|
class ImageTextCreateSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = ImageText
|
|
fields = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'img',
|
|
'txt',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'image-text'
|
|
|
|
|
|
class ImageTextSerializer(ImageTextCreateSerializer):
|
|
img = ImageObjectSerializer()
|
|
|
|
|
|
class VideoCreateSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Video
|
|
fields = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
) + ('url',)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'video'
|
|
|
|
|
|
class VideoSerializer(VideoCreateSerializer):
|
|
pass
|
|
|
|
|
|
class GalleryImageCreateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = GalleryImage
|
|
fields = (
|
|
'id',
|
|
'gallery',
|
|
'img',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class GalleryImageSerializer(GalleryImageCreateSerializer):
|
|
img = ImageObjectSerializer()
|
|
|
|
|
|
class GallerySerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
gallery_images = GalleryImageSerializer(many=True)
|
|
|
|
class Meta:
|
|
model = Gallery
|
|
fields = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'gallery_images',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'images'
|
|
|
|
|
|
class ContentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Content
|
|
fields = (
|
|
'id',
|
|
'course',
|
|
'lesson',
|
|
'title',
|
|
'position',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def to_representation(self, obj):
|
|
if isinstance(obj, Image):
|
|
return ImageSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Text):
|
|
return TextSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, ImageText):
|
|
return ImageTextSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Video):
|
|
return VideoSerializer(obj, context=self.context).to_representation(obj)
|
|
elif isinstance(obj, Gallery):
|
|
return GallerySerializer(obj, context=self.context).to_representation(obj)
|
|
return super(ContentSerializer, self).to_representation(obj)
|
|
|