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.
115 lines
4.2 KiB
115 lines
4.2 KiB
from rest_framework import serializers
|
|
|
|
from .models import Video, VideoCategory, VideoCollection
|
|
from .serializers_admin import AbsoluteFileField, AbsoluteImageField
|
|
|
|
|
|
class DovodiVideoCategorySerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
|
|
class Meta:
|
|
model = VideoCategory
|
|
fields = ["id", "title", "slug", "status", "order"]
|
|
|
|
|
|
class DovodiVideoCollectionSerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
thumbnail = AbsoluteImageField(required=False, allow_null=True)
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
|
|
class Meta:
|
|
model = VideoCollection
|
|
fields = [
|
|
"id",
|
|
"title",
|
|
"slug",
|
|
"summary",
|
|
"display_position",
|
|
"status",
|
|
"order",
|
|
"pin_top",
|
|
"thumbnail",
|
|
"remove_thumbnail",
|
|
]
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop("remove_thumbnail", False)
|
|
return super().create(validated_data)
|
|
|
|
def update(self, instance, validated_data):
|
|
remove_thumbnail = validated_data.pop("remove_thumbnail", False)
|
|
if remove_thumbnail and instance.thumbnail:
|
|
instance.thumbnail.delete(save=False)
|
|
instance.thumbnail = None
|
|
return super().update(instance, validated_data)
|
|
|
|
|
|
class DovodiVideoItemSerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
thumbnail = AbsoluteImageField(required=False, allow_null=True)
|
|
video_file = AbsoluteFileField(required=False, allow_null=True)
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
remove_video_file = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
|
|
class Meta:
|
|
model = Video
|
|
fields = [
|
|
"id",
|
|
"title",
|
|
"slug",
|
|
"thumbnail",
|
|
"description",
|
|
"video_type",
|
|
"video_file",
|
|
"video_url",
|
|
"video_time",
|
|
"status",
|
|
"view_count",
|
|
"created_at",
|
|
"updated_at",
|
|
"remove_thumbnail",
|
|
"remove_video_file",
|
|
]
|
|
read_only_fields = ["id", "view_count", "created_at", "updated_at"]
|
|
|
|
def validate(self, attrs):
|
|
video_type = attrs.get("video_type", getattr(self.instance, "video_type", None))
|
|
video_url = attrs.get("video_url", getattr(self.instance, "video_url", None))
|
|
video_file = attrs.get("video_file", getattr(self.instance, "video_file", None))
|
|
remove_video_file = attrs.get("remove_video_file", False)
|
|
|
|
if video_type == Video.VedioTypeChoices.YOUTUBE_LINK and not video_url:
|
|
raise serializers.ValidationError({
|
|
"video_url": "This field is required when video type is link.",
|
|
})
|
|
|
|
if video_type == Video.VedioTypeChoices.VIDEO_FILE and not video_file and not getattr(self.instance, "video_file", None):
|
|
raise serializers.ValidationError({
|
|
"video_file": "This field is required when video type is file.",
|
|
})
|
|
|
|
if video_type == Video.VedioTypeChoices.VIDEO_FILE and remove_video_file and "video_file" not in attrs:
|
|
raise serializers.ValidationError({
|
|
"video_file": "Please upload a new file before removing the current one.",
|
|
})
|
|
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop("remove_thumbnail", False)
|
|
validated_data.pop("remove_video_file", False)
|
|
return super().create(validated_data)
|
|
|
|
def update(self, instance, validated_data):
|
|
remove_thumbnail = validated_data.pop("remove_thumbnail", False)
|
|
remove_video_file = validated_data.pop("remove_video_file", False)
|
|
|
|
if remove_thumbnail and instance.thumbnail:
|
|
instance.thumbnail.delete(save=False)
|
|
instance.thumbnail = None
|
|
|
|
if remove_video_file and instance.video_file:
|
|
instance.video_file.delete(save=False)
|
|
instance.video_file = None
|
|
|
|
return super().update(instance, validated_data)
|