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.
267 lines
6.4 KiB
267 lines
6.4 KiB
from urllib.parse import urlparse, urlunparse
|
|
from rest_framework import serializers
|
|
from django.conf import settings
|
|
|
|
from apps.content.models import (
|
|
Banner, Content, Image, Text, ImageText, Video,
|
|
Gallery, GalleryImage, ImageObject, FAQ)
|
|
|
|
from . import Base64ImageField
|
|
|
|
|
|
BASE_CONTENT_FIELDS = (
|
|
'id',
|
|
'uuid',
|
|
'course',
|
|
'lesson',
|
|
'contest',
|
|
'live_lesson',
|
|
'title',
|
|
'position',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class BannerSerializer(serializers.ModelSerializer):
|
|
image = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Banner
|
|
fields = (
|
|
'id',
|
|
'text',
|
|
'button_text',
|
|
'url',
|
|
'image',
|
|
'use',
|
|
'color',
|
|
'stretch_image',
|
|
'future_date',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_image(self, banner):
|
|
if banner.image:
|
|
return 'http://' + settings.MAIN_HOST + '/' + banner.image.url
|
|
else:
|
|
return None
|
|
|
|
|
|
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,
|
|
)
|
|
image_thumbnail = Base64ImageField(
|
|
required=False, allow_empty_file=True, allow_null=True, read_only=True,
|
|
)
|
|
|
|
class Meta:
|
|
model = ImageObject
|
|
fields = (
|
|
'id',
|
|
'image',
|
|
'image_thumbnail',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class ImageCreateSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Image
|
|
fields = BASE_CONTENT_FIELDS + ('img',)
|
|
|
|
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 = BASE_CONTENT_FIELDS + ('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 = BASE_CONTENT_FIELDS + ('img', 'txt',)
|
|
|
|
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 = BASE_CONTENT_FIELDS + ('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 = BASE_CONTENT_FIELDS + ('gallery_images',)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'type',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def get_type(self, object):
|
|
return 'images'
|
|
|
|
|
|
class ContentSerializer(serializers.ModelSerializer):
|
|
type = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Content
|
|
fields = BASE_CONTENT_FIELDS
|
|
|
|
def get_type(self, object):
|
|
return 'content'
|
|
|
|
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 FAQSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = FAQ
|
|
fields = '__all__'
|
|
|